Skip to content

Commit 7890764

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 3c67db3 commit 7890764

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
@@ -67,3 +67,9 @@ This attribute is unused and deprecated.
6767
``widgets.TextBox.params_to_disable``
6868
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
6969
This attribute is deprecated.
70+
71+
`~matplotlib.colorbar.Colorbar` methods
72+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
73+
The ``on_mappable_changed`` and ``update_bruteforce`` methods of
74+
`~matplotlib.colorbar.Colorbar` are deprecated; both can be replaced by calls
75+
to `~matplotlib.colorbar.Colorbar.update_normal`.

lib/matplotlib/colorbar.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1194,6 +1194,7 @@ def __init__(self, ax, mappable, **kw):
11941194

11951195
ColorbarBase.__init__(self, ax, **kw)
11961196

1197+
@cbook.deprecated("3.3", alternative="update_normal")
11971198
def on_mappable_changed(self, mappable):
11981199
"""
11991200
Update this colorbar to match the mappable's properties.
@@ -1234,9 +1235,8 @@ def update_normal(self, mappable):
12341235
"""
12351236
Update solid patches, lines, etc.
12361237
1237-
Unlike `.update_bruteforce`, this does not clear the axes. This is
1238-
meant to be called when the norm of the image or contour plot to which
1239-
this colorbar belongs changes.
1238+
This is meant to be called when the norm of the image or contour plot
1239+
to which this colorbar belongs changes.
12401240
12411241
If the norm on the mappable is different than before, this resets the
12421242
locator and formatter for the axis, so if these have been customized,
@@ -1259,6 +1259,7 @@ def update_normal(self, mappable):
12591259
self.add_lines(CS)
12601260
self.stale = True
12611261

1262+
@cbook.deprecated("3.3", alternative="update_normal")
12621263
def update_bruteforce(self, mappable):
12631264
"""
12641265
Destroy and rebuild the colorbar. This is
@@ -1669,7 +1670,7 @@ def colorbar_factory(cax, mappable, **kwargs):
16691670
else:
16701671
cb = Colorbar(cax, mappable, **kwargs)
16711672

1672-
cid = mappable.callbacksSM.connect('changed', cb.on_mappable_changed)
1673+
cid = mappable.callbacksSM.connect('changed', cb.update_normal)
16731674
mappable.colorbar = cb
16741675
mappable.colorbar_cid = cid
16751676

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
@@ -715,6 +715,33 @@ def add_lines(self, CS):
715715
#tlinewidths = [col.get_linewidth()[0] for lw in CS.collections]
716716
ColorbarBase.add_lines(self, CS.levels, tcolors, tlinewidths)
717717

718+
def update_normal(self, mappable):
719+
"""
720+
Update solid patches, lines, etc.
721+
722+
This is meant to be called when the norm of the image or contour plot
723+
to which this colorbar belongs changes.
724+
725+
If the norm on the mappable is different than before, this resets the
726+
locator and formatter for the axis, so if these have been customized,
727+
they will need to be customized again. However, if the norm only
728+
changes values of *vmin*, *vmax* or *cmap* then the old formatter
729+
and locator will be preserved.
730+
"""
731+
self.mappable = mappable
732+
self.set_alpha(mappable.get_alpha())
733+
self.cmap = mappable.cmap
734+
if mappable.norm != self.norm:
735+
self.norm = mappable.norm
736+
self._reset_locator_formatter_scale()
737+
738+
self.draw_all()
739+
if isinstance(self.mappable, contour.ContourSet):
740+
CS = self.mappable
741+
if not CS.filled:
742+
self.add_lines(CS)
743+
self.stale = True
744+
718745
def update_bruteforce(self, mappable):
719746
"""
720747
Update the colorbar artists to reflect the change of the

0 commit comments

Comments
 (0)