Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/282.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 有没有办法改善切割产生的桶的标签?_Python_Python 3.x_Pandas_Matplotlib - Fatal编程技术网

Python 有没有办法改善切割产生的桶的标签?

Python 有没有办法改善切割产生的桶的标签?,python,python-3.x,pandas,matplotlib,Python,Python 3.x,Pandas,Matplotlib,我使用了pd.cut来存储数据,但当我打印数据时,标签看起来很糟糕。有没有办法改变这种状况 df["rel"].plot(kind="bar", ax=ax[0, 0]) ax[0, 0].set(xlabel="Weight Bucket", ylabel="Relative Frequency (%)") ax[0, 0].tick_params(axis="x", labelrotation

我使用了
pd.cut
来存储数据,但当我打印数据时,标签看起来很糟糕。有没有办法改变这种状况

df["rel"].plot(kind="bar", ax=ax[0, 0])
ax[0, 0].set(xlabel="Weight Bucket", ylabel="Relative Frequency (%)")
ax[0, 0].tick_params(axis="x", labelrotation=20)
目前看起来是这样的:

最好我希望它看起来像
2.5-3.8
3.8-5.0
,…

格式索引:

df.index = [f'{x.left:.1f}-{x.right:.1f}' for x in df.index]

# then plot
df["rel"].plot(kind="bar", ax=ax[0, 0])
如果您不想修改数据帧,请对照格式化的索引:

ax[0,0].bar([f'{x.left:.1f}-{x.right:.1f}' for x in df.index], df['rel'])

这非常有效,谢谢!您可以将任何想要的标签直接传递给熊猫。剪切