Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

matplotlib ticks position relative to axis

Tags:

matplotlib

Is there a way in matplotlib to set ticks between the labels and the axis as it is by default in Origin? The examples section online does not show a single plot with this feature. I like having ticks outside the plotting area because sometimes the plot hides the ticks when inside.

like image 821
user1850133 Avatar asked Feb 05 '13 15:02

user1850133


1 Answers

To set the just the major ticks:

ax = plt.gca()
ax.get_yaxis().set_tick_params(direction='out')
ax.get_xaxis().set_tick_params(direction='out')
plt.draw()

to set all ticks (minor and major),

ax.get_yaxis().set_tick_params(which='both', direction='out')
ax.get_xaxis().set_tick_params(which='both', direction='out')
plt.draw()

to set both the x and y axis at the same time:

ax = plt.gca()
ax.tick_params(direction='out')

axis level doc and axes level doc

To shift the tick labels relative to the ticks use pad. Compare

ax.tick_params(direction='out', pad=5)
plt.draw()

with

ax.tick_params(direction='out', pad=15)
plt.draw()
like image 69
tacaswell Avatar answered Sep 24 '22 06:09

tacaswell