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

Skip to content

Commit 9cba6fb

Browse files
committed
contour: clarify and improve support for norm.vmin, norm.vmax
Previously, although vmin, vmax could be specified manually in a norm used to initialize a ContourSet, the values would be used only if both were specified. Now one can set either or both of vmin, vmax in the norm, and they will take precedence over the default.
1 parent 8c582e4 commit 9cba6fb

File tree

1 file changed

+48
-24
lines changed

1 file changed

+48
-24
lines changed

lib/matplotlib/contour.py

Lines changed: 48 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -682,7 +682,7 @@ class ContourSet(cm.ScalarMappable, ContourLabeler):
682682
683683
layers:
684684
same as levels for line contours; half-way between
685-
levels for filled contours. See _process_colors method.
685+
levels for filled contours. See :meth:`_process_colors`.
686686
"""
687687
def __init__(self, ax, *args, **kwargs):
688688
"""
@@ -1029,40 +1029,65 @@ def _contour_level_args(self, z, args):
10291029
raise ValueError("Filled contours require at least 2 levels.")
10301030

10311031
def _process_levels(self):
1032-
# Color mapping range (norm vmin, vmax) is based on levels.
1032+
"""
1033+
Assign values to :attr:`layers` based on :attr:`levels`,
1034+
adding extended layers as needed if contours are filled.
1035+
1036+
For line contours, layers simply coincide with levels;
1037+
a line is a thin layer. No extended levels are needed
1038+
with line contours.
1039+
"""
1040+
# The following attributes are no longer needed, and
1041+
# should be deprecated and removed to reduce confusion.
10331042
self.vmin = np.amin(self.levels)
10341043
self.vmax = np.amax(self.levels)
1035-
# Make a private _levels to include extended regions.
1044+
1045+
# Make a private _levels to include extended regions; we
1046+
# want to leave the original levels attribute unchanged.
1047+
# (Colorbar needs this even for line contours.)
10361048
self._levels = list(self.levels)
1049+
1050+
if not self.filled:
1051+
self.layers = self.levels
1052+
return
1053+
10371054
if self.extend in ('both', 'min'):
10381055
self._levels.insert(0, min(self.levels[0],self.zmin) - 1)
10391056
if self.extend in ('both', 'max'):
10401057
self._levels.append(max(self.levels[-1],self.zmax) + 1)
10411058
self._levels = np.asarray(self._levels)
1042-
if self.filled:
1043-
# layer values are mid-way between levels
1044-
self.layers = 0.5 * (self._levels[:-1] + self._levels[1:])
1045-
# ...except that extended layers must be outside the
1046-
# normed range:
1047-
if self.extend in ('both', 'min'):
1048-
self.layers[0] = -np.inf
1049-
if self.extend in ('both', 'max'):
1050-
self.layers[-1] = np.inf
1051-
else:
1052-
self.layers = self.levels # contour: a line is a thin layer
1053-
# Use only original levels--no extended levels
1059+
1060+
# layer values are mid-way between levels
1061+
self.layers = 0.5 * (self._levels[:-1] + self._levels[1:])
1062+
# ...except that extended layers must be outside the
1063+
# normed range:
1064+
if self.extend in ('both', 'min'):
1065+
self.layers[0] = -np.inf
1066+
if self.extend in ('both', 'max'):
1067+
self.layers[-1] = np.inf
10541068

10551069
def _process_colors(self):
10561070
"""
10571071
Color argument processing for contouring.
10581072
1059-
Note that we base the color mapping on the contour levels,
1060-
not on the actual range of the Z values. This means we
1061-
don't have to worry about bad values in Z, and we always have
1062-
the full dynamic range available for the selected levels.
1073+
Note that we base the color mapping on the contour levels
1074+
and layers, not on the actual range of the Z values. This
1075+
means we don't have to worry about bad values in Z, and we
1076+
always have the full dynamic range available for the selected
1077+
levels.
10631078
10641079
The color is based on the midpoint of the layer, except for
1065-
an extended end layers.
1080+
extended end layers. By default, the norm vmin and vmax
1081+
are the extreme values of the non-extended levels. Hence,
1082+
the layer color extremes are not the extreme values of
1083+
the colormap itself, but approach those values as the number
1084+
of levels increases. An advantage of this scheme is that
1085+
line contours, when added to filled contours, take on
1086+
colors that are consistent with those of the filled regions;
1087+
for example, a contour line on the boundary between two
1088+
regions will have a color intermediate between the those
1089+
of the regions.
1090+
10661091
"""
10671092
self.monochrome = self.cmap.monochrome
10681093
if self.colors is not None:
@@ -1079,12 +1104,11 @@ def _process_colors(self):
10791104
self.set_norm(colors.NoNorm())
10801105
else:
10811106
self.cvalues = self.layers
1082-
if not self.norm.scaled():
1083-
self.set_clim(self.vmin, self.vmax)
1107+
self.set_array(self.levels)
1108+
self.autoscale_None()
10841109
if self.extend in ('both', 'max', 'min'):
10851110
self.norm.clip = False
1086-
self.set_array(self.layers) # Required by colorbar, but not
1087-
# actually used.
1111+
10881112
# self.tcolors are set by the "changed" method
10891113

10901114
def _process_linewidths(self):

0 commit comments

Comments
 (0)