【问题标题】:Hide internal tick marks and select external tick labels on matplotlib subplots?隐藏内部刻度线并在 matplotlib 子图上选择外部刻度线标签?
【发布时间】:2013-10-03 13:59:27
【问题描述】:

我有一个由 6 个形状 (2, 3) 的子图组成的图形。我想删除所有内部刻度线,只显示左侧和底部的刻度线标签。

默认刻度线:

import matplotlib.pyplot as plt
import numpy as np

fig, ax = plt.subplots(2,3,
                       sharex = True,
                       sharey = True)
plt.subplots_adjust(hspace = 0,
                    wspace = 0)

产生这个:

在查看了无数示例之后,我设法删除了内部刻度线,但现在出现了新的(附加)刻度线标签。我找到的删除刻度标签的解决方案不起作用,它们会删除所有 x(或 y)刻度标签,而不仅仅是指定的轴。

新代码:

import matplotlib.pyplot as plt
import numpy as np

fig, ax = plt.subplots(2,3,
                       sharex = True,
                       sharey = True)
plt.subplots_adjust(hspace = 0,
                    wspace = 0)
ax[0,0].xaxis.set_ticks_position('top')
ax[0,0].yaxis.set_ticks_position('left')
ax[0,1].xaxis.set_ticks_position('top')
ax[0,1].yaxis.set_ticks_position('none')
ax[0,2].xaxis.set_ticks_position('top')
ax[0,2].yaxis.set_ticks_position('right')

ax[1,0].xaxis.set_ticks_position('bottom')
ax[1,0].yaxis.set_ticks_position('left')
ax[1,1].xaxis.set_ticks_position('bottom')
ax[1,1].yaxis.set_ticks_position('none')
ax[1,2].xaxis.set_ticks_position('bottom')
ax[1,2].yaxis.set_ticks_position('right')

产生这个:

我想要的最终输出是这样的:

注意左侧和底部的标签,但要在周边打勾。

【问题讨论】:

    标签: python matplotlib


    【解决方案1】:

    这适用于任意大小的网格。你的问题是你没有移除蜱虫,你只是将它们移到顶部:

    import matplotlib.pyplot as plt
    import numpy as np
    
    Nrows = 2
    Ncols = 3
    
    fig, ax = plt.subplots(Nrows, Ncols,
                           sharex=True,
                           sharey=True)
    plt.subplots_adjust(hspace=0,
                        wspace=0)
    
    
    for i in range(Nrows):
        for j in range(Ncols):
            if i == 0:
                ax[i,j].xaxis.set_ticks_position('top')
                plt.setp(ax[i,j].get_xticklabels(), visible=False)
            elif i == Nrows-1:
                ax[i,j].xaxis.set_ticks_position('bottom')
            else:
                ax[i,j].xaxis.set_ticks_position('none')
    
            if j == 0:
                ax[i,j].yaxis.set_ticks_position('left')
            elif j == Ncols-1:
                ax[i,j].yaxis.set_ticks_position('right')
                plt.setp(ax[i,j].get_yticklabels(), visible=False)
            else:
                ax[i,j].yaxis.set_ticks_position('none')
    

    【讨论】:

    • 感谢您的回答,但我试图在整个周边(如我的第二张图片中)上获得刻度线,左侧/底部只有标签。我已经用所需最终输出的“photoshopped”示例更新了问题。
    • 我已经更新了答案来做你想做的事:显示刻度但隐藏标签。
    • 正是我想要的。我以前使用过 get_yticklabels() 命令,但从未使用过 setp(),这似乎是我所缺少的!谢谢,这非常有帮助!
    【解决方案2】:

    下面的函数删除内部轴刻度标签:

    import matplotlib.pyplot as plt
    import numpy as np
    
    def remove_internal_ticks(ax,remove_x = True,remove_y = True):
        '''Function removes ytick labels from all the subplots (ax) other than those on
        the first column (provided remove_y=True) and all xtick labels from subplots (ax) 
        other than those on the bottom row (provided remove_x=True).'''
        nrows = np.size(ax,0)
        ncols = np.size(ax,1)
        for i in range(nrows):
            for j in range(ncols):
                if remove_x and i<nrows-1:
                    plt.setp(ax[i,j].get_xticklabels(), visible=False)
    
                if remove_y and j>0:
                    plt.setp(ax[i,j].get_yticklabels(), visible=False)
    
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-06-10
      • 2021-06-14
      • 2013-06-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多