Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Add ability to pass different kwargs when adding different subplots simultaneously throughplt.subplots #20392

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
phinate opened this issue Jun 8, 2021 · 6 comments

Comments

@phinate
Copy link

phinate commented Jun 8, 2021

Problem

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.

# Create array to hold all axes.
axarr = np.empty((self._nrows, self._ncols), dtype=object)
for row in range(self._nrows):
for col in range(self._ncols):
shared_with = {"none": None, "all": axarr[0, 0],
"row": axarr[row, 0], "col": axarr[0, col]}
subplot_kw["sharex"] = shared_with[sharex]
subplot_kw["sharey"] = shared_with[sharey]
axarr[row, col] = figure.add_subplot(
self[row, col], **subplot_kw)

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

@jklymak
Copy link
Member

jklymak commented 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
Copy link
Author

phinate commented 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
Copy link
Member

jklymak commented Jun 8, 2021

You cannot change an axes' projection trivially. However, you can get the subplotspec and make a new axes like:

import matplotlib.pyplot as plt 

fig = plt.figure()
axs = fig.subplot_mosaic([['A', 'B'], ['C', 'B']])
ss = axs['B'].get_subplotspec()
axs['B'].remove()
axs['B'] = fig.add_subplot(ss, projection='3d')
plt.show()

@phinate
Copy link
Author

phinate commented 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
Copy link
Member

jklymak commented 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
Copy link
Member

jklymak commented 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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants