Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

matplotlib top bottom ticks different

Tags:

matplotlib

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()
like image 298
user1850133 Avatar asked Nov 09 '13 23:11

user1850133


2 Answers

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.

like image 108
Zaus Avatar answered Sep 22 '22 17:09

Zaus


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

like image 33
Luis Avatar answered Sep 19 '22 17:09

Luis