在 Matplotlib 的條形圖上繪製 Pandas 資料框的多列


要在 matplotlib 中的條形圖上繪製 Pandas 資料框的多列,我們可以採取以下步驟 −

  • 建立一個包含 1 到 10 範圍的不同鍵的字典。

  • 使用 Pandas 資料框建立一個數據框。

  • 使用 plot() 方法(其中 kind=”bar”)建立一個條形圖。

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

示例

import pandas as pd
from matplotlib import pyplot as plt
plt.rcParams["figure.figsize"] = [7.50, 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()

輸出

更新於: 09-4-2021

581 次瀏覽

開啟您的 職業生涯

結業即可獲得認證

開始
廣告
© . All rights reserved.