【问题标题】:How to set a maximum number of x-ticks in matplotlib如何在 matplotlib 中设置 x-ticks 的最大数量
【发布时间】:2021-12-09 21:15:09
【问题描述】:

我正在尝试在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() 时我无法找到类似的解决方案。

【问题讨论】:

    标签: python matplotlib plot xticks


    【解决方案1】:

    您可以使用以下方法获取轴的实例:

    ax = plt.gca()
    

    那么,你就可以使用你平时在轴上使用的方法了:

    ax.xaxis.set_major_locator(....)
    

    【讨论】:

    • 好技巧,效果很好!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-01-11
    • 1970-01-01
    • 2021-11-16
    • 1970-01-01
    • 2020-12-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多