如何使用 Matplotlib 建立折線圖?


要使用 matplotlib 建立折線圖,我們可以採取以下步驟 −

  • 設定圖形大小並調整各子圖間及周圍的填充。
  • 建立年份和人口增長率列表。
  • 使用 plot() 方法在折線上繪製年份和人口。
  • 要顯示圖形,請使用 show() 方法。

示例

from matplotlib import pyplot as plt

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

years = [1901, 1911, 1921, 1931, 1941, 1951,
        1961, 1971, 1981, 1991, 2001, 2011]
population = [237.4, 238.4, 252.09, 251.31, 278.98,
              318.66, 361.09, 439.23, 548.16,
              683.33, 846.42, 1028.74]

plt.plot(years, population, color='red', marker='o')

plt.show()

輸出

更新時間: 04-Aug-2021

806 次瀏覽

開啟你的 職業生涯

完成課程以獲取認證

開始
廣告
© . All rights reserved.