Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/cocoa/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 为轴上具有多个比例的图形设置matplotlib纵横比_Python_Matplotlib_Plot_Aspect Ratio_Multiple Axes - Fatal编程技术网

Python 为轴上具有多个比例的图形设置matplotlib纵横比

Python 为轴上具有多个比例的图形设置matplotlib纵横比,python,matplotlib,plot,aspect-ratio,multiple-axes,Python,Matplotlib,Plot,Aspect Ratio,Multiple Axes,我正试图生成一个具有特定纵横比的matplotlib图形,两个x轴和两个y轴具有不同的比例,并沿着下图的线条重新格式化记号标签: 在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_powe

我正试图生成一个具有特定纵横比的
matplotlib
图形,两个x轴和两个y轴具有不同的比例,并沿着下图的线条重新格式化记号标签:

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)))))
这让我

但是当我引入额外的轴时,我得到了一个错误:
ValueError:math domain error-dL_width=math.log10(dL.x1)-math.log10(dL.x0)
,因此我只能通过注释掉
set_aspect
行来继续

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)))))
这将导致图形的纵横比错误:


纵横比
设置沿x轴的一个单位的轴空间长度与沿y轴的一个单位的轴空间长度之间的比率。这对于对数刻度没有任何意义,因为它们是非线性的,比率取决于沿轴的位置


您想使用
fig.set_size_inches
使纵横比符合您的要求。

如果它对对数刻度“毫无意义”,为什么最初会起作用?什么是“默认”大小(即,我如何重现用另一种方法得到的纵横比和刻度)。类似于
fig.set\u size\u inches(fig.get\u size\u inches()[0],fig\u ratio*fig.get\u size\u inches()[0])
似乎是一个方向,但它会生成一个更大的数字。它不应该工作,现在会在master上发出警告。如果
fig\u ratio
大于1,则您的身材将变大。否:
fig.set\u size\u inches(fig.get\u size\u inches()[1]/fig.get\u size\u inches()[1])
执行此操作。因为您正在分割;)