Python 绘制 Pandas DataSeries.GroupBy

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16376159/
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-08-18 22:25:39  来源:igfitidea点击:

Plotting a Pandas DataSeries.GroupBy

pythonpython-2.7numpyscipypandas

提问by Nyxynyx

I am new to python and pandas, and have the following DataFrame.

我是 python 和 pandas 的新手,并且有以下DataFrame.

How can I plot the DataFramewhere each ModelIDis a separate plot, saledateis the x-axis and MeanToDateis the y-axis?

如何绘制DataFrame每个ModelID单独的图,saledatex 轴和MeanToDatey 轴?

Attempt

试图

data[40:76].groupby('ModelID').plot()

enter image description here

在此处输入图片说明

DataFrame

数据框

enter image description here

在此处输入图片说明

采纳答案by joris

You can make the plots by looping over the groups from groupby:

您可以通过循环以下组来绘制图groupby

import matplotlib.pyplot as plt

for title, group in df.groupby('ModelID'):
    group.plot(x='saleDate', y='MeanToDate', title=title)

See for more information on plotting with pandas dataframes:
http://pandas.pydata.org/pandas-docs/stable/visualization.html
and for looping over a groupby-object:
http://pandas.pydata.org/pandas-docs/stable/groupby.html#iterating-through-groups

有关使用 Pandas 数据框绘图的更多信息,请参阅:
http: //pandas.pydata.org/pandas-docs/stable/visualization.html
和循环遍历 groupby-object:http:
//pandas.pydata.org/pandas- docs/stable/groupby.html#iterating-through-groups

回答by chbrown

Example with aggregation:

聚合示例:

I wanted to do something like the following, if pandas had a colour aesthetic like ggplot:

如果熊猫有像 ggplot 这样的颜色美感,我想做以下事情:

aggregated = df.groupby(['model', 'training_examples']).aggregate(np.mean)
aggregated.plot(x='training_examples', y='accuracy', label='model')

(columns: model is a string, training_examples is an integer, accuracy is a decimal)

(列:model 是一个字符串,training_examples 是一个整数,accuracy 是一个小数)

But that just produces a mess.

但这只会造成混乱。

Thanks to joris's answer, I ended up with:

多亏了 joris 的回答,我得到了:

for index, group in df.groupby(['model']):
    group_agg = group.groupby(['training_examples']).aggregate(np.mean)
    group_agg.plot(y='accuracy', label=index)

I found that title=was just replacing the single title of the plot on each loop iteration, but label=does what you'd expect -- afterrunning plt.legend(), of course.

我发现这title=只是在每次循环迭代中替换了绘图的单个标题,但label=确实符合您的期望——当然,运行之后plt.legend()