简体   繁体   English

如何在 matplotlib 中设置 x-ticks 的最大数量

[英]How to set a maximum number of x-ticks in matplotlib

I am trying to plot two long time series in matplotlib .我试图在matplotlib绘制两个长时间序列。 Since I am not going to need any subplot, I did not use the figure.add_subplot() method.因为我不需要任何子图,所以我没有使用 figure.add_subplot() 方法。

fig = plt.figure()
plt.plot(series1)
plt.plot(series2)
plt.xlabel('Date')
plt.ylabel("Values")
plt.legend(loc='best')
plt.title(Plot title)
fig.autofmt_xdate()
plt.savefig("fig.png", format="png")

However, the above format works well with some short series, while with long ones the x-ticks overlap quite a bit even if they are rotated with the autofmt_xdate() command.但是,上述格式适用于一些短系列,而对于长系列,即使使用autofmt_xdate()命令旋转,x-ticks 也会重叠很多。 I am aware of the ax.set_major_locator() solution when using subplots but I was not able to find a similar solution when working with plt.figure() .我知道的ax.set_major_locator()使用的次要情节,但我没能有工作时,发现了类似的解决方案时,解决方案plt.figure()

You can get an instance of the axis using the following:您可以使用以下方法获取轴的实例:

ax = plt.gca()

Then, you can use the methods you normally use on the axis:然后,您可以使用您通常在轴上使用的方法:

ax.xaxis.set_major_locator(....)

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

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