matplotlib top bottom ticks different
Asked Answered
C

2

13

Is there a way to have top ticks in and bottom tick out in matplotlib plots? Sometimes I have data hiding ticks and I would like to set ticks out only for the side that is affected.

The following code will affect both top and bottom or both right and left.

import matplotlib.pyplot as plt
fig = plt.figure()
ax = fig.add_subplot( 111 )
ax.plot( [0, 1, 3], 'o' )
ax.tick_params( direction = 'out' )
plt.show()
Cyrene answered 9/11, 2013 at 23:16 Comment(0)
A
12

With the upgrade from #11859 for matplotlib>=3.1.0 we can now use a Secondary Axis via secondary_xaxis and secondary_yaxis to achieve independent tick directions:

import matplotlib.pyplot as plt
fig = plt.figure()
ax = fig.add_subplot( 111 )
ax.plot( [0, 1, 3], 'o' )
ax.tick_params( direction = 'out' )
ax_r = ax.secondary_yaxis('right')
ax_t = ax.secondary_xaxis('top')
ax_r.tick_params(axis='y', direction='in')
ax_t.tick_params(axis='x', direction='inout')

which produces this figure:

Axis with independent tick directions (in/out/inout) left and right as well as top and bottom.

Ambages answered 21/6, 2020 at 12:54 Comment(3)
You should specify which version can use this feature for me it does not workSlog
@GM I am very sorry my answer does not meet your standards of completeness. I did link the matplotlib PR that introduces this, which flags the milestone v3.1.0, so my guess is you need matplotlib>=3.1.0. What is your matplotlib version? Does upgrading solve the issue for you?Ambages
Seems so I had matplotlib 2Slog
B
10

You can have twin axes, then you can set the properties for each side separately:

import matplotlib.pyplot as plt
fig = plt.figure()
ax = fig.add_subplot(111)
ax.plot([0, 1, 3], 'o')

axR = ax.twinx()
axT = ax.twiny()

ax.tick_params(direction = 'out')
axR.tick_params(direction = 'in')

ax.tick_params(direction = 'out')
axT.tick_params(direction = 'in')

plt.show()

enter image description here

Brisbane answered 16/8, 2016 at 10:3 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.