I want to compare subplots visually with ease. To do this, I want to set the same scale for all subplots.
My code works fine, and I'm able to plot subplots, but with their own scales. I want to maintain the scale on the x axis.
To change the size of subplots in Matplotlib, use the plt. subplots() method with the figsize parameter (e.g., figsize=(8,6) ) to specify one size for all subplots — unit in inches — and the gridspec_kw parameter (e.g., gridspec_kw={'width_ratios': [2, 1]} ) to specify individual sizes.
If we use "equal" as an aspect ratio in the function, we get a plot with the same scaling from data points to plot units for X-axis and Y-axis. It sets both X-axis and Y-axis to have the same range. Then ax. set_aspect('equal') sets both axes to be equal.
If you want to have two subplots with the same xaxis, you can use the sharex
-keyword when you create the second axes:
import matplotlib.pyplot as plt
import numpy as np
fig = plt.figure()
ax1 = fig.add_subplot(2, 1, 1)
ax2 = fig.add_subplot(2, 1, 2, sharex=ax1)
t = np.linspace(0, 1, 1000)
ax1.plot(t, np.sin(2 * np.pi * t))
ax2.plot(t, np.cos(2 * np.pi * t))
plt.show()
Result:
If you want to use subplots:
fig,axs = plt.subplots(2,1, figsize = (10,8), sharex=True)
x = np.random.randn(1000)
x1 = x + 3
sns.histplot(x, ax = axs[0])
sns.histplot(x1, ax = axs[1])
fig.show()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With