2012-07-22 161 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) 
相關問題