【问题标题】:Pandas loop over groupby and plot each groupPandas 循环遍历 groupby 并绘制每个组
【发布时间】:2018-05-13 19:16:32
【问题描述】:

我正在尝试遍历 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]],
              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);

看来我已经接近了,但只是在某些地方存在一些问题。

  1. 第一个问题是 groupby 对象中有 5 个组。我得到了我想要的 5 个数字,但只有第一个有图(线)。其他数字是空白的,上面有正确的标题,知道我的代码有什么问题吗?
  2. 如何将组列/键设置为 x 轴,我试过这个 x=group.desiredx 但它没有做任何事情。

我的钥匙|年份| val1| val2
项目1| 2000| 5| 34
项目2| 2001| 45| 34
项目3| 2002| 34| 34
项目1| 2000| 22| 65
项目2| 2001| 34| 54
项目3| 2002| 12| 54
项目1| 2000| 23| 54
项目2| 2001| 34| 34
项目3| 2002| 21| 21

【问题讨论】:

标签: pandas matplotlib plot group-by


【解决方案1】:

要在 y 轴上绘制的值是 12。您将ylim 设置为比5ylim=[5,20] 更大的值。因此,您看不到 12 处的值。

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

此外,group.val1.plot 将根据其索引绘制数据,因为group.val1 中没有"year"。而是将"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]],
              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()

【讨论】:

  • 谢谢,x 仍然无法正常工作。我在 x 轴上得到 0, 0.25,0.50 .....。这些图是 y 轴图现在正在工作。
  • 现在有年份,但后面有 .00 、 .25 、 .50 .... 并且每年重复 3 次。所以: 2000.00 , 2000.25 , 2000.50, 2001.00 等等
  • 是的,这是意料之中的。你会发现很多关于格式化 x 轴标签的问题和答案。
  • 哇,这太接近了。我已经为此工作了好几天。您能否为年份格式问题提出解决方案?我也在寻找选项。
  • 谷歌搜索“matplotlib 格式 xaxis 整数”或“matplotlib 格式日期为年份”或类似内容。
【解决方案2】:

感谢 ImportanceOfBeingErnest,我得到了我想要的结果。这是代码的完整调整版本,供将来可能需要它的任何人使用。我添加了 legend = False,没有这个 val1 和 val2 写在彼此之上,看起来很乱。

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,3, 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", legend 
  = False, xticks = [2000,2001,2002])
  ax1.set_ylabel('val1  ssssssss')
  ax2 = ax1.twinx()
  group.plot.line(ax=ax2, ylim=[0,3], color='blue',x="year",y="val2", legend 
  = False, xticks = [2000,2001,2002])
  ax2.set_ylabel('val2 dddddd')
  plt.title(str(name), fontsize=15)

   plt.show()

【讨论】:

    猜你喜欢
    • 2019-06-12
    • 2019-10-18
    • 2019-06-12
    • 2014-04-26
    • 2014-10-17
    • 2021-12-28
    • 2015-08-03
    • 2017-10-02
    • 1970-01-01
    相关资源
    最近更新 更多