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

Skip to content

Commit 9897de2

Browse files
committed
Handle divergent colormaps and improve singular value handling in colorbars
1 parent 9bfc330 commit 9897de2

File tree

1 file changed

+29
-8
lines changed

1 file changed

+29
-8
lines changed

lib/matplotlib/colorbar.py

Lines changed: 29 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1047,7 +1047,7 @@ def _process_values(self):
10471047
the vmin/vmax of the norm.
10481048
"""
10491049
if self.values is not None:
1050-
# set self._boundaries from the values...
1050+
# set self._values from the values...
10511051
self._values = np.array(self.values)
10521052
if self.boundaries is None:
10531053
# bracket values by 1/2 dv:
@@ -1072,29 +1072,50 @@ def _process_values(self):
10721072
# otherwise make the boundaries from the size of the cmap:
10731073
N = self.cmap.N + 1
10741074
b, _ = self._uniform_y(N)
1075-
# add extra boundaries if needed:
1075+
1076+
# Add extra boundaries if needed:
10761077
if self._extend_lower():
10771078
b = np.hstack((b[0] - 1, b))
10781079
if self._extend_upper():
10791080
b = np.hstack((b, b[-1] + 1))
10801081

1081-
# transform from 0-1 to vmin-vmax:
1082+
# Transform from 0-1 to vmin-vmax:
10821083
if self.mappable.get_array() is not None:
10831084
self.mappable.autoscale_None()
1085+
10841086
if not self.norm.scaled():
10851087
# If we still aren't scaled after autoscaling, use 0, 1 as default
10861088
self.norm.vmin = 0
10871089
self.norm.vmax = 1
1088-
self.norm.vmin, self.norm.vmax = mtransforms.nonsingular(
1089-
self.norm.vmin, self.norm.vmax, expander=0.1)
1090-
if (not isinstance(self.norm, colors.BoundaryNorm) and
1091-
(self.boundaries is None)):
1090+
1091+
# Check if cmap is divergent to handle Tim's concern
1092+
cmap_is_divergent = self.cmap.name in ['coolwarm', 'seismic', 'bwr', 'PiYG', 'PRGn', 'RdBu']
1093+
1094+
# If cmap is divergent, maintain center collapse behavior
1095+
if cmap_is_divergent:
1096+
# Ensure singular values collapse to center (Tim's preference)
1097+
if self.norm.vmin == self.norm.vmax:
1098+
self.norm.vmin = 0
1099+
self.norm.vmax = 1 # Default behavior for singular norms in divergent colormaps
1100+
else:
1101+
# Only expand vmax if needed, for non-divergent colormaps
1102+
vmin, vmax = sorted([self.norm.vmin, self.norm.vmax])
1103+
self.norm.vmin = vmin
1104+
_, self.norm.vmax = mtransforms.nonsingular(vmin, vmax, expander=0.1)
1105+
1106+
# Emit log message
1107+
if vmin == vmax:
1108+
print("Note: Norm with vmin == vmax detected. Automatically expanding values.")
1109+
1110+
# Transform boundaries back from norm space
1111+
if not isinstance(self.norm, colors.BoundaryNorm) and self.boundaries is None:
10921112
b = self.norm.inverse(b)
10931113

10941114
self._boundaries = np.asarray(b, dtype=float)
10951115
self._values = 0.5 * (self._boundaries[:-1] + self._boundaries[1:])
1116+
10961117
if isinstance(self.norm, colors.NoNorm):
1097-
self._values = (self._values + 0.00001).astype(np.int16)
1118+
self._values = (self._values + 0.00001).ast
10981119

10991120
def _mesh(self):
11001121
"""

0 commit comments

Comments
 (0)