Add ability to pass different kwargs when adding different subplots simultaneously through`plt.subplots` · Issue #20392 · matplotlib/matplotlib · GitHub
I was just messing aroung with trying to see if I could use the (super nice!) plt.subplot_mosaic function with a mixture of 2d and 3d axes, but didn't get anywhere. Looking into it, it seems like it could be possible with subplot_kw = {"projection": "3d"}, but this is static across the different subplots (or more accurately, calls to plt.add_subplot).
Proposed Solution
From looking at the source code (specifically at these line in gridspec.py, since that's where I believe the function calls eventually end up) it seems like it may be possible to package subplot_kws in an object of size _nrows x _ncols, since that is the dimensions iterated over.
The only part that's unclear to me from an implementation standpoint is whether this would necessitate a new kwarg itself, or perhaps one can use single dispatch or conditional logic to handle the case where subplot_kw is not dict-like.
Happy to have a go if this is desired and/or the functionality exists already and I'm blindsighted to it :)
Additional context and prior art
Could be many scenarios where one would want to control each subplot! I think it would be super neat to be able to do all this from one call to plt.subplots :D
We have a way to do this already, which is to make the gridspec and then add the axes individually. I'm not sure how constructing an array of dicts is a preferable interface.
That's fair, though that gets tedious for complicated layouts -- helper functions in plt.subplots and plt.subplot_mosaic are the starting point for those kind of tasks, and I thought it could be useful to have that built in, since I didn't find a way to quickly modify one individual subplot to change its projection to 3d.
Activity
jklymak commented on Jun 8, 2021
We have a way to do this already, which is to make the gridspec and then add the axes individually. I'm not sure how constructing an array of dicts is a preferable interface.
phinate commented on Jun 8, 2021
That's fair, though that gets tedious for complicated layouts -- helper functions in plt.subplots and plt.subplot_mosaic are the starting point for those kind of tasks, and I thought it could be useful to have that built in, since I didn't find a way to quickly modify one individual subplot to change its projection to 3d.
jklymak commented on Jun 8, 2021
You cannot change an axes' projection trivially. However, you can get the subplotspec and make a new axes like:
phinate commented on Jun 8, 2021
Ah, this makes a lot of sense! That covers my particular use case, thanks :)
Happy for the issue to be closed if you don't deem the original post to be an appropriate feature, but also open to more discussion!
jklymak commented on Jun 8, 2021
I guess I'm somewhat against trying to make the API for subplot_mosaic too complicated.
Note we also have subfigures, which is another way of dealing with different projections in the same figure: https://matplotlib.org/stable/gallery/subplots_axes_and_figures/subfigures.html
jklymak commented on Jun 8, 2021
I'll close here, and we can continue discussion on discourse.matplotlib.org... https://discourse.matplotlib.org/t/modifying-an-axessubplot-to-be-3d-in-the-context-of-plt-subplot-mosaic/22158