<div dir="ltr"><div>All-</div><div><br></div><div>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).<br><br>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.<br><br>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.<br></div><div><br></div><div>Is there an easy way to do this? Failing that, what's the hard way (I assume it exists)?</div><div><br></div><div>Thank you,</div><div>--Chad<br></div><div><br></div><div><br></div><div><span style="font-family:monospace"># generate some data (only 2 channels/5 runs in this example)<br></span></div><div><span style="font-family:monospace">nc, nr, nd = 2, 5, 12<br></span></div><div><span style="font-family:monospace">data = {c: {r: [random() for x in range(nd)] for r in range(nr)} for c in range(nc)}</span></div><div><span style="font-family:monospace">data[0][3] = [] # whoops, no data here.<br></span></div><div><span style="font-family:monospace"><br></span></div><div><span style="font-family:monospace">
 # save the handles here

</span></div><div><span style="font-family:monospace">figaxlist = []</span></div><div><span style="font-family:monospace"># for each channel<br></span></div><div><span style="font-family:monospace">for chan in data:</span></div><div><span style="font-family:monospace">  nplots = len(data[chan].keys())</span></div><div><span style="font-family:monospace">  # create a figure with many subplots<br></span></div><div><span style="font-family:monospace">  fig, axs = plt.subplots(nplots, sharex=True)</span></div><div><span style="font-family:monospace">  figaxlist.append((fig, axs))

</span></div><div><span style="font-family:monospace">  for run, ax in zip(data[chan].keys(), axs):</span></div><div><span style="font-family:monospace">    # plot the data<br></span></div><div><span style="font-family:monospace">    ax.plot(data[chan][run])</span></div><div><span style="font-family:monospace"><br></span></div><div><span style="font-family:monospace">fig, axs = figaxlist[0]</span></div><div><span style="font-family:monospace"><br></span></div><div><span style="font-family:monospace"># removes axes, but leaves hole.<br></span></div><div><span style="font-family:monospace">fig.delaxes(fig.axes[3])</span><br></div></div>