pandas 在groupby上循环并绘制每个组 [英] Pandas loop over groupby and plot each group

查看:286
本文介绍了 pandas 在groupby上循环并绘制每个组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图循环一个groupby对象并绘制每个组。但我有一些问题。

  df = pd.DataFrame([['item1',2000,1 ,2],['item1',2001,1,2],['item1',2002,1,2],
['item2',2000,1,2],['item2',2001 ,1,2],['item2',2002,1,2]],
列= ['mykey','year','val1','val2'])

groupped = df.groupby('mykey')
用于名称,分组中的组:
fig = plt.figure()
ax1 = fig.add_subplot(111)
组.val1.plot.line(ax = ax1,ylim = [5,20],color ='red',x = group.year)
ax1.set_ylabel('val1')
ax2 = ax1 .twinx()
group.val2.plot.line(ax = ax2,ylim = [5,20],color ='blue',x = group.year)
ax2.set_ylabel('val2 ')
plt.title(str(name),fontsize = 15);

看起来我很接近,但只是在某些地方存在一些问题。




  1. 第一个问题是groupby对象中有5个组。我想得到5个数字,但只有第一个有它的阴谋(线条)。其他人的数字是空白的,他们的正确标题,任何想法我的代码有什么问题?


  2. 如何将组列/键设置为x轴,我试过这个x = group.desiredx,但它什么也没做。 b $ b

mykey |一年| VAL1 | val2

item1 | 2000 | 5 | 34

item2 | 2001 | 45 | 34

item3 | 2002 | 34 | 34

item1 | 2000 | 22 | 65

item2 | 2001 | 34 | 54

item3 | 2002 | 12 | 54

item1 | 2000 | 23 | 54

item2 | 2001 | 34 | 34

item3 | 2002 | 21 | 21

解决方案

在y轴上绘制的值是 1 2 。您将 ylim 设置为大于 5 ylim = [5,20] 。因此,您在 1 2 中看不到值。



ylim 设置为一些合理的数字,例如 ylim = [0,3] 将允许您查看数据。

此外, group.val1.plot 会根据其索引绘制数据,因为没有year位于 group.val1 中。取而代之的是val1作为 y 的值。

  import pandas as pd 
import matplotlib.pyplot as plt

df = pd.DataFrame([['item1',2000,1,2] ,''item1',2001,1,2],['item1',2002,1,2],
['item2',2000,1,2],['item2',2001,1, 2],['item2',2002,1,2]],
列= ['mykey','year','val1','val2'])

分组= df.groupby('mykey')
用于名称,分组中的组:
fig = plt.figure()
ax1 = fig.add_subplot(111)
group.plot。 line(ax = ax1,ylim = [0,3],color ='red',x =year,y =val1)
ax1.set_ylabel('val1')
ax2 = ax1.twinx()
group.plot.line(ax = ax2,ylim = [0,3],color ='blue',x =year,y =val2)
ax2 .set_ylabel('val2')
plt.title(str(name),fontsize = 15)
$ b plt.show()
pre>

I am trying to loop over a groupby object and plot each group. But I am having some issues. Can someone please tell me where I am going wrong?

df = pd.DataFrame([['item1',2000,1, 2], ['item1',2001,1, 2], ['item1',2002,1, 2], 
              ['item2',2000,1, 2], ['item2',2001,1, 2], ['item2',2002,1, 2]],
              columns=['mykey', 'year','val1','val2'])

grouped = df.groupby('mykey')
for name,group in grouped:
  fig = plt.figure()
  ax1 = fig.add_subplot(111)
  group.val1.plot.line(ax=ax1, ylim=[5,20], color='red',x=group.year)
  ax1.set_ylabel('val1')
  ax2 = ax1.twinx()
  group.val2.plot.line(ax=ax2, ylim=[5,20], color='blue' ,x=group.year)
  ax2.set_ylabel('val2')
  plt.title(str(name), fontsize=15);

It seems I'm close, but just there are some issues some where.

  1. First issue is that there are 5 groups in the groupby object. I get the 5 figures as I want, but only first one has the plots(lines) on it. Others figures are blank with the correct title on them, any idea what is wrong with my code?
  2. How can I set a group column / key as the x axis, I have tried this x=group.desiredx but it doesn't do anything.

mykey| year| val1| val2
item1| 2000| 5| 34
item2| 2001| 45| 34
item3| 2002| 34| 34
item1| 2000| 22| 65
item2| 2001| 34| 54
item3| 2002| 12| 54
item1| 2000| 23| 54
item2| 2001| 34| 34
item3| 2002| 21| 21

解决方案

The values to plot on the y axis are 1 and 2. You set the ylim to something bigger than 5, ylim=[5,20]. Hence you do not see the values at 1 and 2.

Setting the ylim to some reasonable numbers, e.g. ylim=[0,3] will allow you to see the data.

Also, group.val1.plot will plot the data against its index, because there is no "year" in group.val1. Instead take "val1" as the y value.

import pandas as pd
import matplotlib.pyplot as plt

df = pd.DataFrame([['item1',2000,1, 2], ['item1',2001,1, 2], ['item1',2002,1, 2], 
              ['item2',2000,1, 2], ['item2',2001,1, 2], ['item2',2002,1, 2]],
              columns=['mykey', 'year','val1','val2'])

grouped = df.groupby('mykey')
for name,group in grouped:
  fig = plt.figure()
  ax1 = fig.add_subplot(111)
  group.plot.line(ax=ax1, ylim=[0,3], color='red',x="year",y="val1")
  ax1.set_ylabel('val1')
  ax2 = ax1.twinx()
  group.plot.line(ax=ax2, ylim=[0,3], color='blue',x="year",y="val2")
  ax2.set_ylabel('val2')
  plt.title(str(name), fontsize=15)

plt.show()

这篇关于 pandas 在groupby上循环并绘制每个组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆