Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
800 views
in Technique[技术] by (71.8m points)

matplotlib ticks position relative to axis

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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()

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...