如何从excel中绘制包含多条线的折线图

俊荣潘

我想用这么多线绘制折线图。我发现以下基本推荐来绘制 1 行。

import matplotlib.pyplot as plt
location_3='C:\\Users\\Poon\\Downloads\\20211014_SBS_BEMS\\1043 final.csv'
csvfiles_3=glob.glob(location_3)
df7=pd.DataFrame()
for file_new_3 in csvfiles_3: 
    df7=pd.read_csv(file_new_3,skiprows=[0])
    print(df7)
    ax=plt.plot(df7.iloc[:, 1],df7.iloc[:, 2],'g')

但我需要绘制超过 100 条线。有更快的方法吗?这是我在excel中的一些数据集。列比我显示的要多。

19/5/2019 0:00  237 64.93   82.35   16.15   46.88   18.11   148.3115
19/5/2019 1:00  237.2667    64.93   82.35   16.15   46.88   18.11   148.3295
19/5/2019 2:00  238 64.93   82.35   16.15   46.88   18.11   148.348
19/5/2019 3:00  238 64.93   82.35   16.15   46.88   18.11   148.3672
19/5/2019 4:00  238 64.93   82.35   16.15   46.88   18.11   148.4432
19/5/2019 5:00  238 64.93   82.35   16.15   46.88   18.11   148.7437
19/5/2019 6:00  238 64.93   82.35   16.15   46.88   18.11   149.064
19/5/2019 7:00  238 64.93   82.35   16.15   46.88   18.11   149.3825
19/5/2019 8:00  238 64.93   82.35   16.15   46.88   18.11   149.7037
19/5/2019 9:00  238.7833    64.93   82.35   16.15   46.88   18.11   150.0222
19/5/2019 10:00 239 64.93   82.35   16.15   46.88   18.11   150.3422
19/5/2019 11:00 239 64.93   82.35   16.15   46.88   18.11   150.6617
19/5/2019 12:00 239 64.93   82.35   16.15   46.88   18.11   150.9813
19/5/2019 13:00 239 64.93   82.35   16.15   46.88   18.11   151.3018
19/5/2019 14:00 239 64.93   82.35   16.15   46.88   18.11   151.6213
19/5/2019 15:00 239 64.93   82.35   16.15   46.88   18.11   151.94
19/5/2019 16:00 239.4167    64.93   82.35   16.15   46.88   18.11   152.2615
19/5/2019 17:00 240 64.93   82.35   16.15   46.88   18.11   152.5812
19/5/2019 18:00 240 64.93   82.35   16.15   46.88   18.11   152.8295
19/5/2019 19:00 240 64.93   82.35   16.15   46.88   18.11   152.8608
19/5/2019 20:00 240 64.93   82.35   16.15   46.88   18.11   152.8797
19/5/2019 21:00 240 64.93   82.35   16.15   46.88   18.11   152.8962
19/5/2019 22:00 240 64.93   82.35   16.15   46.88   18.11   152.9148
19/5/2019 23:00 240.05  64.93   82.35   16.15   46.88   18.11   152.9337
20/5/2019 0:00  241 64.93   82.35   16.15   46.88   18.11   152.9508

所以我想要的是 x 轴是日期,而 Y 轴是第 2 列,然后是 3,4....直到最后。

此外,x 轴需要减少绘图刻度的数量。现在有太多的情节滴答声。我怎样才能将它们减少到“仅一年”?例如,应该只有 2019,2020,2021,2022。

非常感谢你们的帮助。

每个

如果要自动生成行,可以使用 for 循环省略 xaxis 列,如下所示(添加了清除示例数据,您仍然可以从文件中读取):

import matplotlib.pyplot as plt
import matplotlib.dates as mdates

data = {
  "date": ['19/5/2019 0:00', '19/5/2019 1:00', '19/5/2020 2:00', '20/5/2021 3:00', '21/5/2022 1:00'],
  "colA": [237, 237, 238, 237, 240],
  "colB": [64.93, 64.94, 64.95, 65.52, 65.32],
  "colC": [644.93, 644.94, 644.95, 654.52, 654.32]
}

df = pd.DataFrame(data)

df.date=pd.to_datetime(df.date)

plt.gca().xaxis.set_major_formatter(mdates.DateFormatter('%Y'))
plt.gca().xaxis.set_major_locator(mdates.YearLocator())

for col in df.columns:
    if not col == 'date':
        plt.plot(df.date, df[col], label=col) 
        
plt.legend()
plt.show()

结果:

在此处输入图像描述

额外说明:您可以将基数添加到 YearLocator 以更改年份的频率,例如,每 5 年将是:

plt.gca().xaxis.set_major_locator(mdates.YearLocator(base=5))

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章