簡體   English   中英

如何在 matplotlib 中設置 x-ticks 的最大數量

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

我試圖在matplotlib繪制兩個長時間序列。 因為我不需要任何子圖,所以我沒有使用 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")

但是,上述格式適用於一些短系列,而對於長系列,即使使用autofmt_xdate()命令旋轉,x-ticks 也會重疊很多。 我知道的ax.set_major_locator()使用的次要情節,但我沒能有工作時,發現了類似的解決方案時,解決方案plt.figure()

您可以使用以下方法獲取軸的實例:

ax = plt.gca()

然后,您可以使用您通常在軸上使用的方法:

ax.xaxis.set_major_locator(....)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM