2016-09-20 93 views
0

我試圖在我的圖上添加第二組tic標記。我得到了get_xticks()的原始tic標記,將它們轉換爲我想要的(在此示例中簡單地添加100)並在從ax.twiny()獲得的座標軸上調用set_xticks()當在twiny()軸上設置標籤時,Matplotlib會擠壓軸標籤

當我這樣做時,我的雙軸標籤都被擠在右側,如圖右上方所示。

這是我使用的代碼:

ax2 = ax.twiny() 
ax2.set_xticks(ax.get_xticks()+100) 

enter image description here

FYI:

print(ax.get_xticks()) 
[ 950. 1000. 1050. 1100. 1150. 1200. 1250.] 

回答

0

試試這個(不完整的代碼;需要使用OPS合併):

from matplotlib.ticker import FuncFormatter 
def shift(x, pos): 
    return x+100 
formatter = FuncFormatter(shift) 

ax2 = ax1.twiny() 
ax2.xaxis.set_major_formatter(formatter) 
ax2.set_xlim(ax1.get_xlim()) 

Read als this question