1. Plot your first time series
We covered how to leverage pandas to read and process time series data, but there is so much more you can do! In this section of the course, you will get your first taste of time series visualization in Python. Let's get started!
2. The Matplotlib library
In Python, matplotlib is an extensive package used to plot data. The library is built in a hierarchy, and most functions that can be used to add elements to your plots can be accessed via the matplotlib dot pyplot module. As a result, it is common to see Python practitioners import matplotlib dot pyplot using the alias plt.
matplotlib is the most widely used plotting library in Python and fortunately for us, the authors of the pandas library have implemented a dot plot() method on both Series and DataFrames objects that work as a simple wrapper around the plt dot plot() function in matplotlib, therefore allowing for fast and simple plotting.
3. Plotting time series data
In case of time series data, if the index consists of dates, pandas will automatically call a separate function to format the x-axis nicely as shown in the figure here.
4. Plotting time series data
Therefore, it is always recommended to set the dates of your time series as the index of your DataFrame using the dot set_index() method. Once you have finished defining the parameters of your figure, call plt dot show() to display the current figure that you are working on.
5. Adding style to your plots
The default style for matplotlib plot may not necessarily be your preferred style, but it is possible to change that. Because it would be time-consuming to customize each plot or to create your own template, several matplotlib style templates have been made available to use. These can be invoked by using the plt dot style dot use command, and will automatically add pre-specified defaults for fonts, lines and points, background colors etc... to your plots. In this case, we opted to use the famous fivethirtyeight style sheet.
6. FiveThirtyEight style
As you can see, the plot looks a lot better!
7. Matplotlib style sheets
If you are interested in looking at the list of available styles in matplotlib, you can use the plt dot style dot available command to display all options. As you can see, several well-known graphic styles such as fivethirtyeight , ggplot and even the Financial Times are included in the default matplotlib installation.
8. Describing your graphs with labels
It is important to remember that your plots should always tell a story and communicate the relevant information. Therefore, it is crucial that each of your plots are carefully annotated with axis labels and legends. The dot plot() method in pandas returns a matplotlib AxesSubplot object, and it is common practice to assign this returned object to a variable called ax. Doing so also allows you to include additional notations and specifications to your plot such as axis labels and titles. In particular, you can use the dot set_xlabel() , dot set_ylabel() and dot set_title() methods to specify the x and yaxis labels, and titles of your plot.
9. Figure size, linewidth, linestyle and fontsize
In addition to labels, you can also tweak several other parameters. For example, the figsize argument can be used to specify the length and height of your figure, which can be helpul for presentations or when you want to share your graphs with others. The line used to display the time series data can be modified by using the linewidth and linestyle arguments, which modify the width and style of the lines representing your time series data. Finally, you can also use the fontsize parameter to specify the font size of axis ticks, labels and titles.
10. Let's practice!
Now let's try some examples.