Pandas: multiple histograms of categorical data

不打扰是莪最后的温柔 提交于 2020-01-04 17:57:06

问题


I have the following dataframe:

    Type       Cat1       Cat2       Cat3
0   A      0.384000   0.393000   0.458000
1   B      0.603337   0.381470   0.299773
2   C      0.585797   0.452570   0.317607
3   D      0.324715   0.765212   0.717755

I want to produce three histograms, one for each "Cat". Each histogram must show the values for each "Type" (e.g., the categories) as x labels on the bottom histogram.

I have been trying to do this using df.plot(kind='hist') but to no avail:

ax=df.plot(kind='hist',subplots=True,layout=(3,1),title='My title',color='orange',grid=True,legend=False)

but I see that:

  1. The x axis does not show the four types, but just a bunch of numbers
  2. The y axis ranges from 0 to 4, while it must go from 0 to 1 (these are frequencies)
  3. Only one bar is shown in each subplot

What am I doing wrong?


The wrong painting:


回答1:


IIUC:
you don't want a histogram, you just want a bar plot.

df.set_index('Type').plot.bar(subplots=True, legend=False)


adjust vertical spacing assuming import matplotlib.pyplot as plt

axes = df.set_index('Type').plot.bar(subplots=True, legend=False)
plt.subplots_adjust(hspace=0.35)



来源:https://stackoverflow.com/questions/40805910/pandas-multiple-histograms-of-categorical-data

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!