Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Removing specific ticks from matplotlib plot

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')
like image 295
Jsg91 Avatar asked Oct 21 '13 15:10

Jsg91


3 Answers

Why not simply doing

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

with ax being the axis object.

like image 122
Jakob Avatar answered Sep 22 '22 18:09

Jakob


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)
like image 23
gg349 Avatar answered Sep 22 '22 18:09

gg349


ax.xaxis.get_major_ticks()[0].draw = lambda *args:None
ax.yaxis.get_major_ticks()[-1].draw = lambda *args:None
like image 21
John La Rooy Avatar answered Sep 20 '22 18:09

John La Rooy