FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

Add ability to pass different kwargs when adding different subplots simultaneously through`plt.subplots` · Issue #20392 · matplotlib/matplotlib · GitHub

Repository navigation

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

Closed

Description

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

Activity

  1. jklymak commented on Jun 8, 2021

    Member

    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.

  2. phinate commented on Jun 8, 2021

    Author

    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.

  3. jklymak commented on Jun 8, 2021

    Member

    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()
  4. phinate commented on Jun 8, 2021

    Author

    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!

  5. jklymak commented on Jun 8, 2021

    Member

    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

  6. jklymak commented on Jun 8, 2021

    Member

    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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions


      Back | FazBrowse Home | New Git URL