情节图例中的 Pandas groupby 对象

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/39902522/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 02:09:23  来源:igfitidea点击:

Pandas groupby object in legend on plot

pythonpandasmatplotlibplot

提问by hselbie

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

我正在尝试groupby使用代码绘制Pandas对象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 groupbyobject, 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.

问题是情节图例['battery']列为图例值。鉴于它为groupby对象中的每个项目绘制一条线,在图例中绘制这些值更有意义。但是我不知道该怎么做。任何帮助,将不胜感激。

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

在此处输入图片说明

回答by cphlewis

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.

像往常一样,x 值必须转换为日期时间才能正确绘制。您也可以在数据框中执行此操作。

Result, labeled by imei:

结果,用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 yargument 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.

在此处输入图片说明(注意:编辑以摆脱我第一次绊倒的奇怪之处。如果您将列表作为y参数传递给group.plot,则列表 ID 将用作线标签,大概是您绘制多个时的方便默认值因变量一次。

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

)

)