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

Skip to content

Commit f2fa21b

Browse files
committed
Add unit conversion for pcolor methods
1 parent 0ccb72b commit f2fa21b

File tree

1 file changed

+31
-8
lines changed

1 file changed

+31
-8
lines changed

lib/matplotlib/axes/_axes.py

Lines changed: 31 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
from numpy import ma
99

1010
import matplotlib as mpl
11+
import matplotlib.cm as cm
1112
import matplotlib.category # Register category unit converter as side effect.
1213
import matplotlib.cbook as cbook
1314
import matplotlib.collections as mcoll
@@ -5838,12 +5839,28 @@ def imshow(self, X, cmap=None, norm=None, *, aspect=None,
58385839
self.add_image(im)
58395840
return im
58405841

5842+
@staticmethod
5843+
def _convert_C_units(C):
5844+
"""
5845+
Remove any units attached to C, and return the units and converter used to do
5846+
the conversion.
5847+
"""
5848+
sm = cm.ScalarMappable()
5849+
C = sm._strip_units(C)
5850+
converter = sm._converter
5851+
units = sm._units
5852+
5853+
C = np.asanyarray(C)
5854+
C = cbook.safe_masked_invalid(C, copy=True)
5855+
return C, units, converter
5856+
58415857
def _pcolorargs(self, funcname, *args, shading='auto', **kwargs):
58425858
# - create X and Y if not present;
58435859
# - reshape X and Y as needed if they are 1-D;
58445860
# - check for proper sizes based on `shading` kwarg;
58455861
# - reset shading if shading='auto' to flat or nearest
58465862
# depending on size;
5863+
# - if C has units, get the converter
58475864

58485865
_valid_shading = ['gouraud', 'nearest', 'flat', 'auto']
58495866
try:
@@ -5855,19 +5872,19 @@ def _pcolorargs(self, funcname, *args, shading='auto', **kwargs):
58555872
shading = 'auto'
58565873

58575874
if len(args) == 1:
5858-
C = np.asanyarray(args[0])
5875+
C, units, converter = self._convert_C_units(args[0])
58595876
nrows, ncols = C.shape[:2]
58605877
if shading in ['gouraud', 'nearest']:
58615878
X, Y = np.meshgrid(np.arange(ncols), np.arange(nrows))
58625879
else:
58635880
X, Y = np.meshgrid(np.arange(ncols + 1), np.arange(nrows + 1))
58645881
shading = 'flat'
58655882
C = cbook.safe_masked_invalid(C, copy=True)
5866-
return X, Y, C, shading
5883+
return X, Y, C, shading, units, converter
58675884

58685885
if len(args) == 3:
58695886
# Check x and y for bad data...
5870-
C = np.asanyarray(args[2])
5887+
C, units, converter = self._convert_C_units(args[2])
58715888
# unit conversion allows e.g. datetime objects as axis values
58725889
X, Y = args[:2]
58735890
X, Y = self._process_unit_info([("x", X), ("y", Y)], kwargs)
@@ -5948,7 +5965,7 @@ def _interp_grid(X):
59485965
shading = 'flat'
59495966

59505967
C = cbook.safe_masked_invalid(C, copy=True)
5951-
return X, Y, C, shading
5968+
return X, Y, C, shading, units, converter
59525969

59535970
@_preprocess_data()
59545971
@_docstring.dedent_interpd
@@ -6100,8 +6117,9 @@ def pcolor(self, *args, shading=None, alpha=None, norm=None, cmap=None,
61006117
if shading is None:
61016118
shading = mpl.rcParams['pcolor.shading']
61026119
shading = shading.lower()
6103-
X, Y, C, shading = self._pcolorargs('pcolor', *args, shading=shading,
6104-
kwargs=kwargs)
6120+
X, Y, C, shading, units, converter = self._pcolorargs(
6121+
'pcolor', *args, shading=shading, kwargs=kwargs
6122+
)
61056123
linewidths = (0.25,)
61066124
if 'linewidth' in kwargs:
61076125
kwargs['linewidths'] = kwargs.pop('linewidth')
@@ -6137,6 +6155,8 @@ def pcolor(self, *args, shading=None, alpha=None, norm=None, cmap=None,
61376155

61386156
collection = mcoll.PolyQuadMesh(
61396157
coords, array=C, cmap=cmap, norm=norm, alpha=alpha, **kwargs)
6158+
collection._units = units
6159+
collection._converter = converter
61406160
collection._scale_norm(norm, vmin, vmax)
61416161

61426162
# Transform from native to data coordinates?
@@ -6356,15 +6376,18 @@ def pcolormesh(self, *args, alpha=None, norm=None, cmap=None, vmin=None,
63566376
shading = shading.lower()
63576377
kwargs.setdefault('edgecolors', 'none')
63586378

6359-
X, Y, C, shading = self._pcolorargs('pcolormesh', *args,
6360-
shading=shading, kwargs=kwargs)
6379+
X, Y, C, shading, units, converter = self._pcolorargs(
6380+
'pcolormesh', *args, shading=shading, kwargs=kwargs
6381+
)
63616382
coords = np.stack([X, Y], axis=-1)
63626383

63636384
kwargs.setdefault('snap', mpl.rcParams['pcolormesh.snap'])
63646385

63656386
collection = mcoll.QuadMesh(
63666387
coords, antialiased=antialiased, shading=shading,
63676388
array=C, cmap=cmap, norm=norm, alpha=alpha, **kwargs)
6389+
collection._units = units
6390+
collection._converter = converter
63686391
collection._scale_norm(norm, vmin, vmax)
63696392

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

0 commit comments

Comments
 (0)