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

Skip to content

Commit 5a0b306

Browse files
committed
Colorbar cleanup.
Deprecate on_mappable_changed in favor of update_normal (they're the same now (except for a logging call) now that colorbars just use the norm of the mappable). Deprecate update_bruteforce in favor of update_normal -- it's not used anywhere except in axes_grid, but that's just because whoever introduced update_normal forgot to update axes_grid at the same time. axes_grid.colorbar is already deprecated but until its complete removal, we need to backport update_normal to it...
1 parent aabbb74 commit 5a0b306

File tree

4 files changed

+39
-10
lines changed

4 files changed

+39
-10
lines changed

doc/api/next_api_changes/deprecations.rst

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,3 +144,9 @@ and containment checks) via `.Line2D.set_picker` is deprecated. Use
144144

145145
`.Line2D.set_picker` no longer sets the artist's custom-contain() check. Use
146146
``Line2D.set_contains`` instead.
147+
148+
`~matplotlib.colorbar.Colorbar` methods
149+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
150+
The ``on_mappable_changed`` and ``update_bruteforce`` methods of
151+
`~matplotlib.colorbar.Colorbar` are deprecated; both can be replaced by calls
152+
to `~matplotlib.colorbar.Colorbar.update_normal`.

lib/matplotlib/colorbar.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1209,6 +1209,7 @@ def __init__(self, ax, mappable, **kwargs):
12091209
_add_disjoint_kwargs(kwargs, alpha=mappable.get_alpha())
12101210
ColorbarBase.__init__(self, ax, **kwargs)
12111211

1212+
@cbook.deprecated("3.3", alternative="update_normal")
12121213
def on_mappable_changed(self, mappable):
12131214
"""
12141215
Update this colorbar to match the mappable's properties.
@@ -1249,9 +1250,8 @@ def update_normal(self, mappable):
12491250
"""
12501251
Update solid patches, lines, etc.
12511252
1252-
Unlike `.update_bruteforce`, this does not clear the axes. This is
1253-
meant to be called when the norm of the image or contour plot to which
1254-
this colorbar belongs changes.
1253+
This is meant to be called when the norm of the image or contour plot
1254+
to which this colorbar belongs changes.
12551255
12561256
If the norm on the mappable is different than before, this resets the
12571257
locator and formatter for the axis, so if these have been customized,
@@ -1274,6 +1274,7 @@ def update_normal(self, mappable):
12741274
self.add_lines(CS)
12751275
self.stale = True
12761276

1277+
@cbook.deprecated("3.3", alternative="update_normal")
12771278
def update_bruteforce(self, mappable):
12781279
"""
12791280
Destroy and rebuild the colorbar. This is
@@ -1684,7 +1685,7 @@ def colorbar_factory(cax, mappable, **kwargs):
16841685
else:
16851686
cb = Colorbar(cax, mappable, **kwargs)
16861687

1687-
cid = mappable.callbacksSM.connect('changed', cb.on_mappable_changed)
1688+
cid = mappable.callbacksSM.connect('changed', cb.update_normal)
16881689
mappable.colorbar = cb
16891690
mappable.colorbar_cid = cid
16901691

lib/mpl_toolkits/axes_grid1/axes_grid.py

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -44,12 +44,7 @@ def colorbar(self, mappable, *, ticks=None, **kwargs):
4444
self, mappable, orientation=orientation, ticks=ticks, **kwargs)
4545
self._config_axes()
4646

47-
def on_changed(m):
48-
cb.set_cmap(m.get_cmap())
49-
cb.set_clim(m.get_clim())
50-
cb.update_bruteforce(m)
51-
52-
self.cbid = mappable.callbacksSM.connect('changed', on_changed)
47+
self.cbid = mappable.callbacksSM.connect('changed', cb.update_normal)
5348
mappable.colorbar = cb
5449

5550
if mpl.rcParams["mpl_toolkits.legacy_colorbar"]:

lib/mpl_toolkits/axes_grid1/colorbar.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -703,6 +703,33 @@ def add_lines(self, CS):
703703
#tlinewidths = [col.get_linewidth()[0] for lw in CS.collections]
704704
ColorbarBase.add_lines(self, CS.levels, tcolors, tlinewidths)
705705

706+
def update_normal(self, mappable):
707+
"""
708+
Update solid patches, lines, etc.
709+
710+
This is meant to be called when the norm of the image or contour plot
711+
to which this colorbar belongs changes.
712+
713+
If the norm on the mappable is different than before, this resets the
714+
locator and formatter for the axis, so if these have been customized,
715+
they will need to be customized again. However, if the norm only
716+
changes values of *vmin*, *vmax* or *cmap* then the old formatter
717+
and locator will be preserved.
718+
"""
719+
self.mappable = mappable
720+
self.set_alpha(mappable.get_alpha())
721+
self.cmap = mappable.cmap
722+
if mappable.norm != self.norm:
723+
self.norm = mappable.norm
724+
self._reset_locator_formatter_scale()
725+
726+
self.draw_all()
727+
if isinstance(self.mappable, contour.ContourSet):
728+
CS = self.mappable
729+
if not CS.filled:
730+
self.add_lines(CS)
731+
self.stale = True
732+
706733
def update_bruteforce(self, mappable):
707734
"""
708735
Update the colorbar artists to reflect the change of the

0 commit comments

Comments
 (0)