Customize your time series plot
1. Customize your time series plot
Plots are great because they allow users to understand the data. However, you may sometimes want to highlight specific events, or guide the user through your train of thought. In this video, you will personalize your time series plots to enable you to communicate the message that you are trying to convey.2. Slicing time series data
If the index of the pandas DataFrame consists of dates, they can be sliced using strings that represent the period in which you are interested. As shown in the first line, you can use strings like '1960:1970' to extract data from the 10 years between 1960 and 1970. Similarly, the second line extracts data from the 12 months between January 1950 and December 1950. Finally, the third line extracts data from the 15 days between January 1st 1960 and January 15th 1960.3. Plotting subset of your time series data
Here you subset the data from the 10 years between 1960 and 1970 and assign that to a new DataFrame called df_subset. You can then use the familiar dot plot() method to plot this subset of our time series data.4. Adding markers
Additional annotations can also help emphasize specific observations or events in your time series. This can be achieved with matplotlib by using the axvline and axvhline methods. As their names suggests, these allow us to draw vertical and horizontal lines that span our entire graphs. Here, the first line of code specifies to draw a red vertical line of width 1 at the date '1969-01-01'. The second line specifies to draw a green horizontal line at the value 100. Notice how we also used the linestyle argument introduced earlier to ensure that the lines have a "dashed" style.5. Using markers: the full code
Let's now review the full code needed to add markers to your plot. The first three commands plot our time series data and label the x-axis and y-axis. Finally, the last two lines add vertical and horizontal lines to our graph.6. Highlighting regions of interest
Beyond annotations, you can also highlight regions of interest to your time series plot. This can help provide more context around your data and really emphasize the story you are trying to convey with your graphs. In order to add a shaded section to a specific region of your plot, you can use the axvspan and axhspan methods in matplolib to produce vertical regions and horizontal regions, respectively. The first line of code draws a red vertical region with transparency of 0-point-5 between the x-axis values '1964-01-01' and '1968-01-01'. The second line draws a blue horizontal region with transparency of 0-point-2 between the y-axis values of 6 and 8.7. Highlighting regions of interest: the full code
Let's review the full code needed to highlight regions of interest to our plot. The first three lines plot our time series data and label the x-axis and y-axis. Finally, the last two lines add vertical and horizontal regions to our graph.8. Let's practice!
It is now time for you to try this yourself!Create Your Free Account
or
By continuing, you accept our Terms of Use, our Privacy Policy and that your data is stored in the USA.