Reputation: 1385
I try to plot two plots vertically within one figure using gridspec. The upper one is supposed to be twice as high as the lower one (i.e. the ratio is 2:1 with 3 equal vertical parts) and they have to share the x-axis scale. I have the following minimal example, which produces the correct 3-part plot, but it does not span the upper subplot twice as high as the lower one. The first plot is at the top third, the second is in the bottom third, and the middle is emptyWhere is the error?
import matplotlib.gridspec as gridspec
x = y = [1,2,3]
gs = gridspec.GridSpec(3, 1)
ax1 = plt.subplot(gs[0:1, 0])
ax1.plot(x, y)
ax2 = plt.subplot(gs[2, 0], sharex=ax1)
ax2.plot(x,y)
plt.show()
Upvotes: 2
Views: 4856
Reputation: 53688
When using gridspec
the indexing acts similarly to numpy arrays. As such you need gs[0:2, 0]
to get the output that you want.
import matplotlib.pyplot as plt
from matplotlib import gridspec
x = y = [1,2,3]
gs = gridspec.GridSpec(3, 1)
ax1 = plt.subplot(gs[0:2, 0])
ax1.plot(x, y)
ax2 = plt.subplot(gs[2, 0], sharex=ax1)
ax2.plot(x,y)
plt.show()
Note that you could use plt.tight_layout()
to remove the overlap of axis
Upvotes: 2