Reputation: 1005
I'm having a simple error with matplotlib's gridspec that I just can't seem to figure out. Can someone tell me where I'm going wrong?
import matplotlib.pyplot as plot
import matplitlib.gridspec as gridspec
gs = gridspec.GridSpec(1,3, width_ratios = [1,1], height_ratios = [2,1])
fig = plot.figure(figsize=(20,10))
ax1 = plot.subplot(gs[:, :-1])
ax2 = plot.subplot(gs[:, -1])
The error I get with this code is
IndexError: index 4 is out of bounds for axis 0 with size 4
This doesn't make sense to me. What I think I'm saying with this code is that the first axis (ax1) should take up all of the rows, and live in the first two columns. The second axis (ax2) should take up all of the rows, and live in only the third column. Is this not what my code actually means?
Upvotes: 6
Views: 15128
Reputation: 879601
gs = gridspec.GridSpec(1,3)
indicates that there is 1 row and 3 columns, but
width_ratios = [1,1]
implies there are 2 columns and height_ratios = [2,1]
implies there are 2 rows. Unfortunately matplotlib does not catch the contradiction upon instantiation of gs
, but the contradiction leads to the error later when
ax1 = plot.subplot(gs[:, :-1])
is called. To fix the error, you could specify 3 width ratios and a single height ratio:
gs = gridspec.GridSpec(1,3, width_ratios=[1,2,3], height_ratios=[1])
for example.
Upvotes: 8