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

Skip to content

Commit fdb4ad3

Browse files
committed
MNT: Use a context manager to change the norm in colorbar code
This removes a deepcopy of the norm in Colorbar, instead updating the vmin/vmax via the context manager and ignoring any callback updates in the process.
1 parent 81e9559 commit fdb4ad3

File tree

1 file changed

+17
-14
lines changed

1 file changed

+17
-14
lines changed

lib/matplotlib/colorbar.py

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,13 @@
1111
End-users most likely won't need to directly use this module's API.
1212
"""
1313

14-
import copy
1514
import logging
1615
import textwrap
1716

1817
import numpy as np
1918

2019
import matplotlib as mpl
21-
from matplotlib import _api, collections, cm, colors, contour, ticker
20+
from matplotlib import _api, cbook, collections, cm, colors, contour, ticker
2221
import matplotlib.artist as martist
2322
import matplotlib.patches as mpatches
2423
import matplotlib.path as mpath
@@ -1154,20 +1153,24 @@ def _mesh(self):
11541153
These are scaled between vmin and vmax, and already handle colorbar
11551154
orientation.
11561155
"""
1157-
# copy the norm and change the vmin and vmax to the vmin and
1158-
# vmax of the colorbar, not the norm. This allows the situation
1159-
# where the colormap has a narrower range than the colorbar, to
1160-
# accommodate extra contours:
1161-
norm = copy.deepcopy(self.norm)
1162-
norm.vmin = self.vmin
1163-
norm.vmax = self.vmax
11641156
y, extendlen = self._proportional_y()
1165-
# invert:
1166-
if (isinstance(norm, (colors.BoundaryNorm, colors.NoNorm)) or
1167-
self.boundaries is not None):
1168-
y = y * (self.vmax - self.vmin) + self.vmin # not using a norm.
1157+
# Use the vmin and vmax of the colorbar, which may not be the same
1158+
# as the norm. There are situations where the colormap has a
1159+
# narrower range than the colorbar and we want to accommodate the
1160+
# extra contours.
1161+
if (isinstance(self.norm, (colors.BoundaryNorm, colors.NoNorm))
1162+
or self.boundaries is not None):
1163+
# not using a norm.
1164+
y = y * (self.vmax - self.vmin) + self.vmin
11691165
else:
1170-
y = norm.inverse(y)
1166+
# Update the norm values in a context manager as it is only
1167+
# a temporary change and we don't want to propagate any signals
1168+
# attached to the norm (callbacks.blocked).
1169+
with self.norm.callbacks.blocked(), \
1170+
cbook._setattr_cm(self.norm,
1171+
vmin=self.vmin,
1172+
vmax=self.vmax):
1173+
y = self.norm.inverse(y)
11711174
self._y = y
11721175
X, Y = np.meshgrid([0., 1.], y)
11731176
if self.orientation == 'vertical':

0 commit comments

Comments
 (0)