Pandas:条形图 xtick 频率

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19143857/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 21:12:50  来源:igfitidea点击:

Pandas: bar plot xtick frequency

pythonmatplotlibplotpandas

提问by KLI

I want to create a simple bar chart for pandas DataFrame object. However, the xtick on the chart appears to be too granular, whereas if I change the plot to line chart, xtick is optimized for better viewing. I was wondering if I can bring the same line chart xtick frequency to bar chart? Thanks.

我想为 Pandas DataFrame 对象创建一个简单的条形图。但是,图表上的 xtick 似乎过于精细,而如果我将绘图更改为折线图,则 xtick 会进行优化以获得更好的查看效果。我想知道是否可以将相同的折线图 xtick 频率带入条形图?谢谢。

locks.plot(kind='bar',y='SUM')

EDIT

编辑

Resultant plot:enter image description here

结果图:在此处输入图片说明

回答by Saullo G. P. Castro

You can reduce the number of thicks by setting one every nticks, doing something like:

您可以通过每个n刻度设置一个来减少厚度的数量,执行如下操作:

n = 10

ax = locks.plot(kind='bar', y='SUM')
ticks = ax.xaxis.get_ticklocs()
ticklabels = [l.get_text() for l in ax.xaxis.get_ticklabels()]
ax.xaxis.set_ticks(ticks[::n])
ax.xaxis.set_ticklabels(ticklabels[::n])

ax.figure.show()