How to disable ticks and labels of axes matlibplot?

I want to build 2 subheadings using the matlibplot axes. Since these two subtitles have the same labels and ticks, I want to disable the labels and characters of the second subtitle. Below 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 labels overlap and are not sure if there is a neat solution or not. (A decision can make the last character invisible for each subheading, except for the last, but not sure how). Thanks!

+7
source share
2 answers

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() 
+9
source

A slightly different solution might be to set the label labels to '. The following will save you all y-ticklabels and tags:

 # This is from @pelson 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 mark and label for the x axis

 # I used a for loop only because it shorter for ax in [ax1, ax2]: plt.setp(ax.get_xticklabels()[-1], visible=False) plt.setp(ax.get_xticklines()[-2:], visible=False) 
+4
source

All Articles