Two variables having different ranges are to be plotted in their own subplot, but their subplot share their y-axis, so there is just one y-axis range. Is the y-axis range supposed to depend on which variable goes onto which subplot?
For example, suppose
x = np.linspace(-100, 100, 1000)
When
y1 = 5e0 * np.sin(x / 5) + 7
y2 = 2e0 * np.sin(x / 5) + 7
, it does not seem to matter which subplot takes which variable.
fig, axs = plt.subplots(nrows=1, ncols=2, figsize=(15, 4))
axs[0].plot(x, y1)
axs[1].plot(x, y2)
axs[0].get_shared_y_axes().join(*axs)
all([ax.set_yscale('log') == None for ax in axs])
and
fig, axs = plt.subplots(nrows=1, ncols=2, figsize=(15, 4))
axs[0].plot(x, y2)
axs[1].plot(x, y1)
axs[0].get_shared_y_axes().join(*axs)
all([ax.set_yscale('log') == None for ax in axs])
give the same common y-axis range.
But, when
y1 = 5e5 * np.sin(x / 5) + 5.5e5
y2 = 2e5 * np.sin(x / 5) + 5.5e5
, this is not the case, and it appears that the common y-axis range is determined by the range of the variable in the last subplot. This means that if the variable in the last subplot has a range smaller than the variable in the first subplot, the variable in the first subplot has some part cropped off.
Is this behaviour expected? Because it means the variable with the larger range always needs to be plotted last in order to have full coverage of all variables.
I can reproduce this on matplotlib 1.4.3, assume it's a bug that has been fixed in later releases. Same behaviour with the syntax,
fig, axs = plt.subplots(nrows=1, ncols=2, figsize=(15, 4), sharey=True)
Best idea is to update matplotlib, but you can specify axis range manually to fix in your case,
[ax.set_ylim([np.min([y1,y2]),np.max([y1,y2])) for ax in axs]
So complete code,
fig, axs = plt.subplots(nrows=1, ncols=2, figsize=(15, 4))
axs[0].plot(x, y1)
axs[1].plot(x, y2)
axs[0].get_shared_y_axes().join(*axs)
for ax in axs:
all([ax.set_yscale('log') == None ])
ax.set_ylim([np.min([y1,y2]),np.max([y1,y2])])
plt.show()