创建一个循环以绘制图形

时间:2020-10-13 10:19:42

标签: python pandas

我有一个这样的数据框:

ID   Product    Yield     Time
3    XC         0.88      2020-01-02
4    XB         1.0       2020-01-03
5    XC         0.2       2020-01-04
...

我想做的是绘制我的270种产品中每一种的产量随时间变化的图。 有没有办法做循环?

我尝试过:

for i in df['Product']:

 g=i.plot.scatter(x='Time',y='Yield',legend=None)
 hfont = {'fontname':'Arial'}
 g.set_xlabel("Time",fontweight='bold',**hfont,size=13)
 plt.xticks(rotation=90)
 g.set_ylabel("Yield",fontweight='bold',**hfont, size=13)
 plt.show

但是它给了我错误:'str'对象没有属性'plot'

感谢您的帮助!

1 个答案:

答案 0 :(得分:0)

您需要遍历各行:

for index, row in df.iterrows():
   g=i.plot.scatter(x=row['Time'],y=row['Yield'],legend=None)
   hfont = {'fontname':'Arial'}
   g.set_xlabel("Time",fontweight='bold',**hfont,size=13)
   plt.xticks(rotation=90)
   g.set_ylabel("Yield",fontweight='bold',**hfont, size=13)
   plt.show

但是每个图只有一个点...