What will we cover in this tutorial?
We will learn how to visualization time series data in a DataFrame with Matplotlib.
This tutorial will show you.
- How to use Matplotlib with DataFrames.
- Use Matplotlib with subplots (the object-oriented way).
- How to make multiple plots in one figure.
- How to create bar-plots
Want to access the code directly in Jupyter Notebook?
You can get the Jupyter Notebooks from the GitHub here, where there are also direct links to Colab for an interactive experience.
Step 1: Read time series data into a DataFrame
A DataFrame is a two-dimensional tabular data. It is the primary data structure of Pandas. The data structure contains labeled axes (rows and columns).
To get access to a DataFrame data structure, you need to import the Pandas library.
import pandas as pd
Then we need some time series data. You con download your own CSV file from financial pages like Yahoo! Finance.
For this tutorial we will use a dataset available from the GitHub.
remote_file = "https://raw.githubusercontent.com/LearnPythonWithRune/FinancialDataAnalysisWithPython/main/AAPL.csv" data = pd.read_csv(remote_file, index_col=0, parse_dates=True)
The pd.read_csv(…) does all the magic. We set the index_col=0, which sets the first column of the CSV data file to be the index. This is the dates.
Then we set parse_dates=True, to ensure that dates are actually parsed as dates and not as strings. This is necessary to take advantage of being time series and index with time intervals.
Step 2: Import Matplotlib in Jupyter Notebook
When you import Matplotlib in Jupyter Notebook, you need to set a rendering mode.
import matplotlib.pyplot as plt %matplotlib notebook
We will use the notebook mode, which is interactive. This enables you to zoom in on interval, move around, and save the figure.
It is common to use inline mode for rendering in Jupyter Notebook. The inline mode creates a static image, which is not interactive.
Step 3: Use Matplotlib the Object-Oriente way
Matplotlib can be used in a functional way and an object-oriented way. Most use it in a functional way, which often creates more confusion, as it is not always intuitive how it works.
The object-oriented way leads to less confusion for the cost of one extra line of code and parsing one argument. Hence, the price is low for the gain.
fig, ax = plt.subplots() data['Close'].plot(ax=ax) ax.set_ylabel("Price") ax.set_title("AAPL")
The first line returns a figure and axis (fig and ax). The figure is where we put the axis, and the axis is the chart.
The actually plot is made by calling the DataFrame, actually, we access the column Close in this case, which is the Series of the time series of the historic Close prices.
Confused? Don’t worry about the details.
Notice, that we parse ax=ax to the plot. This ensures that we render the chart on the returned axis ax.
Finally, we add a y-label and a title to our axis.
Step 4: Creating multiple charts in one Matplotlib figure
How can we create multiple charts (or axes) in one Matplotlib figure?
Luckily, this is quite easy.
fig, ax = plt.subplots(2, 2) data['Open'].plot(ax=ax[0, 0], title="Open") data['High'].plot(ax=ax[0, 1], title="High") data['Low'].plot(ax=ax[1, 0], title="Low") data['Close'].plot(ax=ax[1, 1], title="Close") plt.tight_layout()
Here we see a few differences. First, notice plt.subplots(2, 2), which will return a figure fig, and a list of lists with 2-by-2 axes. Hence, ax is a two dimensional list of axes.
We can access the first axis with ax[0, 0,], and parse it as an argument to plot.
This continues for all the 4 plots we make, as you see.
Finally, we use plt.tight_layout(), which will ensures that the layout of the axes does not overlap. You can try without to see the difference.
Step 5: Create a bar-chart with Matplotlib
Finally, we will make a bar-chart with Matplotlib.
Actually, we will render a horizontal bar-chart.
fig, ax = plt.subplots() data['Volume'].loc['2020-07-01':'2020-08-15'].plot.barh(ax=ax)
We do it for the volume and only on a limited interval of time. This shows you how to take advantage of the time series aspect of the DataFrame.
Want to learn more?
This is part of the course of Master Technical Analysis with pandas.
In the next lesson you will learn how to Read Historical Prices from Yahoo! Finance with Python.
12% Investment Solution
Would you like to get 12% in return of your investments?
D. A. Carter promises and shows how his simple investment strategy will deliver that in the book The 12% Solution. The book shows how to test this statement by using backtesting.
Did Carter find a strategy that will consistently beat the market?
Actually, it is not that hard to use Python to validate his calculations. But we can do better than that. If you want to work smarter than traditional investors then continue to read here.
Do you know what the 5 key success factors every programmer must have?
How is it possible that some people become programmer so fast?
While others struggle for years and still fail.
Not only do they learn python 10 times faster they solve complex problems with ease.
What separates them from the rest?
I identified these 5 success factors that every programmer must have to succeed:
- Collaboration: sharing your work with others and receiving help with any questions or challenges you may have.
- Networking: the ability to connect with the right people and leverage their knowledge, experience, and resources.
- Support: receive feedback on your work and ask questions without feeling intimidated or judged.
- Accountability: stay motivated and accountable to your learning goals by surrounding yourself with others who are also committed to learning Python.
- Feedback from the instructor: receiving feedback and support from an instructor with years of experience in the field.
I know how important these success factors are for growth and progress in mastering Python.
That is why I want to make them available to anyone struggling to learn or who just wants to improve faster.
With the Python Circle community, you can take advantage of 5 key success factors every programmer must have.
Be part of something bigger and join the Python Circle community.
2 thoughts on “Matplotlib Visualization for DataFrame Time Series Data”
when I run the code in step 3 above I get the error…
AttributeError: ‘numpy.datetime64’ object has no attribute ‘toordinal’
In order to figure it out, I need to see the full code and error stack trace (the output given).
Can you share that? It would help.