10

I'm trying to remove the origin ticks from my plot below to stop them overlapping, alternatively just moving them away from each other would also be great I tried this:

enter image description here

xticks = ax.xaxis.get_major_ticks()
xticks[0].label1.set_visible(False)
yticks = ax.yaxis.get_major_ticks()
yticks[0].label1.set_visible(False)

However this removed the first and last ticks from the y axis like so:

enter image description here

Does anyone have an idea about how to do this? Any help would be greatly appreciated.

EDIT: Added more example code

import matplotlib
import matplotlib.pyplot as plt
fig = plt.figure()
ax = fig.add_subplot(111)
plt.xlabel(xlab)
plt.ylabel(ylab)
ax.spines["right"].set_color('none')
ax.xaxis.set_ticks_position('top')
ax.yaxis.set_ticks_position('left')
ax.spines["bottom"].set_color('none')
ax.xaxis.set_label_position('top')
ax.spines['left'].set_color('black')
ax.spines['top'].set_color('black')
ax.tick_params(colors='black')
xticks = ax.xaxis.get_major_ticks()
xticks[0].label1.set_visible(False)
yticks = ax.yaxis.get_major_ticks()
yticks[-1].label1.set_visible(False)
for x, y in all:
    ax.plot(x, y, 'ro')
Jsg91
  • 465
  • 2
  • 6
  • 12

3 Answers3

11

You were almost there. The origin of the y axis is at the bottom. This means that the tick that you want to delete, being at the top, is the last one, that is yticks[-1]:

yticks[-1].set_visible(False)
gg349
  • 21,996
  • 5
  • 54
  • 64
  • so close yet so far, I tried both answers here and they had different results! Setting yticks to [-1] that removed the label at -145, what I don't understand is how my original effort managed to remove 2 ticks from the y axis – Jsg91 Oct 21 '13 at 16:36
  • but it is not a minimal WORKING example. – gg349 Oct 24 '13 at 13:37
  • Sorry what else do you need for it? – Jsg91 Oct 24 '13 at 15:51
  • the script should reproduce the error you describe. If i try to run your snippet I'd clearly get errors, since for example `all` is not defined. – gg349 Oct 24 '13 at 15:56
  • 1
    all is a list of data passed to this function I don't think you want the data as well do you? – Jsg91 Oct 24 '13 at 16:08
  • no, I want a script that reproduces the error you describe. This is a common request here at SO – gg349 Oct 24 '13 at 16:17
  • Isn't yticks a function? [matplotlib.pyplot.yticks] How can you apply an index to it? – imrek Aug 22 '15 at 08:13
9

Why not simply doing

ax.set_xticks(ax.get_xticks()[1:])
ax.set_yticks(ax.get_yticks()[:-1])

with ax being the axis object.

Jakob
  • 19,815
  • 6
  • 75
  • 94
2
ax.xaxis.get_major_ticks()[0].draw = lambda *args:None
ax.yaxis.get_major_ticks()[-1].draw = lambda *args:None
John La Rooy
  • 295,403
  • 53
  • 369
  • 502
  • I tried both answers and this managed to remove the label at 0.2 but then the tick at -145 instead of 140 – Jsg91 Oct 21 '13 at 16:37