Reputation: 333
I have a function that returns the axes on a figure I created using subplot and gridspecs. It raises a MatplotlibDeprecationWarning and I'd like to understand how I should be doing this. Can anyone help?
def get_gridspec():
fig10 = plt.figure(constrained_layout=True)
gs0 = fig10.add_gridspec(1, 2)
loss_gs = gs0[0].subgridspec(1, 1)
graphs_gs = gs0[1].subgridspec(2, 1)
fig10.add_subplot(loss_gs[0])
for dataset in range(2):
for irow_metric in range(2):
for jrow_metric in range(1):
fig10.add_subplot(graphs_gs[irow_metric, jrow_metric])
return fig10.axes
Raises:
MatplotlibDeprecationWarning: Adding an axes using the same arguments as a previous axes currently reuses the earlier instance. In a future version, a new instance will always be created and returned. Meanwhile, this warning can be suppressed, and the future behavior ensured, by passing a unique label to each axes instance.
fig10.add_subplot(graphs_gs[irow_metric, jrow_metric])
Upvotes: 0
Views: 3144
Reputation: 5913
Yes, that is a little awkward. We are working on a "subfigure" or "subpanel" metaphor that will be less annoying (https://github.com/matplotlib/matplotlib/issues/17375)
OTOH, there is, as of recent matplotlib, a subplots
method on gridspec
objects (https://matplotlib.org/api/_as_gen/matplotlib.gridspec.GridSpecBase.html#matplotlib.gridspec.GridSpecBase.subplots) that will create the subplots on the figure associated with the gridspec:
import matplotlib.pyplot as plt
fig10 = plt.figure(constrained_layout=True)
gs0 = fig10.add_gridspec(1, 2)
loss_axs = gs0[0].subgridspec(1, 1).subplots()
graphs_axs = gs0[1].subgridspec(2, 1).subplots()
BTW the deprecation warning is because of your outer loop - you are "creating" the axes twice, which is deprecated.
Upvotes: 2