<p>下面是一个使用<a href="https://matplotlib.org/3.3.1/api/_as_gen/matplotlib.axes.Axes.annotate.html" rel="nofollow noreferrer">^{<cd1>}</a>的示例,另一个答案是:</p>
<pre><code>import matplotlib.pyplot as plt
import pandas as pd
dr = pd.date_range('02-01-2020', '07-01-2020', freq='1D')
y = pd.Series(range(len(dr))) ** 2
fig, ax = plt.subplots()
ax.plot(dr, y)
ax.annotate('1st Lockdown',
xy=(dr[50], y[50]), #annotate the 50th data point; you could select this in a better way
xycoords='data', #the xy we passed refers to the data
xytext=(0, 100), #where we put the text relative to the xy
textcoords='offset points', #what the xytext coordinates mean
arrowprops=dict(arrowstyle="->"), #style of the arrow
ha='center') #center the text horizontally
ax.annotate('2nd Lockdown',
xy=(dr[100], y[100]), xycoords='data',
xytext=(0, 100), textcoords='offset points',
arrowprops=dict(arrowstyle="->"), ha='center')
</code></pre>
<p><a href="https://i.stack.imgur.com/aVVWk.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/aVVWk.png" alt="enter image description here"/></a></p>
<p>有很多带有注解的选项,所以我会<a href="https://matplotlib.org/3.3.1/api/_as_gen/matplotlib.axes.Axes.annotate.html" rel="nofollow noreferrer">look for an example</a>匹配您想要做的事情,并尝试遵循它</p>
<p>注释似乎是在<code>matplotlib</code>中实现这一点的“聪明”方式;您也可以只使用<code>axvline</code>和<code>text</code>,但您可能需要添加额外的格式以使事情看起来更好:</p>
<pre><code>import matplotlib.pyplot as plt
import pandas as pd
dr = pd.date_range('02-01-2020', '07-01-2020', freq='1D')
y = pd.Series(range(len(dr))) ** 2
fig, ax = plt.subplots()
ax.plot(dr, y)
ax.axvline(dr[50], ymin=0, ymax=.7, color='gray')
ax.text(dr[50], .7, '1st Lockdown', transform=ax.get_xaxis_transform(), color='gray')
</code></pre>
<p><a href="https://i.stack.imgur.com/1T9M4.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/1T9M4.png" alt="enter image description here"/></a></p>