ValueError: Number of rows must be a positive integer, not GridSpec(2, 1, height_ratios=[1, 3])[1:2, 0:1] python matplotlib

Viewed 7748

Trying to make a 2 row 1 col subplot with gridspec and keep getting the error

ValueError: Number of rows must be a positive integer, not GridSpec(2, 1, height_ratios=[1, 3])[1:2, 0:1]

when using code

fig, ax = plt.subplots(figsize = (8,6))
gs = gridspec.GridSpec(2,1,height_ratios = [1,3])

and I cant for the life of me figure out why. I want the subplot on top to be the same width but about 1/3 the height of the plot below.

1 Answers

'GridSpec' should be in the following format.

import matplotlib.pyplot as plt
from matplotlib import gridspec

fig, ax = plt.subplots(figsize = (8,6))
fig.clf()

gs = gridspec.GridSpec(2,1, height_ratios=[3,1], hspace=0.2)
ax1 = fig.add_subplot(gs[0,0])
ax2 = fig.add_subplot(gs[1,0])

enter image description here

Related