简体   繁体   中英

Hide internal tick marks and select external tick labels on matplotlib subplots?

I have a figure consisting of 6 subplots of shape (2, 3). I would like to remove all internal tick marks and have only the left- and bottom-side display tick labels.

Default tick marks:

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)

produces this:

默认

After looking at countless examples, I've managed to remove the interior tick marks, but now new (additional) tick labels have appeared. The solutions I've found for removing tick labels don't work, they remove all x (or y) tick labels, not just the specified axis.

New code:

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

produces this:

尝试

What I want the final output to be is this:

决赛

Note the labels on the left and bottom, but tick marks around the perimeter.

This works for an arbitrarily sized grid. Your problem was that you weren't removing the ticks, you were just shifting them to the top:

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

The function below removes internal axis tick labels:

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM