CF84 CF84 - 13 days ago 9
Python Question

Pandas: multiple histograms of categorical data

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:
enter image description here

Answer

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

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

enter image description here