從 Pandas 資料框繪製時註釋資料點


要在從 Pandas 資料框繪製時註釋資料點,我們可以執行以下步驟 -

  • 使用 DataFrame 建立 df,其帶有 x、y 和索引鍵。

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

  • 使用plot() 方法、kind='scatter'ax=axc='red'marker='x' 繪製一系列資料框。

  • 要使用索引值註釋散點,請迭代資料框。

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

示例

import numpy as np
import pandas as pd
from matplotlib import pyplot as plt
import string
plt.rcParams["figure.figsize"] = [7.00, 3.50]
plt.rcParams["figure.autolayout"] = True
df = pd.DataFrame({'x': np.random.rand(10), 'y': np.random.rand(10)}, index=list(string.ascii_lowercase[:10]))
fig, ax = plt.subplots()
df.plot('x', 'y', kind='scatter', ax=ax, c='red', marker='x')
for k, v in df.iterrows():
   ax.annotate(k, v)
plt.show()

輸出

更新於:2021 年 5 月 7 日

3K+ 閱讀次數

開啟您的 職業生涯

透過完成課程獲得認證

開始
廣告