2016-09-20 91 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