使用 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.