【问题标题】:How to make the x-axis values visible in a subplot with sharex=True after the axes below it has been deleted如何在删除其下方的轴后使用 sharex=True 使 x 轴值在子图中可见
【发布时间】:2022-12-12 06:15:58
【问题描述】:

我创建了一个包含多行和多列的子图,它们共享它们的 x 轴。 删除最右下方的 ax 后,其上方的 ax 不显示 x 轴值。如何使这些 x 轴值在此轴上可见?

我不想要的是稍后必须添加最右上角的斧头。

from matplotlib import pyplot as plt

fig, ax = plt.subplots(2, 3, sharex=True, sharey=True)
fig.tight_layout()
fig.delaxes(ax[1, 2])

plt.show()

这就是我得到的:

这是我想要得到的(此屏幕截图已在 Paint 中更改):

我试过这样的事情:

ax[0,2].xaxis.set_visible(True)
ax[0,2].set_xticks([0, 0.25, 0.5, 0.75, 1])
ax[0,2].set_xticklabels([0, 0.25, 0.5, 0.75, 1])

但这些都没有帮助。 谢谢!

【问题讨论】:

    标签: python matplotlib graph


    【解决方案1】:

    一种选择是:

    ax[0, 2].tick_params(labelbottom=True)
    

    【讨论】:

    • 这完全符合我的要求,谢谢!
    【解决方案2】:

    那么,子图是否必须共享 x 轴?

    如果没有,你可以这样做:

    from matplotlib import pyplot as plt
    
    fig, ax = plt.subplots(2, 3, sharex=False, sharey=True)
    fig.tight_layout()
    
    ax[0, 0].xaxis.set_visible(False)
    ax[0, 1].xaxis.set_visible(False)
    fig.delaxes(ax[1, 2])
    
    plt.show()
    

    如果您确实需要共享轴,也许可以将一个图移到底行?

    from matplotlib import pyplot as plt
    
    fig, ax = plt.subplots(2, 3, sharex=True, sharey=True)
    fig.tight_layout()
    
    fig.delaxes(ax[0, 2])
    
    plt.show()
    

    【讨论】:

      猜你喜欢
      • 2019-04-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-09-07
      • 2019-06-25
      相关资源
      最近更新 更多