Search code examples
pythonpandasmatplotlibplot

Pandas groupby object in legend on plot


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))

The problem is the plot legend lists ['battery']as the legend value. Given it's drawing a line for each item in the groupby object, it makes more sense to plot those values in the legend instead. However I'm not sure how to do that. Any help would be appreciated.

Data

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

Full code

for i in entity:
    fil = df[(df['entity_id']==i)]
    fig, ax = plt.subplots(figsize=(18,6))
    fil.groupby('imei').plot(x=['time'],y = ['battery'],ax=ax, title = str(i))  
    plt.legend(fil.imei)
    plt.show()

Current plot

enter image description here


Solution

  • 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:

    enter image description here (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)
    

    )