Python:循环绘制图表(多列)

时间:2018-07-24 07:05:34

标签: python loops matplotlib plot

请找到参考问题 Reference question

我想知道如何使用此相似代码使用csv文件中的数据绘制图形。 首先,我想将第一列固定为x轴,并将数据框中的其他列称为不同的图例。 类似于图片中的此数据 enter image description here

1 个答案:

答案 0 :(得分:1)

我不确定这是否是您要的内容。我的代码将Col1保留为x轴,其余列保留为'y'或'legends'。而且您似乎想使用for循环来创建图表。让我知道这是否不是您要的。

输入:

import pandas as pd
import matplotlib.pyplot as plt

columns = df.columns
y = df.columns[1:]

for col in y:
    plt.plot(df['Col1'], df[col])
    plt.xlabel('x')
    plt.ylabel('y')
    plt.legend(y)

输出: matplotlib with different legends using for loop

希望这会有所帮助。

相关问题