利用 Matplotlib 繪製 CSV 檔案中的資料
要將 CSV 檔案的特定列提取到 Python 中的列表中,我們可以使用 Pandas read_csv() 方法。
步驟
- 列出需要提取的列。
- 使用 read_csv() 方法將 CSV 檔案資料提取到資料框中。
- 列印提取的資料。
- 使用 plot() 方法繪製資料框。
- 要顯示圖形,請使用 show() 方法。
示例
import pandas as pd from matplotlib import pyplot as plt plt.rcParams["figure.figsize"] = [7.00, 3.50] plt.rcParams["figure.autolayout"] = True columns = ["Name", "Marks"] df = pd.read_csv("input.csv", usecols=columns) print("Contents in csv file:
", df) plt.plot(df.Name, df.Marks) plt.show()
輸出
廣告