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

Skip to content

Commit ffaa6b4

Browse files
committed
Multivariate plots for imshow, pcolor and pcolormesh
1 parent 9aef880 commit ffaa6b4

21 files changed

Lines changed: 962 additions & 69 deletions

lib/matplotlib/axes/_axes.py

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6165,6 +6165,21 @@ def pcolor(self, *args, shading=None, alpha=None, norm=None, cmap=None,
61656165
if shading is None:
61666166
shading = mpl.rcParams['pcolor.shading']
61676167
shading = shading.lower()
6168+
6169+
mcolorizer.ColorizingArtist._check_exclusionary_keywords(colorizer,
6170+
vmin=vmin,
6171+
vmax=vmax)
6172+
6173+
# we need to get the colorizer object to know the number of
6174+
# n_variates that should exist in the array, we therefore get the
6175+
# colorizer here.
6176+
colorizer = mcolorizer.ColorizingArtist._get_colorizer(norm=norm,
6177+
cmap=cmap,
6178+
colorizer=colorizer)
6179+
if colorizer.norm.n_input > 1:
6180+
data = mcolorizer._ensure_multivariate_data(colorizer.norm.n_input, args[0])
6181+
args = (data, *args[1:])
6182+
61686183
X, Y, C, shading = self._pcolorargs('pcolor', *args, shading=shading,
61696184
kwargs=kwargs)
61706185
linewidths = (0.25,)
@@ -6201,9 +6216,8 @@ def pcolor(self, *args, shading=None, alpha=None, norm=None, cmap=None,
62016216
coords = stack([X, Y], axis=-1)
62026217

62036218
collection = mcoll.PolyQuadMesh(
6204-
coords, array=C, cmap=cmap, norm=norm, colorizer=colorizer,
6219+
coords, array=C, colorizer=colorizer,
62056220
alpha=alpha, **kwargs)
6206-
collection._check_exclusionary_keywords(colorizer, vmin=vmin, vmax=vmax)
62076221
collection._scale_norm(norm, vmin, vmax)
62086222

62096223
# Transform from native to data coordinates?
@@ -6428,6 +6442,20 @@ def pcolormesh(self, *args, alpha=None, norm=None, cmap=None, vmin=None,
64286442
shading = shading.lower()
64296443
kwargs.setdefault('edgecolors', 'none')
64306444

6445+
mcolorizer.ColorizingArtist._check_exclusionary_keywords(colorizer,
6446+
vmin=vmin,
6447+
vmax=vmax)
6448+
# we need to get the colorizer object to know the number of
6449+
# n_variates that should exist in the array, we therefore get the
6450+
# colorizer here.
6451+
colorizer_obj = mcolorizer.ColorizingArtist._get_colorizer(norm=norm,
6452+
cmap=cmap,
6453+
colorizer=colorizer)
6454+
if colorizer_obj.norm.n_input > 1:
6455+
data = mcolorizer._ensure_multivariate_data(colorizer_obj.norm.n_input,
6456+
args[-1])
6457+
args = (*args[:-1], data)
6458+
64316459
X, Y, C, shading = self._pcolorargs('pcolormesh', *args,
64326460
shading=shading, kwargs=kwargs)
64336461
coords = np.stack([X, Y], axis=-1)
@@ -6436,8 +6464,8 @@ def pcolormesh(self, *args, alpha=None, norm=None, cmap=None, vmin=None,
64366464

64376465
collection = mcoll.QuadMesh(
64386466
coords, antialiased=antialiased, shading=shading,
6439-
array=C, cmap=cmap, norm=norm, colorizer=colorizer, alpha=alpha, **kwargs)
6440-
collection._check_exclusionary_keywords(colorizer, vmin=vmin, vmax=vmax)
6467+
array=C, colorizer=colorizer_obj,
6468+
alpha=alpha, **kwargs)
64416469
collection._scale_norm(norm, vmin, vmax)
64426470

64436471
coords = coords.reshape(-1, 2) # flatten the grid structure; keep x, y

lib/matplotlib/cbook.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -690,7 +690,17 @@ def safe_masked_invalid(x, copy=False):
690690
try:
691691
xm = np.ma.masked_where(~(np.isfinite(x)), x, copy=False)
692692
except TypeError:
693-
return x
693+
if len(x.dtype.descr) == 1:
694+
return x
695+
else:
696+
# in case of a dtype with multiple fields:
697+
try:
698+
mask = np.empty(x.shape, dtype=np.dtype('bool, '*len(x.dtype.descr)))
699+
for dd, dm in zip(x.dtype.descr, mask.dtype.descr):
700+
mask[dm[0]] = ~(np.isfinite(x[dd[0]]))
701+
xm = np.ma.array(x, mask=mask, copy=False)
702+
except TypeError:
703+
return x
694704
return xm
695705

696706

lib/matplotlib/cm.py

Lines changed: 0 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -279,32 +279,3 @@ def get_cmap(name=None, lut=None):
279279
return _colormaps[name]
280280
else:
281281
return _colormaps[name].resampled(lut)
282-
283-
284-
def _ensure_cmap(cmap):
285-
"""
286-
Ensure that we have a `.Colormap` object.
287-
288-
For internal use to preserve type stability of errors.
289-
290-
Parameters
291-
----------
292-
cmap : None, str, Colormap
293-
294-
- if a `Colormap`, return it
295-
- if a string, look it up in mpl.colormaps
296-
- if None, look up the default color map in mpl.colormaps
297-
298-
Returns
299-
-------
300-
Colormap
301-
302-
"""
303-
if isinstance(cmap, colors.Colormap):
304-
return cmap
305-
cmap_name = cmap if cmap is not None else mpl.rcParams["image.cmap"]
306-
# use check_in_list to ensure type stability of the exception raised by
307-
# the internal usage of this (ValueError vs KeyError)
308-
if cmap_name not in _colormaps:
309-
_api.check_in_list(sorted(_colormaps), cmap=cmap_name)
310-
return mpl.colormaps[cmap_name]

lib/matplotlib/collections.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2523,10 +2523,16 @@ def _get_unmasked_polys(self):
25232523
arr = self.get_array()
25242524
if arr is not None:
25252525
arr = np.ma.getmaskarray(arr)
2526-
if arr.ndim == 3:
2526+
if self.norm.n_input > 1:
2527+
# multivar case
2528+
for a in mcolors.MultiNorm._iterable_variates_in_data(
2529+
arr, self.norm.n_input):
2530+
mask |= np.any(a, axis=0)
2531+
elif arr.ndim == 3:
25272532
# RGB(A) case
25282533
mask |= np.any(arr, axis=-1)
25292534
elif arr.ndim == 2:
2535+
# scalar case
25302536
mask |= arr
25312537
else:
25322538
mask |= arr.reshape(self._coordinates[:-1, :-1, :].shape[:2])

0 commit comments

Comments
 (0)