gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-05 02:49:43 25 4
gpt4 key购买 nike

我有一个看起来像这样的 pandas 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 我想绘制一个条形图/直方图,显示“最大差异”列中每个值的出现次数。我目前有这个条形图 enter image description here

使用以下代码:

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):

enter image description here

关于python - 如何按数字顺序而不是基于 y 轴值绘制 pandas 条形图的 x 轴,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63942111/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com