如何在 "groupby" 中定义直方图的颜色?

How to define colors for histogram in "groupby"?

我需要为下一个示例定义客户颜色('F' 和 'M' 的 2 种颜色):

d = {'gender' : Series(['M', 'F', 'F', 'F', 'M']),'year' : Series([1900, 1910, 1920, 1920, 1920])}
df = DataFrame(d)

grouped = df.groupby('gender').year
grouped.plot(kind='hist',legend=True)

如果您不需要 groupby(在这种情况下我看不出它对您有任何好处),那么您可以轻松设置颜色:

ax1 = plt.subplot(111)
df[df['gender']=='M'].hist(ax=ax1, color='red', label='M')
df[df['gender']=='F'].hist(ax=ax1, color='blue', label='F')
ax1.legend(loc='best')