I want to have a row of subplots with the same aspect ratio. Let's say I want to plot two different functions that have different extents on the y axis.
There seem to be lots of questions on this but the examples seem to conveniently have the same axes for all subplots. In any case none that I found were helpful to me.
I've tried the following
import numpy as np
import matplotlib.pyplot as plt
xdata = np.arange(0,2,.01)
ydata1 = xdata
ydata2 = xdata ** 2
fig, ax = plt.subplots(1,2)
ax[0].plot(xdata,ydata1)
ax[1].plot(xdata,ydata2)
ax[0].set_aspect(.5)
ax[1].set_aspect(.5)
plt.show(fig)
However, matplotlib appears to be using the x and y axis data to determine the aspect ratio and because the y axes have different extents the subplots come out different. This is super weird to me. How do I make them the same physical aspect ratio?
I guess one can try extracting the the limits for each subplot and calculate the right aspect ratio individually but this seems to be a very roundabout way and I would expect such a basic functionality as making all subplots look the same to be built in.
EDIT So what I want is for both subplots to have the same aspect ratio in the human sense, that is that the ratio of the width of the figure to the height is the same for both. For example in Mathematica I would achieve this by simply setting the AspectRatio attribute:
xdata = Range[0, 2, .01];
ydata1 = {#, #} & /@ xdata;
ydata2 = {#, #^2} & /@ xdata;
fig = GraphicsRow[
ListLinePlot[#, AspectRatio -> 1/GoldenRatio, Frame -> True,
ImageSize -> 300] & /@ {ydata1, ydata2}];
Export["fig.png", fig]





fig, ax = plt.subplots(1,2, sharex=True, sharey=True)? - Quang Hoang