用 matplotlib 註釋來自 Pandas 資料幀中的點
要使用 Matplotlib 註釋來自 Pandas 資料幀中的點,我們可以採用以下步驟 -
設定圖形大小並調整子圖之間的填充和周圍填充。
製作一個滿足 x、y 和 textc 列的二維、尺寸可變、可能異構的表格資料。
使用 plot() 方法繪製列 x 和 y 資料點。
使用可選的軸間集合邏輯將 Pandas 物件沿著特定軸連線起來。
迭代 Pandas 物件。
使用 text() 方法放置每個繪製點上的文字。
要顯示圖形,使用 show() 方法。
示例
import pandas as pd import matplotlib.pyplot as plt plt.rcParams["figure.figsize"] = [7.50, 3.50] plt.rcParams["figure.autolayout"] = True df = pd.DataFrame(dict(x=[2, 4, 1, 5, 2], y=[2, 1, 3, 5, 7], text=['First', 'Second', 'Third', 'Fourth', 'Fifth'])) ax = df.set_index('x')['y'].plot(style='*', color='red', ms=20) a = pd.concat({'x': df.x, 'y': df.y, 'text': df.text}, axis=1) for i, point in a.iterrows(): ax.text(point['x']+0.125, point['y'], str(point['text'])) plt.show()
輸出
廣告