8
8
from numpy import ma
9
9
10
10
import matplotlib as mpl
11
+ import matplotlib .cm as cm
11
12
import matplotlib .category # Register category unit converter as side effect.
12
13
import matplotlib .cbook as cbook
13
14
import matplotlib .collections as mcoll
@@ -5838,12 +5839,28 @@ def imshow(self, X, cmap=None, norm=None, *, aspect=None,
5838
5839
self .add_image (im )
5839
5840
return im
5840
5841
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
+
5841
5857
def _pcolorargs (self , funcname , * args , shading = 'auto' , ** kwargs ):
5842
5858
# - create X and Y if not present;
5843
5859
# - reshape X and Y as needed if they are 1-D;
5844
5860
# - check for proper sizes based on `shading` kwarg;
5845
5861
# - reset shading if shading='auto' to flat or nearest
5846
5862
# depending on size;
5863
+ # - if C has units, get the converter
5847
5864
5848
5865
_valid_shading = ['gouraud' , 'nearest' , 'flat' , 'auto' ]
5849
5866
try :
@@ -5855,19 +5872,19 @@ def _pcolorargs(self, funcname, *args, shading='auto', **kwargs):
5855
5872
shading = 'auto'
5856
5873
5857
5874
if len (args ) == 1 :
5858
- C = np . asanyarray (args [0 ])
5875
+ C , units , converter = self . _convert_C_units (args [0 ])
5859
5876
nrows , ncols = C .shape [:2 ]
5860
5877
if shading in ['gouraud' , 'nearest' ]:
5861
5878
X , Y = np .meshgrid (np .arange (ncols ), np .arange (nrows ))
5862
5879
else :
5863
5880
X , Y = np .meshgrid (np .arange (ncols + 1 ), np .arange (nrows + 1 ))
5864
5881
shading = 'flat'
5865
5882
C = cbook .safe_masked_invalid (C , copy = True )
5866
- return X , Y , C , shading
5883
+ return X , Y , C , shading , units , converter
5867
5884
5868
5885
if len (args ) == 3 :
5869
5886
# Check x and y for bad data...
5870
- C = np . asanyarray (args [2 ])
5887
+ C , units , converter = self . _convert_C_units (args [2 ])
5871
5888
# unit conversion allows e.g. datetime objects as axis values
5872
5889
X , Y = args [:2 ]
5873
5890
X , Y = self ._process_unit_info ([("x" , X ), ("y" , Y )], kwargs )
@@ -5948,7 +5965,7 @@ def _interp_grid(X):
5948
5965
shading = 'flat'
5949
5966
5950
5967
C = cbook .safe_masked_invalid (C , copy = True )
5951
- return X , Y , C , shading
5968
+ return X , Y , C , shading , units , converter
5952
5969
5953
5970
@_preprocess_data ()
5954
5971
@_docstring .dedent_interpd
@@ -6100,8 +6117,9 @@ def pcolor(self, *args, shading=None, alpha=None, norm=None, cmap=None,
6100
6117
if shading is None :
6101
6118
shading = mpl .rcParams ['pcolor.shading' ]
6102
6119
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
+ )
6105
6123
linewidths = (0.25 ,)
6106
6124
if 'linewidth' in kwargs :
6107
6125
kwargs ['linewidths' ] = kwargs .pop ('linewidth' )
@@ -6137,6 +6155,8 @@ def pcolor(self, *args, shading=None, alpha=None, norm=None, cmap=None,
6137
6155
6138
6156
collection = mcoll .PolyQuadMesh (
6139
6157
coords , array = C , cmap = cmap , norm = norm , alpha = alpha , ** kwargs )
6158
+ collection ._units = units
6159
+ collection ._converter = converter
6140
6160
collection ._scale_norm (norm , vmin , vmax )
6141
6161
6142
6162
# Transform from native to data coordinates?
@@ -6356,15 +6376,18 @@ def pcolormesh(self, *args, alpha=None, norm=None, cmap=None, vmin=None,
6356
6376
shading = shading .lower ()
6357
6377
kwargs .setdefault ('edgecolors' , 'none' )
6358
6378
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
+ )
6361
6382
coords = np .stack ([X , Y ], axis = - 1 )
6362
6383
6363
6384
kwargs .setdefault ('snap' , mpl .rcParams ['pcolormesh.snap' ])
6364
6385
6365
6386
collection = mcoll .QuadMesh (
6366
6387
coords , antialiased = antialiased , shading = shading ,
6367
6388
array = C , cmap = cmap , norm = norm , alpha = alpha , ** kwargs )
6389
+ collection ._units = units
6390
+ collection ._converter = converter
6368
6391
collection ._scale_norm (norm , vmin , vmax )
6369
6392
6370
6393
coords = coords .reshape (- 1 , 2 ) # flatten the grid structure; keep x, y
0 commit comments