2013-12-20 94 views
0

我正在尝试生成一个matplotlib图,其中具有不同刻度的特定纵横比,2个x轴和2个y轴以及重新格式化的刻度标签如下图线:为坐标轴上的多个刻度设置matplotlib纵横比

enter image description here

matplotlib我得到尽可能

x_power_min = -2 
x_power_max = 3 
y_power_min = -5 
y_power_max = 2 
fig_ratio = 1.2 

fig, axy = plt.subplots() 

axy.set(xscale='log', xlim=[10**x_power_min,10**x_power_max], xlabel='X1') 
axy.set(yscale='log', ylim=[10**y_power_min,10**y_power_max], ylabel='Y1') 

axy.set_aspect(float(x_power_max-x_power_min)/float(y_power_max-y_power_min) * fig_ratio) 

axy.xaxis.set_major_formatter(matplotlib.ticker.FuncFormatter(lambda x, pos: '{:0d}'.format(int(math.log10(x))))) 
axy.yaxis.set_major_formatter(matplotlib.ticker.FuncFormatter(lambda y, pos: '{:0d}'.format(int(math.log10(y))))) 

这给了我

enter image description here

但是当我介绍附加轴,我得到一个错误:ValueError: math domain error - dL_width = math.log10(dL.x1) - math.log10(dL.x0)使得我只能通过注释掉与

fig, axy = plt.subplots() 
ay2 = axy.twinx() 
ax2 = axy.twiny() 

axy.set(xscale='log', xlim=[10**x_power_min,10**x_power_max], xlabel='X1') 
axy.set(yscale='log', ylim=[10**y_power_min,10**y_power_max], ylabel='Y1') 
ay2.set(yscale='log', ylim=[317.83*10**y_power_min,317.83*10**y_power_max], ylabel='Y2') 
ax2.set(xscale='log', xlim=[10**(3*x_power_min/2.0),10**(3*x_power_max/2.0)], xlabel='X2') 

#axy.set_aspect(float(x_power_max-x_power_min)/float(y_power_max-y_power_min) * fig_ratio) 

axy.xaxis.set_major_formatter(matplotlib.ticker.FuncFormatter(lambda x, pos: '{:0d}'.format(int(math.log10(x))))) 
axy.yaxis.set_major_formatter(matplotlib.ticker.FuncFormatter(lambda y, pos: '{:0d}'.format(int(math.log10(y))))) 
ay2.yaxis.set_major_formatter(matplotlib.ticker.FuncFormatter(lambda y, pos: '{:0d}'.format(int(math.log10(y))))) 
ax2.xaxis.set_major_formatter(matplotlib.ticker.FuncFormatter(lambda x, pos: '{:0d}'.format(int(math.log10(x))))) 

其导致的曲线图与错误的宽高比set_aspect线继续:

enter image description here

回答

1

aspect设置在长度之比一个单元ALON的轴空间将x轴沿着y轴移动到一个单位。这对于对数尺度是没有意义的,因为它们是非线性的,并且比率取决于沿着您看它的轴线的位置。

您想使用fig.set_size_inches来制作您想要的宽高比。

+0

如果它对于对数刻度“没有意义”,它为什么最初起作用? – orome

+0

什么是“默认”大小(即,如何重现我使用其他方法获得的纵横比和缩放比例)。像'fig.set_size_inches(fig.get_size_inches()[0],fig_ratio * fig.get_size_inches()[0])'似乎是要走的方向,但它会产生一个更大的数字。 – orome

+2

现在它不应该起作用并且对主人发出警告。如果'fig_ratio'大于1,那么你的身材会变得更大。 – tacaswell

相关问题