Python 是否在matplotlib子批次上隐藏内部记号标记并选择外部记号标签?

Python 是否在matplotlib子批次上隐藏内部记号标记并选择外部记号标签?,python,matplotlib,Python,Matplotlib,我有一个由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,

我有一个由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')
产生以下结果:

我想要的最终输出是:


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

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

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')

以下功能用于删除内部轴记号标签:

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)

导入matplotlib.pyplot作为plt
将numpy作为np导入
def remove_internal_ticks(ax,remove_x=True,remove_y=True):
''函数从所有子批次(ax)中移除ytick标签,而不是上的子批次
第一列(提供remove_y=True)和子批次(ax)中的所有xtick标签
除最下面一行的内容外(前提是remove_x=True)。“”
nrows=np.尺寸(最大,0)
ncols=np.尺寸(最大,1)
对于范围内的i(nrows):
对于范围内的j(ncols):
如果移除_x和i0:
setp(ax[i,j].get_yticklabels(),visible=False)

谢谢你的回答,但我正试图在整个周长上画上记号(就像我的第二张照片),只有标签在左/下角。我已经用一个“Photoshop”的例子更新了这个问题的最终结果。我已经更新了答案,让你做你想做的事情:显示记号,但隐藏标签。这正是我想要的。我以前使用过get_yticklabels()命令,但从未使用过setp(),似乎这就是我所缺少的!谢谢,这非常有帮助!