【Data Analysis(3)】- Matplotlib

A plot is better than countless words

TEJ 台灣經濟新報
TEJ-API Financial Data Analysis
6 min readApr 26, 2021

--

After two weeks of some complicated articles, let’s take a break and turn to our previous topic “Data Analysis” and introduce another important tool when working on the data analysis- data visualization!

If you forget what we have done few weeks ago, you can go back, review on them and come back again~~💪💪

✨ Highlights of this article ✨

  • 🌟 Matplotlib Intro/Application

🚪Links related to this article🚪

* What is the difference between Matplotlib and other packages?

There are many packages that could be used in Python to do the data visualization. In addition to Matplotlib, there are other packages to plot such as plotly, seaborn, cufflinks, etc. The logic and syntax are similar when working on those packages, but because the connection between Matplotlib and pandas is relatively closer, we will use it for the introduction in this article.
If you want to read the whole document of Matplotlib, you could go through this link: Matplotlib👍👍

* How to use Matplotlib?

Let’s code step by step!

1. Data Collecting(TEJAPI)

We first get the stock price data of TSMC(2330) and UMC(2303) from the TEJ API.

2. Basic Plotting (Single-Axis, Dual-Axis, Scatter, Histogram, Box)

* Single-Axis Plot

Introduction of different functions:

plt.plot(): Set the information of the plot such as length of width, color, name, etc.
plt.figure(figsize = (10,6)): Size of the plot
plt.legend(): Location of the legend
plt.grid(): Add the grid
plt.xlabel(): Name of X axis
plt.ylabel(): Name of Y axis
plt.title(): Name of title

* Dual-Axis Plot

There is often a need for dual-axis plots because it is not convenient for reading if the units or magnitudes of the 2 data are different and placed on the same axis.

We use fig, ax1 = plt.subplots() and ax2 = ax1.twinx() to generate the second chart but share the X-axis of the first chart. It could be understood as making two pictures with exactly the same unit on the X-axis and then stack them up!

* Scatter Plot, Histogram, and Box Plot

First, let’s introduce the scatter plot. It could be used to see the relationship between the two data. If we use the regression line at the same time, the correlation between the two data will be more easily observed.

Second, let’s introduce the histogram. We can use the histogram to understand the distribution and the frequencies of the yearly log return.

The last is the box plot, which could be used to display the statistical characteristics of the dataset and compare multiple datasets at the same time.

3. Financial Time-Series Data Plotting

Let’s combine the plot we’ve mentioned above with the financial data!

* Data Collecting

Historical Stock Price DataFrame

*Pandas built-in statistical function

Descriptive Statistics

We can use Pandas to present the descriptive statistics of the underlying assets with one-line code! It is really a clear and convenient way to process the data!!

* Pandas built-in plotting

Average Rate of Return

We can directly use the pandas to plot what we want. Isn’t it very convenient?

We use the DataFrame + the apply method to perform cumsum() function on the data after the log has been taken, and then perform np.exp() on the result in order to find the cumulative log return.

TSMC 20 Days High, Low and Avg Price

Here we use the same concept to find out the 20 days high, low, and the average price of TSMC, and style = [‘g- -’] to indicate the color and line presentation mode.

Finally, with the application of a little technical analysis, we can find the long-short term moving averages and the intersection through np.where() function. Therefore, it could be seen that when the Position changes from 1 to -1, the trading signal point is generated.

* Conclusion

There are multiple ways to display the plots. We can only introduce the most basic methods here. If you have a high interest in plotting, you could do like explore more application websites or read documents of those packages~
Then, we will go further into financial data analysis and applications in the next article, please look forward to it ❗️❗️

Finally, if you like this topic, please click 👏 below, giving us more support and encouragement. Additionally, if you have any questions or suggestions, please leave a message or email us, we will try our best to reply to you.👍👍

Links related to this article again!💪

If you have any question and difficulty, do not hesitate to contact us: Contact Information

Source Code🚪:

--

--

TEJ 台灣經濟新報
TEJ-API Financial Data Analysis

TEJ 為台灣本土第一大財經資訊公司,成立於 1990 年,提供金融市場基本分析所需資訊,以及信用風險、法遵科技、資產評價、量化分析及 ESG 等解決方案及顧問服務。鑒於財務金融領域日趨多元與複雜,TEJ 結合實務與學術界的精英人才,致力於開發機器學習、人工智慧 AI 及自然語言處理 NLP 等新技術,持續提供創新服務