如何使用迭代方法绘制线路图并在其中分配适当的标签
这是我正在研究的数据集,
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
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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以通过稍作修改代码来做到这一点。我正在使用您提供的数据框。
给出这个:
另外,您可以通过Pandas自己的情节方法来执行此操作。
给出这个:
You can do that by modifying your code slightly. I'm using the dataframe exerpt you provided.
Gives this:
Alternatively, you can do this by pandas' own plot method.
Gives this: