0
votes

I'm trying to create a figure with a number of non-uniform subplots. I would like to be able to create the plots using an iterable index so that I do not have to create each plot individually.

I can create a series of uniform subplots using fig, ax = plt.subplots(5) where I can plot to the various axes using ax[i].

fig, ax = plt.subplots(5)

Going forward I can plot to each plot using ax[i] using ax[0].plt etc.

However I would like to be able to create a series of plots that looks like:

gridsize = (10,3)
fig = plt.figure(figsize=(5,3))
ax0 = plt.subplot2grid(gridsize, (0, 0), colspan=3, rowspan=1)

for i in range(1,5):
    ax1 = plt.subplot2grid(gridsize, (i, 0), colspan=2, rowspan=1)
    ax2 = plt.subplot2grid(gridsize, (i, 2), colspan=2, rowspan=1)

where I can call each plot using ax[i] as above.

Does anyone have any ideas? Thanks.

1

1 Answers

0
votes

You may append the axes to a list from which to index the respective item or over which to iterate.

import numpy as np
import matplotlib.pyplot as plt

gridsize = (10,3)
fig = plt.figure(figsize=(5,3))
ax0 = plt.subplot2grid(gridsize, (0, 0), colspan=3, rowspan=1)

ax = [ax0]

for i in range(1,5):
    ax.append(plt.subplot2grid(gridsize, (i, 0), colspan=2, rowspan=1))
    ax.append(plt.subplot2grid(gridsize, (i, 2), colspan=2, rowspan=1))

## Now plot to those axes:

for i in range(2*4+1):
    ax[i].plot(np.arange(14),np.random.randn(14))

for axi in ax:
    axi.plot(np.arange(14),np.random.randn(14))


plt.show()