简体   繁体   English

如何在Python Pandas中显示分组直方图的标题?

[英]How to display the titles of a grouped histogram in Python Pandas?

Here is an example dataframe: 这是一个示例数据框:

print(df.head(5))


     Days     State
0    1.2     IN
1    3.4     IN
2   18.8     CA
3   16.5     CA
4    0.2     IL

I have a dataframe that I am grouping to display histograms as such: 我有一个要分组的数据框,以显示如下直方图:

  df.groupby('State').hist(bins=10, label='Dept')
  plt.show()

Lets say there are only 3 States: IN, IL, and CA. 可以说只有3种状态:IN,IL和CA。

This displays the 3 histograms, but the titles are not displaying the states, but rather just say 'Country' for each one. 这将显示3个直方图,但标题未显示状态,而只是对每个国家说“国家”。 How can I set this up to have the states listed as the titles? 如何设置以将州列为标题?

this kinda of accomplishes what I am after: 这种成就实现了我的追求:

df.hist(by=df['State'], bins=30)
plt.show()

But I don't like how it puts the histograms on a automatic grid. 但是我不喜欢它如何将直方图放在自动网格上。 Is there anyway to just show them separately, on top of each other like it does in the top code? 无论如何,是否像顶部代码中那样将它们单独显示在彼此之上?

To rephrase my question, when there are 9 states in the data, the df.hist(by=df['State'], bins=30) will have 9 histograms in a 3x3 grid, 3 histograms wide, 3 tall. 换一个问题,当数据中有9个状态时,df.hist(by = df ['State'],bins = 30)将在3x3网格中具有9个直方图,宽3个直方图,高3个。

This is an issue because it makes them each too small and labels overlap. 这是一个问题,因为这会使它们每个都太小并且标签重叠。 I'm not sure how to adjust the layout to make the histograms larger and in a single column with a scroll bar in jupyter for example. 我不确定如何调整布局以使直方图更大,例如在jupyter中带有滚动条的单列中。

Use layout and figsize : 使用layoutfigsize

df.hist(by=df['State'], bins=30, layout=(3,1), figsize=(5,15))

Output: 输出:

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM