Pandas loop over groupby and plot each group

前端 未结 2 708
忘掉有多难
忘掉有多难 2021-01-28 07:09

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(         


        
相关标签:
2条回答
  • 2021-01-28 07:21

    Thanks to ImportanceOfBeingErnest, I have got the result what I wanted. Here is full tweaked version of the code, for anyone who may need it in future. I have added legend = False, without this val1 and val2 are written on top of each other and looks messy.

    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 ffffdffffd')
      plt.title(str(name), fontsize=15)
    
       plt.show()
    
    0 讨论(0)
  • 2021-01-28 07:29

    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()
    
    0 讨论(0)
提交回复
热议问题