<div dir="ltr"><div>Hi Adrien-</div><div><br></div><div>This is exactly the effect I'm looking to achieve. Thank you.</div><div><br></div><div>The only problem is that it appears to be a solution for only the near term:<br></div><div>MatplotlibDeprecationWarning: <br>The change_geometry function was deprecated in Matplotlib 3.4 and will be removed two minor releases later. Use set_subplotspec instead.<br>  ax.change_geometry(n_nonempty, 1, idx+1)<br>MatplotlibDeprecationWarning: <br>The update_params function was deprecated in Matplotlib 3.4 and will be removed two minor releases later.<br>  ax.change_geometry(n_nonempty, 1, idx+1)<br>MatplotlibDeprecationWarning: <br>The figbox attribute was deprecated in Matplotlib 3.4 and will be removed two minor releases later. Use get_subplotspec().get_position(self.figure) instead.<br>  ax.change_geometry(n_nonempty, 1, idx+1)</div><div><br></div><div>These messages suggest that there must be a way to accomplish this with subplotspecs. A little further digging and I came up with a very similar alternative:</div><div><br>gs = gridspec.GridSpec(len(fig.axes), 1)<br>for i, ax in enumerate(ig.axes):<br>  ax.set_subplotspec(gs[i])</div><div><br></div><div>Thanks for your solution!</div><div>--Chad<br></div></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Wed, May 5, 2021 at 3:09 AM <a href="mailto:vincent.adrien@gmail.com">vincent.adrien@gmail.com</a> <<a href="mailto:vincent.adrien@gmail.com">vincent.adrien@gmail.com</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">Hi Chad,<br>
<br>
I am no expert on the topic, but I found the `ax.update_geometry` method that may allow to solve your problem. See at the end of the following example adapted from your own code.<br>
<br>
```python<br>
import matplotlib.pyplot as plt<br>
from numpy.random import random<br>
plt.ion()<br>
<br>
# generate some data (only 2 channels/5 runs in this example)<br>
nc, nr, nd = 2, 5, 12<br>
data = {c: {r: [random() for x in range(nd)] for r in range(nr)} for c in range(nc)}<br>
data[0][3] = [] # whoops, no data here.<br>
<br>
# save the handles here<br>
figaxlist = []<br>
# for each channel<br>
for chan in data:<br>
  nplots = len(data[chan].keys())<br>
  # create a figure with many subplots<br>
  fig, axs = plt.subplots(nplots, sharex=True)<br>
  figaxlist.append((fig, axs))<br>
  for run, ax in zip(data[chan].keys(), axs):<br>
    # plot the data<br>
    ax.plot(data[chan][run], color=f"C{run}")<br>
<br>
fig, axs = figaxlist[0]<br>
<br>
# removes axes, but leaves hole.<br>
fig.delaxes(fig.axes[3])<br>
<br>
# update the axes geometry information<br>
n_nonempty = len(fig.axes)  # amount of non empty plots<br>
for idx, ax in enumerate(fig.axes):<br>
    ax.change_geometry(n_nonempty, 1, idx+1)  # assuming single column layout<br>
```<br>
<br>
Hopefully this is more or less what you are looking for.<br>
<br>
Regards,<br>
Adrien<br>
<br>
Le 04/05/2021 à 23:41, Chad Parker a écrit :<br>
> All-<br>
> <br>
> 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>
> <br>
> Is there an easy way to do this? Failing that, what's the hard way (I assume it exists)?<br>
> <br>
> Thank you,<br>
> --Chad<br>
> <br>
> <br>
> # generate some data (only 2 channels/5 runs in this example)<br>
> nc, nr, nd = 2, 5, 12<br>
> data = {c: {r: [random() for x in range(nd)] for r in range(nr)} for c in range(nc)}<br>
> data[0][3] = [] # whoops, no data here.<br>
> <br>
> # save the handles here<br>
> figaxlist = []<br>
> # for each channel<br>
> for chan in data:<br>
>   nplots = len(data[chan].keys())<br>
>   # create a figure with many subplots<br>
>   fig, axs = plt.subplots(nplots, sharex=True)<br>
>   figaxlist.append((fig, axs))<br>
>   for run, ax in zip(data[chan].keys(), axs):<br>
>     # plot the data<br>
>     ax.plot(data[chan][run])<br>
> <br>
> fig, axs = figaxlist[0]<br>
> <br>
> # removes axes, but leaves hole.<br>
> fig.delaxes(fig.axes[3])<br>
> <br>
> _______________________________________________<br>
> Matplotlib-users mailing list<br>
> <a href="mailto:Matplotlib-users@python.org" target="_blank">Matplotlib-users@python.org</a><br>
> <a href="https://mail.python.org/mailman/listinfo/matplotlib-users" rel="noreferrer" target="_blank">https://mail.python.org/mailman/listinfo/matplotlib-users</a><br>
> <br>
<br>
_______________________________________________<br>
Matplotlib-users mailing list<br>
<a href="mailto:Matplotlib-users@python.org" target="_blank">Matplotlib-users@python.org</a><br>
<a href="https://mail.python.org/mailman/listinfo/matplotlib-users" rel="noreferrer" target="_blank">https://mail.python.org/mailman/listinfo/matplotlib-users</a><br>
</blockquote></div>