Comparing AxesSubplot with Axes

in order to implement some user interaction in a specific subplot that the user double clicked in, I used to be able to query the .inaxes attribute of the mouse event and compare it to a known of list of Axes to see if it matches with one of them or not (implementation here).

However, that is broken now as the MouseEvent.inaxes attribute is an AxesSubplot (as a result of call to plt.subplots()), whereas the others have are created with plt.axes() and hence are simply Axes.

I agree with this comment this AxesSubplot is a pain, for many reasons including showing up from nowhere with no docs. But for now what i need is a reliable way to perform an equality check between AxesSubplot (ASP) and Axes (AX) i.e. how do I check if ASP and AX are the same axis? I’d like a method that’s unlikely to be deprecated anytime soon.

the most natural way would have been an attribute .ax to MouseEvent.inaxes but that doesn’t exist. There is an .axes attribute but they keep returning the AxesSubplot instance instead.

So, the solution is, as AxesSubplot is going away completely, I should not use plt.subplots() (or any derivatives of pyplot really to be future-proof) that would return an AxesSubplot. Hence, I need a clear and robust alternative to

fig, axes = plt.subplots(n_rows, n_cols, figsize)

that doesn’t depend on any subplot mechanism, and returns the “unadulterated” native Axes?

I know GridSpec only deals with geometry (and not create axes), and the new mosaic compositional mechanism still returns AxesSubplots:

from matplotlib import pyplot as plt
fig = plt.figure(constrained_layout=True)
ax_dict = fig.subplot_mosaic(
    [
        ["bar", "plot"],
        ["hist", "image"],
    ],
)

ax_dict
Out[22]: 
{'bar': <AxesSubplot:label='bar'>,
 'plot': <AxesSubplot:label='plot'>,
 'hist': <AxesSubplot:label='hist'>,
 'image': <AxesSubplot:label='image'>}

I have created grids of axes before e.g. in the mrivis library that does not depend on any subplot mechanism purely using fig.add_axes() one by one, but I am hoping they may be a more native or better solution, that’ll likely to remain the codebase for a longtime to come (so I dont have to worry about deprecations and future changes)

Thanks you for help.

To be clear, what is going to be removed is the (dynamically generated) class AxesSubplot not any of the functionality (which should all still be present on Axes). The motivation here was that we had almost no use for the Axes without the geometry information so we are collapsing the class tree not removing functionality!

You may also be interested in GitHub - matplotlib/mpl-gui: Prototype for mpl-gui module .

thanks for the helpful comments and questions. I reworked the code to avoid the need to compare equality of AxesSubplot and Axes, and that works now.

i’ll take a look at mpl-gui, although truth be told, we don’t have the resources to adopt new frameworks :slight_smile: