如何使用 Matplotlib 在 Pandas 資料序列上繪製任意標記?


若要繪製 Pandas 資料序列上的任意標記,我們可以將帶有標記的 pyplot.plot() 用於。

步驟

  • 設定圖片大小並調整子圖之間的間距和填充。
  • 製作帶有軸標籤(包括時間序列)的 Pandas 資料序列。
  • 使用帶有 **linestyle="dotted"** 的 plot() 方法繪製序列索引。
  • 使用 **tick_params()** 方法旋轉重疊標籤。
  • 使用 **show()** 方法顯示圖片。

範例

import pandas as pd
from matplotlib import pyplot as plt
import numpy as np

plt.rcParams["figure.figsize"] = [7.50, 3.50]
plt.rcParams["figure.autolayout"] = True

ts = pd.Series(np.random.randn(10),
index=pd.date_range('2021-04-10', periods=10))

plt.plot(ts.index, ts, '*', ls='dotted', color='red')

plt.tick_params(rotation=45)

plt.show()

輸出

更新於: 2021 年 6 月 15 日

344 次瀏覽

開啟你的 職業生涯

完成課程,獲得認證

入門
廣告
© . All rights reserved.