[英]Plot histogram with index values on x axis and frequencies of each column value on y axis
Based on the dataframe 基于数据框
import pandas as pd
df = pd.DataFrame({'cat1':[100,0,0,5],'cat2':[5,20,50,0]})
df
cat1 cat2
0 100 5
1 0 20
2 0 50
3 5 0
I want to plot a histogram such that the x axis represent the index values 0 to 3 and the bars for each index value show the distribution of the column values cat1 and cat2. 我想绘制一个直方图,以便x轴表示索引值0到3,每个索引值的条形图显示列值cat1和cat2的分布。
df.plot.hist(alpha=0.5)
plots the index values on the y and the category values on the x axis: 在y上绘制索引值,在x轴上绘制类别值:
And 和
df.transpose().plot.hist(alpha=0.5)
results in something I don't even fully understand: 结果导致我什至不完全了解:
What I actually want is a bar for each index value, that illustrates the values in the columns - eg for index 0, there should be a bar in cat1 color stretching to 100 on the y axis, containing another bar in cat2 color stretching to 5. 我真正想要的是每个索引值的条,以说明列中的值-例如,对于索引0,cat1颜色的条应在y轴上延伸到100,包含cat2颜色的另一条条应延伸到5 。
How can I achieve that? 我该如何实现?
you can use the pandas viz functions : 您可以使用pandas viz函数:
df.plot.bar(stacked=True) df.plot.bar(堆叠=真)
Try this, it's close that what you want: 试试这个,接近您想要的是:
import matplotlib.pyplot as plt
df.plot.barh()
plt.show()
Update thanks to SpghttCd: 感谢SpghttCd更新:
import matplotlib.pyplot as plt
df.plot.bar()
plt.show(
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.