Pandas groupby object in legend on plot

后端 未结 1 987
感动是毒
感动是毒 2021-02-13 20:20

I am trying to plot a pandas groupby object using the code fil.groupby(\'imei\').plot(x=[\'time\'],y = [\'battery\'],ax=ax, title = str(i))

Th

1条回答
  •  迷失自我
    2021-02-13 20:44

    Slightly tidied data:

        date         time             imei      battery_raw
    0 2016-09-30 07:01:23  862117020146766       42208
    1 2016-09-30 07:06:23  862117020146766        42213
    2 2016-09-30 07:11:23  862117020146766        42151
    3 2016-09-30 07:16:23 862117995146745       42263
    4 2016-09-30 07:21:23  862117995146745       42293
    

    Complete example code:

    import matplotlib.pyplot as plt
    
    fil = pd.read_csv('imei.csv', sep=r'\s*', engine='python')
    fig, ax = plt.subplots(figsize=(18,6))
    
    for name, group in fil.groupby('imei'):
        group.plot(x=pd.to_datetime(group['time']), y='battery_raw', ax=ax, label=name)
    
    plt.show()
    

    The x-values have to be converted to datetime for plotting to come out right, as usual. You could do that in the dataframe, too.

    Result, labeled by imei:

    (NOTE: edited to get rid of an oddity I tripped over the first time. If you pass a list as the y argument to group.plot, the list IDs will be used as the line labels, presumably as a handy default for when you're plotting several dependent variables at once.

    #for name, group in fil.groupby('imei'):
    #    group.plot(x=['time'], y=['battery_raw'], ax=ax, label=name)
    

    )

    0 讨论(0)
提交回复
热议问题