# [Matplotlib-users] Remove Axes, Rescale Others to Fill Figure

Tue May 4 17:41:00 EDT 2021

```All-

I frequently find myself in a position where I am programmatically
generating figures with many subplots. For example, I might have data from
8 channels for 10 experimental runs and want to compare the different runs
for each channel, ending up with 8 different figures (a figure for each
channel), each of which has 10 stacked subplots (a subplot for each run).

I look at the data I've plotted, and discover that, whoops, channel 0 had
an error and didn't produce any data during run number 3, and now I have an
empty axis in the middle of my figure. I'd like to be able to delete that
empty axes and have the other axes grow to fill the now empty space.

The Figure class provides the delaxes method, which removes the axes from
the figure, but doesn't change the size of any of the other axes. So I
still end up with a hole in my figure. I was hoping that tight_layout might
take care of it, but it assumes the axes are still there.

Is there an easy way to do this? Failing that, what's the hard way (I
assume it exists)?

Thank you,

# generate some data (only 2 channels/5 runs in this example)
nc, nr, nd = 2, 5, 12
data = {c: {r: [random() for x in range(nd)] for r in range(nr)} for c in
range(nc)}
data = [] # whoops, no data here.

# save the handles here
figaxlist = []
# for each channel
for chan in data:
nplots = len(data[chan].keys())
# create a figure with many subplots
fig, axs = plt.subplots(nplots, sharex=True)
figaxlist.append((fig, axs))
for run, ax in zip(data[chan].keys(), axs):
# plot the data
ax.plot(data[chan][run])

fig, axs = figaxlist

# removes axes, but leaves hole.
fig.delaxes(fig.axes)
-------------- next part --------------
An HTML attachment was scrubbed...