如何在 Matplotlib 中從 Pandas series 中繪製條形圖?


要在 Matplotlib 中從 Pandas series 中繪製條形圖,我們可以採取以下步驟 -

  • 建立一個包含不同鍵的字典,介於 1 到 10 之間

  • 使用 Pandas 資料框架建立資料框

  • 使用 plot() 方法建立條形圖,其中**kind="bar"**。

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

示例

import pandas as pd
from matplotlib import pyplot as plt
plt.rcParams["figure.figsize"] = [7.00, 3.50]
plt.rcParams["figure.autolayout"] = True
d = {'y=1/x': [1 / i for i in range(1, 10)],
   'y=x': [i for i in range(1, 10)],
   'y=x^2': [i * i for i in range(1, 10)],
   'y=x^3': [i * i * i for i in range(1, 10)]}
df = pd.DataFrame(d)
df.plot(kind='bar')
plt.show()

輸出

更新於:2021 年 5 月 15 日

2K+ 次瀏覽

開啟你的 職業生涯

完成課程並獲得認證

開始吧
廣告
© . All rights reserved.