I am plotting multiple sets of data on separate subplots, showing the difference between plotting a function on a linear vs a logarithmic x-axis. Since the subplot labels and x-axes remain unchanged, I am plotting the data on each subplot, saving the figure, removing the lines, and repeating (taking inspiration from this answer). I am able to autoscale the y-axis for each subplot based on the data in the corresponding subplot (thanks to this answer):
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.gridspec import GridSpec
fig = plt.figure(figsize=(12,6))
gs = GridSpec(1, 2, figure=fig, width_ratios=[1, 1])
ax1 = fig.add_subplot(gs[0, 0])
ax2 = fig.add_subplot(gs[0, 1])
ax1.set_xlabel(r"$x$")
ax1.set_yscale("log")
ax1.set_ylabel(r"$f(x)$")
ax2.set_xscale("log")
ax2.set_xlabel(r"$x$")
ax2.set_yscale("log")
ax2.set_ylabel(r"$f(x)$")
for i in range(whatever): # looping over all sets of data
ax1.plot(x_linear[i], y_linear[i])
ax1.relim()
ax1.autoscale(axis="y")
ax2.plot(x_log[i], y_log[i])
ax2.relim()
ax2.autoscale(axis="y")
fig.savefig("%s.png" % i)
for line in ax1.get_lines():
line.remove()
for line in ax2.get_lines():
line.remove()
However, I would like to autoscale the y-axis for both subplots based on all of the data, such that both y-axes are the same. For example, this is the sort of figure I am currently making, and this is the sort of figure I would like to make. I have considered two methods so far:
- plotting both sets of data in each subplot, rescaling each y-axis, and then removing the necessary lines from each subplot
- plotting both sets of data in each subplot and make the necessary lines invisible (
alpha=0.
)
While these would work, I have a lot of data and a lot of plots to make, and am just wondering if there is another tidier and more efficient way of going about this that avoids unnecessary plotting. Any help is appreciated.