2012-07-22 140 views
7

我想通过使用matlibplot轴来绘制2个子图。由于这两个子图具有相同的ylabel和刻度,我想关闭第二个子图的刻度和标记。以下是我的短脚本:如何关闭matlibplot坐标轴的刻度和标记?

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) 

顺便说一句,X轴标记重叠,不知道是否有一个整洁的解决与否。 (一个解决方案可能会使最后一个标记对每个子图都是不可见的,除了最后一个标记外,但不知道如何)。谢谢!

回答

8

快速谷歌,我发现答案:

plt.setp(ax2.get_yticklabels(), visible=False) 
ax2.yaxis.set_tick_params(size=0) 
ax1.yaxis.tick_left() 
4

稍微不同的解决方案可能是实际设置ticklabels为“”。下面将摆脱所有的y ticklabels和刻度线:

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

现在摆脱了过去的对勾标记和标签为x轴

# 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) 
相关问题