如何为 Pandas 生成的分组直方图添加图例和标题
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/30919063/
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
How to add legends and title to grouped histograms generated by Pandas
提问by vumaasha
I am trying to plot a histogram of multiple attributes grouped by another attributes, all of them in a dataframe.
我正在尝试绘制由另一个属性分组的多个属性的直方图,所有这些属性都在一个数据框中。
with the help of this question, I am able to set title for the plot. Is there an easy way to switch on legend for each subplot.
在这个问题的帮助下,我可以为情节设置标题。是否有一种简单的方法可以为每个子图打开图例。
Here is my code
这是我的代码
import numpy as np
from numpy.random import randn,randint
import pandas as pd
from pandas import DataFrame
import pylab as pl
x=DataFrame(randn(100).reshape(20,5),columns=list('abcde'))
x['new']=pd.Series(randint(0,3,10))
x.hist(by='new')
pl.suptitle('hist by new')


回答by LondonRob
You can almostget what you want by doing:
你几乎可以通过以下方式得到你想要的:
g.plot(kind='bar')
but it produces one plot per group (and doesn't name the plots after the groups so it's a bit useless IMO.)
但它为每组生成一个图(并且不会在组之后命名图,因此 IMO 有点无用。)
Here's something which looks rather beautiful, but does involve quite a lot of "manual" matplotlibwork, which everyone wants to avoid, but no one can:
这里有一些看起来很漂亮,但确实涉及很多“手动”matplotlib工作的东西,每个人都想避免,但没有人可以:
import numpy.random as rnd
import pandas as pd
import matplotlib.pyplot as plt
from matplotlib import cm
x = pd.DataFrame(rnd.randn(100).reshape(20, 5), columns=list('abcde'))
group_col = 'groups'
groups = ['foo', 'bar', 'baz']
x[group_col] = pd.Series(rnd.choice(groups, len(x)))
g = x.groupby(group_col)
num_groups = g.ngroups
fig, axes = plt.subplots(num_groups)
for i, (k, group) in enumerate(g):
ax = axes[i]
ax.set_title(k)
group = group[[c for c in group.columns if c != group_col]]
num_columns = len(group.columns)
colours = cm.Spectral([float(x) / num_columns for x in range(num_columns)])
ax.hist(group.values, 5, histtype='bar',
label=list(group.columns), color=colours,
linewidth=1, edgecolor='white')
ax.legend()
plt.show()
Which I think gives you what you want:

我认为这给了你你想要的:

Update更新为了回应评论(并且因为这个答案已经有几年了),我试图将这个答案剥离到最简单的骨头。那里may可现在是标注的地块的方式
groupbygroupby对象,但我不知道它。Here's the simplest possible way to do this:
这是执行此操作的最简单方法:
axes = g.plot(kind='hist')
for i, (groupname, group) in enumerate(g):
axes[i].set_title(groupname)

