Reputation: 48436
I'm trying to generate a matplotlib
figure with a specific aspect ration, 2 x-axes and 2 y-axes with different scales, and reformatted tick labels, along the lines of the following figure:
In matplotlib
I get as far as
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)))))
which gives me
but when I introduce additional axes, I get an error: ValueError: math domain error - dL_width = math.log10(dL.x1) - math.log10(dL.x0)
so that I can only proceed by commenting out the set_aspect
line with
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)))))
which results in a graph with the wrong aspect ratio:
Upvotes: 0
Views: 2759
Reputation: 87356
aspect
sets the ratio between the length in axes-space of one unit along the x-axis to one unit along the y-axis. This makes no sense for log scales as they are non linear and the ratio depends on where along the axes you look at it.
You want to use fig.set_size_inches
to make the aspect ratio what you want.
Upvotes: 1