Skip to content

Colorbar cleanup. #15982

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

Merged
merged 1 commit into from
Jan 11, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions doc/api/next_api_changes/deprecations.rst
Original file line number Diff line number Diff line change
Expand Up @@ -144,3 +144,9 @@ and containment checks) via `.Line2D.set_picker` is deprecated. Use

`.Line2D.set_picker` no longer sets the artist's custom-contain() check. Use
``Line2D.set_contains`` instead.

`~matplotlib.colorbar.Colorbar` methods
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
The ``on_mappable_changed`` and ``update_bruteforce`` methods of
`~matplotlib.colorbar.Colorbar` are deprecated; both can be replaced by calls
to `~matplotlib.colorbar.Colorbar.update_normal`.
9 changes: 5 additions & 4 deletions lib/matplotlib/colorbar.py
Original file line number Diff line number Diff line change
Expand Up @@ -1209,6 +1209,7 @@ def __init__(self, ax, mappable, **kwargs):
_add_disjoint_kwargs(kwargs, alpha=mappable.get_alpha())
ColorbarBase.__init__(self, ax, **kwargs)

@cbook.deprecated("3.3", alternative="update_normal")
def on_mappable_changed(self, mappable):
"""
Update this colorbar to match the mappable's properties.
Expand Down Expand Up @@ -1249,9 +1250,8 @@ def update_normal(self, mappable):
"""
Update solid patches, lines, etc.

Unlike `.update_bruteforce`, this does not clear the axes. This is
meant to be called when the norm of the image or contour plot to which
this colorbar belongs changes.
This is meant to be called when the norm of the image or contour plot
to which this colorbar belongs changes.

If the norm on the mappable is different than before, this resets the
locator and formatter for the axis, so if these have been customized,
Expand All @@ -1274,6 +1274,7 @@ def update_normal(self, mappable):
self.add_lines(CS)
self.stale = True

@cbook.deprecated("3.3", alternative="update_normal")
def update_bruteforce(self, mappable):
"""
Destroy and rebuild the colorbar. This is
Expand Down Expand Up @@ -1684,7 +1685,7 @@ def colorbar_factory(cax, mappable, **kwargs):
else:
cb = Colorbar(cax, mappable, **kwargs)

cid = mappable.callbacksSM.connect('changed', cb.on_mappable_changed)
cid = mappable.callbacksSM.connect('changed', cb.update_normal)
mappable.colorbar = cb
mappable.colorbar_cid = cid

Expand Down
7 changes: 1 addition & 6 deletions lib/mpl_toolkits/axes_grid1/axes_grid.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,7 @@ def colorbar(self, mappable, *, ticks=None, **kwargs):
self, mappable, orientation=orientation, ticks=ticks, **kwargs)
self._config_axes()

def on_changed(m):
cb.set_cmap(m.get_cmap())
cb.set_clim(m.get_clim())
cb.update_bruteforce(m)

self.cbid = mappable.callbacksSM.connect('changed', on_changed)
self.cbid = mappable.callbacksSM.connect('changed', cb.update_normal)
mappable.colorbar = cb

if mpl.rcParams["mpl_toolkits.legacy_colorbar"]:
Expand Down
27 changes: 27 additions & 0 deletions lib/mpl_toolkits/axes_grid1/colorbar.py
Original file line number Diff line number Diff line change
Expand Up @@ -703,6 +703,33 @@ def add_lines(self, CS):
#tlinewidths = [col.get_linewidth()[0] for lw in CS.collections]
ColorbarBase.add_lines(self, CS.levels, tcolors, tlinewidths)

def update_normal(self, mappable):
"""
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess this needs some sort of test while we are here....

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Given that axes_grid1.colorbar is already deprecated, I would rather not bother...

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh I didn't catch that.

Update solid patches, lines, etc.

This is meant to be called when the norm of the image or contour plot
to which this colorbar belongs changes.

If the norm on the mappable is different than before, this resets the
locator and formatter for the axis, so if these have been customized,
they will need to be customized again. However, if the norm only
changes values of *vmin*, *vmax* or *cmap* then the old formatter
and locator will be preserved.
"""
self.mappable = mappable
self.set_alpha(mappable.get_alpha())
self.cmap = mappable.cmap
if mappable.norm != self.norm:
self.norm = mappable.norm
self._reset_locator_formatter_scale()

self.draw_all()
if isinstance(self.mappable, contour.ContourSet):
CS = self.mappable
if not CS.filled:
self.add_lines(CS)
self.stale = True

def update_bruteforce(self, mappable):
"""
Update the colorbar artists to reflect the change of the
Expand Down