【问题标题】:How to get ticks on the bottom subplot in matplotlib如何在 matplotlib 的底部子图中获取刻度
【发布时间】:2020-06-11 21:49:36
【问题描述】:

我在一个图中绘制了 8 个子图,如下所示:

import matplotlib.pyplot as plt
fig, axs = plt.subplots(8,sharex="col" )
label = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H']
data = [0.6, 0.4, 1.3, 0.8, 0.9, 1.0, 1.6, 0.2]
plt.xlim(0,2)
for i in range(8):
    axs[i].set_xlim([0, 2])
    axs[i].axvline(data[i],linestyle='--')
    axs[i].set_yticks(())
    axs[i].set_ylabel(label[i], rotation=0, ha='right', va='center')
axs[7].tick_params(axis='x', direction='out')
plt.show()

除了 x 轴上没有刻度外,一切正常。我做错了什么?

【问题讨论】:

  • matplotlib 2.2.2 对我来说很好用。也许试试sharex=True 我看不出它不适合你的原因。尝试重新启动内核
  • 也在 3.0.2 上为我工作。在导入pyplot 之前使用agg 非交互式后端(import matplotlib; matplotlib.use('Agg'),然后使用plt.savefig(filename) 而不是plt.show(),您是否得到相同的结果?想知道是否是您的X 显示器以某种方式导致了问题。
  • 使用 3.1.3。
  • 这是在带有 %matplotlib qt 的 Jupyter 笔记本中。你能重现吗? @Sheldore
  • 我在 jupyter 中尝试了 qt,它在新窗口中打开了一个图,我仍然可以看到刻度

标签: python matplotlib


【解决方案1】:

我在 cmets 中回答你的问题。您可以使用以下命令隐藏除最后一个轴之外的所有轴的刻度

for i in range(8):
    axs[i].set_xlim([0, 2])
    axs[i].axvline(data[i],linestyle='--')
    axs[i].set_yticks(())
    axs[i].set_ylabel(label[i], rotation=0, ha='right', va='center')
    if i!=7:
        axs[i].tick_params(axis='x', direction='out', length=0) # Hide the ticks for all but last axis
        # axs[i].xaxis.set_tick_params(direction='out', length=0) # This is 2nd way to do

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-12-15
    • 2012-10-14
    • 1970-01-01
    • 2016-04-17
    • 2014-06-30
    • 2013-01-05
    • 1970-01-01
    • 2021-03-11
    相关资源
    最近更新 更多