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.
importmatplotlib.pyplotasplotimportmpl_toolkits.axes_grid1asaxes_grid1importnumpyasnpt=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()
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:
classAxesPolar(_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=axesself._aspect=aspectifaspect=="axes"andref_axisNone:
raiseValueError("ref_ax must be set when aspect='axes'")
self._ref_ax=ref_axdefget_size(self, renderer):
r1, r2=self._axes.get_ylim()
l1, l2=-r2, r2ifself._aspect=="axes": # FIXME: might need changes, tooref_aspect=_get_axes_aspect(self._ref_ax)
aspect=ref_aspect/_get_axes_aspect(self._axes)
else:
aspect=self._aspectrel_size=abs(l2-l1)*aspectabs_size=0.returnrel_size, abs_sizepad_fraction=0.5aspect=15size=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:
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:
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.)
changed the title [-]colorbar doesn't work correctly with pcolormesh and polar projection[/-][+]Handling different projections in axes_grid1.AxisDivider[/+]on Dec 29, 2022
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!
Activity
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:
Then, when appending the axes, force to have a regtangular, non-polar 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:
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
anntzer commented on Dec 29, 2022
I think the minimal repro is essentially
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.)
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!