使用 matplotlib 從 .CSV 檔案繪製多行圖


要使用 matplotlib 從 .CSV 檔案繪製多行圖,我們可以採取以下步驟 −

  • 設定影像大小並調整子圖之間的間距和子圖周圍的間距。
  • 建立列列表,以從 .CSV 檔案獲取資料。確保名稱與 .CSV 檔案中使用的列名稱相匹配。
  • 從 .CSV 檔案中讀取資料。
  • 使用 df.plot() 方法繪製線條。
  • 若要顯示圖形,請使用 show() 方法。

示例

import pandas as pd
from matplotlib import pyplot as plt

# Set the figure size
plt.rcParams["figure.figsize"] = [7.00, 3.50]
plt.rcParams["figure.autolayout"] = True

# Make a list of columns
columns = ['mpg', 'displ', 'hp', 'weight']

# Read a CSV file
df = pd.read_csv("auto-mpg.csv", usecols=columns)

# Plot the lines
df.plot()

plt.show()

輸出

它將產生以下輸出

更新於:2021 年 9 月 22 日

12K+ 瀏覽

開啟你的職業生涯

透過完成課程獲取認證

開始
廣告
© . All rights reserved.