问题
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:
- The x axis does not show the four types, but just a bunch of numbers
- The y axis ranges from 0 to 4, while it must go from 0 to 1 (these are frequencies)
- 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