4

我有一个由 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')

产生这个:

试图

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

最后

请注意左侧和底部的标签,但周边有刻度线。

4

2 回答 2

5

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

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')
于 2013-10-03T14:27:49.213 回答
0

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

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)

于 2019-11-29T12:59:15.637 回答