使用 Matplotlib 在 Python/Pandas 資料框架中繪製頻率圖


若要使用 Matplotlib 在 Python/Pandas 資料幀中顯示頻率圖,我們可以執行以下步驟——

  • 設定圖形大小並調整子圖之間的邊距和周圍的邊距。
  • 建立圖形和一組子圖。
  • 製作一個二維、尺寸可變、可能包含異質表的表格資料。
  • 返回一個包含唯一值計數的序列。
  • 若要顯示圖形,請使用 show() 方法。

例項

import pandas as pd
from matplotlib import pyplot as plt

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

fig, ax = plt.subplots()

df = pd.DataFrame({'numbers': [2, 4, 1, 4, 3, 2, 1, 3, 2, 4]})
df['numbers'].value_counts().plot(ax=ax, kind='bar', xlabel='numbers', ylabel='frequency')

plt.show()

輸出

更新時間:2021-06-10

14K+ 瀏覽量

開啟您的職業生涯

完成課程獲得認證

開始學習
廣告
© . All rights reserved.