【问题标题】:How to define colors for histogram in "groupby"?如何在“groupby”中定义直方图的颜色?
【发布时间】:2015-07-04 19:31:00
【问题描述】:

我需要为下一个示例定义客户颜色(“F”和“M”两种颜色):

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)

【问题讨论】:

    标签: python pandas colors histogram


    【解决方案1】:

    如果您不需要 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')
    

    【讨论】:

      猜你喜欢
      • 2011-02-02
      • 1970-01-01
      • 2013-08-26
      • 2018-02-09
      • 1970-01-01
      • 2015-02-03
      • 2019-02-03
      • 1970-01-01
      • 2015-02-13
      相关资源
      最近更新 更多