I have a time series that has a few years' worth of data, for example this:
ts = pd.Series(np.random.randn(1000), index=pd.date_range('1/1/2000', periods=1000))
ts = ts.cumsum()
ts.plot()
I also have two extra arrays: let's call the first
dates = [pd.datetime("2000-12-01"), pd.datetime("2001-01-03")]
And the second
labels = ["My birthday", "My dad's birthday"]
labels[i] contains the label for dates[i]. What I'd like to do is to display them in the time series graph so that they can be recognized. One possible visualization could be to display the date on the x axis, draw a vertical line starting from there and have the label either in a legend (with color coding) or somewhere next to the line.
The end result shouldn't be too different from this:
To create a time series plot in Excel, first select the time (DateTime in this case) Column and then the data series (streamflow in this case) column. Next, click on the Insert ribbon, and then select Scatter. From scatter plot options, select Scatter with Smooth Lines as shown below.
To customize date format, from 'Axes' under 'Style' menu choose 'Tick Labels' submenu. Next, select the axis you wish to modify, and then set 'advanced(d3-time-format)' for 'Label Format' attribute. A text box will appear where you can enter a custom time format.
In X-axis we should have a variable of DateTime. In Y-axis we can have the variable which we want to analyze with respect to time. plt. plot() method is used to plot the graph in matplotlib.
Switching between pandas and matplotlib APIs can be confusing at first.
The solution: get the current axis and then use standard matplotlib API to annotate. This starts you off:
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
ts = pd.Series(np.random.randn(1000),
index=pd.date_range('1/1/2000',
periods=1000))
ts = ts.cumsum()
ts.plot()
label_list = [
(pd.to_datetime("2001-05-01"), 'My\nbirthday', 'r'),
(pd.to_datetime("2001-10-16"), "Dad's\nbirthday", 'b')
]
ax = plt.gca()
for date_point, label, clr in label_list:
plt.axvline(x=date_point, color=clr)
plt.text(date_point, ax.get_ylim()[1]-4, label,
horizontalalignment='center',
verticalalignment='center',
color=clr,
bbox=dict(facecolor='white', alpha=0.9))
plt.show()
This produces the image below, and you need to look into modifying titles, and text labels and their bounding boxes to the axis object:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With