繁体   English   中英

如何按数字顺序而不是基于 y 轴值绘制熊猫条形图的 x 轴

[英]How to plot the x-axis of a pandas bar plot in numerical order rather than based on y-axis value

我有一个看起来像这样的熊猫 df

,Difference Max
0,12.0
1,1.0
2,-7.0
3,-5.0
4,2.0
5,3.0
6,10.0
7,0.0
8,0.0
9,3.0
10,3.0

从这个 df 我想绘制一个条形图/直方图,显示“差异最大值”列中每个值的出现次数。 我目前有这个条形图在此处输入图片说明

使用以下代码:

df_maxdif['Difference Max'].value_counts().plot(kind='bar')

但我希望 x 轴按数字顺序排列,从最左侧的 -21 到最右侧的 10。 我试过简单地绘制 kind = 'hist' 但它不是我想要的。

value_counts()sort_index

df_maxdif['Difference Max'].value_counts().sort_index().plot(kind='bar')

或使用plt.hist

plt.hist(df['Difference Max'], bins=np.arange(-21,10))

输出(对于plt.hist ):

在此处输入图片说明

暂无
暂无

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

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