2013-12-20 91 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

相關問題