在 Matplotlib 中註釋時間序列圖


要在 matplotlib 中註釋時間序列圖,我們可以執行以下步驟 -

  • 時間 數字 建立列表。

  • 使用subplots()方法,建立一個圖形和一組子圖。

  • 使用 plot_date() 方法,繪製包含具有線型“-. ”的日期的資料。

  • 使用 annotate() 方法在圖中註釋一個點。

  • 日期 ticklabels 經常會重疊,因此旋轉它們並將它們右對齊很有用。

  • 要顯示圖形,請使用 show() 方法。

示例

import datetime as dt
from matplotlib import pyplot as plt, dates as mdates
plt.rcParams["figure.figsize"] = [7.00, 3.50]
plt.rcParams["figure.autolayout"] = True
x = [dt.datetime(2021, 1, 1), dt.datetime(2021, 1, 2),
   dt.datetime(2021, 1, 3), dt.datetime(2021, 1, 4)]
y = [1, 3, 2, 5]
fig, ax = plt.subplots()
ax.plot_date(x, y, linestyle='-.')
ax.annotate('Point', (mdates.date2num(x[1]), y[1]), xytext=(15, 15),
   textcoords='offset points', arrowprops=dict(arrowstyle='-|>'))
fig.autofmt_xdate()
plt.show()

輸出

更新於: 07-05-2021

992 次瀏覽

啟動您的職業生涯事業

透過完成該課程獲得認證

開始
廣告
© . All rights reserved.