I have N subplots, and I want to share Y axis for all but one of them. Is it possible?
Yes, you can specify which suplots shares which axis with which axes (this is not a typo I mean this sentence). There's a sharex
and a sharey
argument for add_subplot
:
For example:
import numpy as np
import matplotlib.pyplot as plt
x = np.array([1,2,3,4,5])
y1 = np.arange(5)
y2 = y1 * 2
y3 = y1 * 5
fig = plt.figure()
ax1 = fig.add_subplot(131) # independant y axis (for now)
ax1.plot(x, y1)
ax2 = fig.add_subplot(132, sharey=ax1) # share y axis with first plot
ax2.plot(x, y2)
ax3 = fig.add_subplot(133) # independant y axis
ax3.plot(x, y3)
plt.show()
this will create a plot like this (1st and 2nd share the y axis, but the 3rd does not):
You can find another example of this in the matplotlib examples "Shared axis Demo".
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