2

I'd like to remove all but the first and last tick labels, but keep their ticks on a plot. However, using the below code, all labels get removed

import matplotlib.pyplot as plt
import numpy as np

fig, ax = plt.subplots(1, 1)
ax.plot(np.arange(10))

locs = ax.get_xticks()
labels = ax.get_xticklabels()
labels[1].set_text('')  # Should only remove the 2nd label
ax.set_xticks(locs)
ax.set_xticklabels(labels)

results in:

enter image description here

Where are all my labels are removed, rather than just the 2. How can I remove just that single number?

I'd expect the following plot to show up (rendered by BigBen using the code above in a comment, using matplotlib 3.6.0 and Python 3.9.7):

Preferably in a method capable of removing all but the first and last label. For that I tried:

for label_idx in range(1, len(labels)-1):
    labels[label_idx].set_text('') 

which also removed all labels. I want all ticks but only the labels 0 and 8 to show up, i.e. remove the labels 2, 4 and 6.

I'm using Python 3.9.12 with matplotlib 3.5.2 on a Spyder 5.3.2 IDE. After upgrading to Python 3.9.15 and matplotlib 3.5.3 the issue remains.

The code in Bhargav's answer generates the same issue on my system: it removes all labels for me. I'm going to presume this is a bug in Python, matplotlib or Spyder.

Adriaan
  • 17,741
  • 7
  • 42
  • 75
  • No repro, I get https://i.stack.imgur.com/Z4JgZ.png. Note that your xticks span -2 to 10. – BigBen Dec 01 '22 at 14:33
  • @BigBen I've added my versions, but yes, that's exactly what I'd expect to happen. (Although removing all the locs/labels stuff I get ticks from 0 to 8, rather than -2 to 10) – Adriaan Dec 01 '22 at 14:37
  • Ok. I'm running matplotlib 3.6.0 and Python 3.9.7. The second point was that, at least for me, `ax.get_xticks()` returns `array([-2., 0., 2., 4., 6., 8., 10.])`. – BigBen Dec 01 '22 at 14:39
  • @BigBen could this be a Spyder problem? If use the script to create an image without changing the tick labels and then call `print(labels)` within the terminal, I get the list of `text` objects displayed correctly, However, if I call that within the script, all label-texts show up empty – Adriaan Dec 01 '22 at 15:08
  • Very possibly, but I can't test on Spyder currently. – BigBen Dec 01 '22 at 15:10

1 Answers1

1

In

Python  - 3.9.10
matplotlib: 3.5.1

Uisng Tick_formatter

plt.xticks(np.arange(min(locs), max(locs)+1, 2))
ax.xaxis.get_major_ticks()[1].draw = lambda *args:None

Gives #

enter image description here

if you want to keep ticks

plt.xticks(np.arange(min(locs), max(locs)+1, 2))
labels = [item.get_text() for item in ax.get_xticklabels()]
labels[1] = ''
ax.set_xticklabels(labels)

Gives #

enter image description here

  • Thanks for that. However, on my system that does the same as in my question: remove *all* labels. I'm going to presume this to be a bug with my specific versions of Python and matplotlib, or, probably more likely, Spyder. – Adriaan Dec 01 '22 at 15:36
  • @Adriaan I've upadted my current despencies...Try to replicate them...Mostly I guess It's issue with `matplot lib`? – Bhargav - Retarded Skills Dec 01 '22 at 15:40
  • I'd sooner assume Spyder, that does iffy stuff with every library somewhere in the background without telling you naught. But yes, a bug somewhere down the line is more likely. (As a side note, rather than building your own ticks and grabbing the text using a list comprehension, you could use my original code, see the comment by BigBen) – Adriaan Dec 01 '22 at 15:42
  • I got same as you output you got when I run your code @Adriaan – Bhargav - Retarded Skills Dec 01 '22 at 15:50