gpt4 book ai didi

python - matplotlib:使用 matplotlib 或 seaborn 显示每个日期的所有产品价格的折线图

转载 作者:行者123 更新时间:2023-12-01 07:50:35 24 4
gpt4 key购买 nike

如何使用折线图在一张图表中显示每个日期的所有产品价格?

数据框有三列,如下所示:

+---------+-------+----------+
| product | price | date |
+---------+-------+----------+
| pen | 1 | 20190101 |
| apple | 2 | 20190101 |
| book | 3 | 20190101 |
| cup | 4 | 20190101 |
| pen | 0.8 | 20190102 |
| apple | 2.2 | 20190102 |
| book | 3.1 | 20190102 |
| cup | 3.5 | 20190102 |
| pen | 0.6 | 20190103 |
| apple | 2.6 | 20190103 |
| book | 2.1 | 20190103 |
| cup | 4.3 | 20190103 |
+---------+-------+----------+

enter image description here

最佳答案

假设您的数据框:

import pandas as pd

df = pd.DataFrame({'product':['pen','apple','book','cup','pen','apple','book','cup','pen','apple','book','cup',],
'price':[1,2,3,4,0.8,2.2,3.1,3.5,.6,2.6,2.1,4.3],
'date':['20190101','20190101','20190101','20190101','20190102','20190102','20190102','20190102','20190103','20190103','20190103','20190103']})

您可以使用pandas.DataFrame.groupby对产品进行分组并 matplotlib绘制每种产品的价格:

import matplotlib.pyplot as plt

for i in df.groupby('product'):
plt.plot(i[1].date,i[1].price,'-o',label=i[0])

plt.legend(loc='upper center',
bbox_to_anchor=(0.5, -0.2),
fancybox=True,
shadow=True,
ncol=4)
plt.xlabel('date')
plt.ylabel('price')
plt.show()

enter image description here

关于python - matplotlib:使用 matplotlib 或 seaborn 显示每个日期的所有产品价格的折线图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56233711/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com