How to turn off the ticks AND marks of a matlibplot axes?
Asked Answered
L

2

8

I want to plot 2 subplots by using matlibplot axes. Since these two subplots have the same ylabel and ticks, I want to turn off both the ticks AND marks of the second subplot. Following is my short script:

import matplotlib.pyplot as plt
ax1=plt.axes([0.1,0.1,0.4,0.8])
ax1.plot(X1,Y1)
ax2=plt.axes([0.5,0.1,0.4,0.8])
ax2.plot(X2,Y2)

BTW, the X axis marks overlapped and not sure whether there is a neat solution or not. (A solution might be make the last mark invisible for each subplot except for the last one, but not sure how). Thanks!

Lynnell answered 22/7, 2012 at 21:38 Comment(0)
R
11

A quick google and I found the answers:

plt.setp(ax2.get_yticklabels(), visible=False)
ax2.yaxis.set_tick_params(size=0)
ax1.yaxis.tick_left()
Richy answered 22/7, 2012 at 22:7 Comment(0)
U
4

A slightly different solution might be to actually set the ticklabels to ''. The following will get rid of all the y-ticklabels and tick marks:

# This is from @pelson's answer
plt.setp(ax2.get_yticklabels(), visible=False)

# This actually hides the ticklines instead of setting their size to 0
# I can never get the size=0 setting to work, unsure why
plt.setp(ax2.get_yticklines(),visible=False)

# This hides the right side y-ticks on ax1, because I can never get tick_left() to work
# yticklines alternate sides, starting on the left and going from bottom to top
# thus, we must start with "1" for the index and select every other tickline
plt.setp(ax1.get_yticklines()[1::2],visible=False)

And now to get rid of the last tickmark and label for the x-axis

# I used a for loop only because it's shorter
for ax in [ax1, ax2]:
    plt.setp(ax.get_xticklabels()[-1], visible=False)
    plt.setp(ax.get_xticklines()[-2:], visible=False)
Unnamed answered 24/7, 2012 at 1:34 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.