使用 Matplotlib 从 CSV 文件绘制数据
matplotlibserver side programmingprogramming更新于 2025/8/26 9:07:17
要提取 CSV 文件中特定列的数据,可以使用 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()
输出


