如何使用迭代方法绘制线路图并在其中分配适当的标签

发布于 2025-02-13 10:10:02 字数 1608 浏览 0 评论 0原文

这是我正在研究的数据集,

Update  Pb95    Pb98    diesel  heating oil
0   6/2/2022    7519    8311    7172    5582
1   6/1/2022    7406    8194    6912    5433
2   5/31/2022   7213    7950    6754    5394
3   5/28/2022   7129    7864    6711    5360
4   5/27/2022   7076    7798    6704    5366
5   5/26/2022   6895    7504    6502    5182
6   5/25/2022   6714    7306    6421    5130
7   5/24/2022   6770    7358    6405    5153
8   5/21/2022   6822    7421    6457    5216
9   5/20/2022   6826    7430    6523    5281

我正在尝试创建一些优雅的图表,以代表时间与价格变化之间的关系。 现在,我已经使用以下代码对单个图进行了,

    import matplotlib.pyplot as plt
    
    plt.plot(df['Update'], df['Pb95'], label='sales', linewidth=3)
    plt.rcParams["figure.figsize"] = (8,8)
    
    #add title and axis labels
    plt.title('Fuels price by Date')
    plt.xlabel('Date')
    plt.ylabel('Price (PLN)')
    
    
    #add legend
    plt.legend()
    
    #display plot
    plt.show() 

我希望通过使用循环来绘制不同图中每个燃料的线条。我已经使用了以下代码:

df_columns = df.iloc[:,1:6]
for i in df_columns: 
    plt.plot(df['Update'], df[i], label='sales', linewidth=3)
    plt.title('Fuels price by Date')
    plt.xlabel('Date')
    plt.ylabel('Price (PLN)')


plt.legend()    
plt.show() 

我获得了一个唯一的图

”在此处输入图像描述”

但是现在我想,我想在哪里分配每条线的燃料专有名称。是否有人可以帮助我想要的两种方式之一(分开或 在同一情节中)我想代表这种关系吗?

最重要的是,我想学习如何通过迭代方法分配标签(一种或另一种绘制图形的方式)。

谢谢

This is the dataset I am working on

Update  Pb95    Pb98    diesel  heating oil
0   6/2/2022    7519    8311    7172    5582
1   6/1/2022    7406    8194    6912    5433
2   5/31/2022   7213    7950    6754    5394
3   5/28/2022   7129    7864    6711    5360
4   5/27/2022   7076    7798    6704    5366
5   5/26/2022   6895    7504    6502    5182
6   5/25/2022   6714    7306    6421    5130
7   5/24/2022   6770    7358    6405    5153
8   5/21/2022   6822    7421    6457    5216
9   5/20/2022   6826    7430    6523    5281

I am attempting to create some elegant graphs in order to represent the relationship between time vs price change. I have use the following code for a single graph

    import matplotlib.pyplot as plt
    
    plt.plot(df['Update'], df['Pb95'], label='sales', linewidth=3)
    plt.rcParams["figure.figsize"] = (8,8)
    
    #add title and axis labels
    plt.title('Fuels price by Date')
    plt.xlabel('Date')
    plt.ylabel('Price (PLN)')
    
    
    #add legend
    plt.legend()
    
    #display plot
    plt.show() 

Now I would have liked to plot the lines for each fuel in different plots by using a for a loop. I have used the following code:

df_columns = df.iloc[:,1:6]
for i in df_columns: 
    plt.plot(df['Update'], df[i], label='sales', linewidth=3)
    plt.title('Fuels price by Date')
    plt.xlabel('Date')
    plt.ylabel('Price (PLN)')


plt.legend()    
plt.show() 

I have obtained a unique plot

enter image description here

but now I would like, where I would like to assign the proper name of fuel to each of the lines. Is there anyone that could help with one of the two ways I would like (separate or
in the same plot) I would like to represent this relationship?

I would like above all to learn how to assign labels (for one or the other way to plot a graph) by an iterated method.

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

允世 2025-02-20 10:10:02

您可以通过稍作修改代码来做到这一点。我正在使用您提供的数据框。

for column in df.columns[1:]: 
    plt.plot(df['Update'], df[column], label=column, linewidth=3)
    plt.title('Fuels price by Date')
    plt.xlabel('Date')
    plt.ylabel('Price (PLN)')


plt.legend()    
plt.show() 

给出这个:

“在此处输入图像描述”

另外,您可以通过Pandas自己的情节方法来执行此操作。

df.plot.line(x='Update', y=['Pb95', 'Pb98', 'diesel', 'heating oil'])

给出这个:

“在此处输入图像描述”

You can do that by modifying your code slightly. I'm using the dataframe exerpt you provided.

for column in df.columns[1:]: 
    plt.plot(df['Update'], df[column], label=column, linewidth=3)
    plt.title('Fuels price by Date')
    plt.xlabel('Date')
    plt.ylabel('Price (PLN)')


plt.legend()    
plt.show() 

Gives this:

enter image description here

Alternatively, you can do this by pandas' own plot method.

df.plot.line(x='Update', y=['Pb95', 'Pb98', 'diesel', 'heating oil'])

Gives this:

enter image description here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文