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

Handling different projections in axes_grid1.AxisDivider · Issue #3286 · matplotlib/matplotlib · GitHub

Repository navigation

Handling different projections in axes_grid1.AxisDivider #3286

Open

Description

Adding a colorbar to a pcolormesh when the polar projection is specified for the axes doesn't work as expected. The code below results in a missing colorbar, and a very small plot area.

import matplotlib.pyplot as plot
import mpl_toolkits.axes_grid1 as axes_grid1
import numpy as np

t = np.linspace(0.0, 2.0 * np.pi, 360)
r = np.linspace(0,100,200)
rg, tg = np.meshgrid(r,t)
c = rg * np.sin(tg)

# If I remove the projection="polar" argument here the 
ax = plot.subplot2grid((1, 1), (0, 0), projection="polar", aspect=1.)
im = ax.pcolormesh(t, r, c.T)
divider = axes_grid1.make_axes_locatable(ax)
cax = divider.append_axes("right", size="5%", pad=0.05)
plot.colorbar(im, cax=cax)
plot.show()

Activity

  1. added this to the v1.4.x milestone on Aug 17, 2014
  2. modified the milestones: v1.4.x, on Feb 7, 2015
  3. modified the milestones: , on Jul 17, 2015
  4. modified the milestones: , on Oct 3, 2017
  5. liangwang0734 commented on Nov 4, 2017

    Update: I made a notebook to show a working case: https://gist.github.com/jimgreen/839f43897e9c1d54f6fb4b890a93ee1b . I consider it a bugfix of mpl_toolkits.axes_grid1.axes_size.AxesX and AxesY to properly support polar projection. I just checked the current source at https://github.com/matplotlib/matplotlib/blob/dde274943cd59600ae6e7ddd0c30dd55bb2a1e1f/lib/mpl_toolkits/axes_grid1/axes_size.py and the problem seems still there. I'm happy to make a commit, but perhaps someone familiar with other projections need to add support to those projections, too.

    Sorry for digging this up. I think one step (not yet the solution) is to add a new axes_grid1.axes_size child class for polar projected axes to calculate the correct size from x and y limits of polar axes, which are really theta angle and radius. The user would need to compute the colorbar from this class. For example:

    class AxesPolar(_Base):
        """
        Scaled size whose relative part corresponds to the data width
        of the *axes* multiplied by the *aspect*.
        """
        def __init__(self, axes, aspect=1., ref_ax=None):
            self._axes = axes
            self._aspect = aspect
            if aspect == "axes" and ref_ax is None:
                raise ValueError("ref_ax must be set when aspect='axes'")
            self._ref_ax = ref_ax
    
        def get_size(self, renderer):
            r1, r2 = self._axes.get_ylim()
            l1, l2 = -r2, r2
            if self._aspect == "axes": # FIXME: might need changes, too
                ref_aspect = _get_axes_aspect(self._ref_ax)
                aspect = ref_aspect/_get_axes_aspect(self._axes)
            else:
                aspect = self._aspect
    
            rel_size = abs(l2-l1)*aspect
            abs_size = 0.
            return rel_size, abs_size
    
    pad_fraction = 0.5
    aspect = 15
    size = AxesPolar(ax, aspect=1./aspect)
    pad = axes_size.Fraction(pad_fraction, size)
    divider = make_axes_locatable(ax)

    Then, when appending the axes, force to have a regtangular, non-polar axes:

    cax = divider.append_axes(position, size, pad, axes_class=matplotlib.axes._axes.Axes)

    This way, I was able to get a reasonably sized colorbar, but unfortunately the original polar mappable is still shrunk to tiny size, which is perhaps because mpl_toolkits.axes_grid1.axes_divider still assumes rectangular coordinates (as its docstring says) and further modification is required.


    Update 2017/11/4 [1]:
    The following modification to mpl_toolkits.axes_grid1.axes_divider.AxesDivider's __init__ method now gives almost what I wanted:

    import matplotlib.projections as mprojections
    
    class AxesDivider(Divider):
        """
        Divider based on the pre-existing axes.
        """
    
        def __init__(self, axes, xref=None, yref=None):
            """
            Parameters
            ----------
            axes : :class:`~matplotlib.axes.Axes`
            xref
            yref
            """
            self._axes = axes
            if xref is None:
                if isinstance(axes, mprojections.polar.PolarAxes):
                    self._xref = Size.AxesPolar(axes)
                else:
                    self._xref = Size.AxesX(axes)
            else:
                self._xref = xref
            if yref is None:
                if isinstance(axes, mprojections.polar.PolarAxes):
                    self._yref = Size.AxesPolar(axes)
                else:
                    self._yref = Size.AxesY(axes)
            else:
                self._yref = yref
    
            Divider.__init__(self, fig=axes.get_figure(), pos=None,
                             horizontal=[self._xref], vertical=[self._yref],
                             aspect=None, anchor="C")
        ...

    Update 2017/11/4 [2]:
    It might be easier and more consistent to modify axes_grid1.axes_size.AxesX and AxesY to detect and support polar projected axes. I made a notebook to show a working case: https://gist.github.com/jimgreen/839f43897e9c1d54f6fb4b890a93ee1b

  6. anntzer commented on Dec 29, 2022

    Contributor

    I think the minimal repro is essentially

    import matplotlib.pyplot as plt
    import mpl_toolkits.axes_grid1 as axes_grid1
    
    ax = plt.figure().add_subplot(aspect=1, projection="polar")
    axes_grid1.make_axes_locatable(ax)
    plt.show()

    where the locator instantiated by make_axes_locatable doesn't handle polar axes properly; everything else follows. (append_axes cannot guess that you want to add a rectilinear axes as it doesn't know yet it'll be used as a colorbar, so I think having to provide the axes class explicitly seems OK.)

  7. changed the title [-]colorbar doesn't work correctly with pcolormesh and polar projection[/-] [+]Handling different projections in axes_grid1.AxisDivider[/+] on Dec 29, 2022
  8. github-actions commented on Jan 1, 2024

    This issue has been marked "inactive" because it has been 365 days since the last comment. If this issue is still present in recent Matplotlib releases, or the feature request is still wanted, please leave a comment and this label will be removed. If there are no updates in another 30 days, this issue will be automatically closed, but you are free to re-open or create a new issue if needed. We value issue reports, and this procedure is meant to help us resurface and prioritize issues that have not been addressed yet, not make them disappear. Thanks for your help!

  9. added
    keepItems to be ignored by the “Stale” Github Action
    and removed
    status: inactiveMarked by the “Stale” Github Action
    on Jan 2, 2024
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

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions


      Back | FazBrowse Home | New Git URL