8

To create 5 subplots I used:

`ax = plt.subplots(5, sharex=True)`

Then, I want to remove the first and the last label tick of each y-axis subplot (because they overplot each other), I used:

`plt.setp([a.get_yticklabels()[0::-1] for a in ax[0:5]], visible=False)`

But this just removes some of the ticks, I don't understand the logic behind.

BartoszKP
  • 34,786
  • 15
  • 102
  • 130
Py-ser
  • 1,860
  • 9
  • 32
  • 58

4 Answers4

22

You should be careful with the result of the first call. You might wanna call it like

fig, ax = plt.subplots(5, sharex=True, squeeze=True)

If you do this, you can then just iterate through all the axes:

for a in ax:
    # get all the labels of this axis
    labels = a.get_yticklabels()
    # remove the first and the last labels
    labels[0] = labels[-1] = ""
    # set these new labels
    a.set_yticklabels(labels)

If you want to keep your style of hiding the labels, you could use

for a in ax:
    plt.setp(a.get_yticklabels()[0], visible=False)    
    plt.setp(a.get_yticklabels()[-1], visible=False)

Note: You may have to call draw() before accessing the tick labels (see: https://stackoverflow.com/a/41131528/8144672). For example, when plotting to a PDF, you have to call plt.gcf().canvas.draw() before get_xticklabels().

Christian Rauch
  • 86
  • 3
  • 10
David Zwicker
  • 23,581
  • 6
  • 62
  • 77
  • Is it not exactly the same? However remember "a" is a numpy.ndarray, so it doenot support those options... – Py-ser Dec 03 '13 at 12:20
  • Thanks Py-ser for noting that the result from `plt.subplots` is a tuple and usually is a 2d-array. I added a note in my reply. My code still is not the same than the one above, since using `[0::-1]` is the same as using `[0]`. You thus only select the first label. – David Zwicker Dec 03 '13 at 12:27
  • Can `labels = ax.get_yticklabels()` just be outside the loop, or is `ax` a typo and should it be `a`? Or is `ax.get_yticklabels()` changing as you loop over `ax`? – knia Jul 31 '19 at 10:49
6

Use MaxNLocator:

from matplotlib.ticker import MaxNLocator
ax.yaxis.set_major_locator(MaxNLocator(prune='both'))
Ruggero Turra
  • 16,929
  • 16
  • 85
  • 141
3

Another option is to use x_ticks[0].label1.set_visible, like the following

import matplotlib
matplotlib.use('Agg')
import pylab
from  matplotlib import rc
rc('font', **{'family': 'sans-serif', 'sans-serif': ['Times-Roman']})
rc('text', usetex = True)
matplotlib.rcParams['text.latex.preamble'] = [r"\usepackage{amsmath}"]

figure, axs = matplotlib.pyplot.subplots(5, sharex = True, squeeze = True)
x_ticks = axs[4].xaxis.get_major_ticks()
x_ticks[0].label1.set_visible(False) ## set first x tick label invisible
x_ticks[-1].label1.set_visible(False) ## set last x tick label invisible
pylab.tight_layout()
pylab.savefig('./test.png', dpi = 200)

and you would get

invisible axis tick label

zyy
  • 1,271
  • 15
  • 25
0

This does not return what you expect:

>>> some_list = [1, 2, 3, 4]
>>> some_list[0::-1]
[1]  # and not [1, 4] !

Using slice notation returns a list, containing some_list items, starting in the example from position 0 and descreasing. This stops after the first step...

I think that you have to do this it two steps, as shown by @DavidZwicker.

Joël
  • 2,723
  • 18
  • 36