在 pandas 和 matplotlib API 之间切换一开始可能会令人困惑。
解决方案:获取当前轴,然后使用标准matplotlib API进行注释。这让你开始:
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()
这会产生下图,您需要考虑修改titles http://matplotlib.org/examples/pylab_examples/figure_title.html, and 文本标签及其边界框 http://matplotlib.org/users/annotations_intro.html到轴对象: