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
@@ -5795,12 +5796,28 @@ def imshow(self, X, cmap=None, norm=None, *, aspect=None,
5795
5796
self .add_image (im )
5796
5797
return im
5797
5798
5799
+ @staticmethod
5800
+ def _convert_C_units (C ):
5801
+ """
5802
+ Remove any units attached to C, and return the units and converter used to do
5803
+ the conversion.
5804
+ """
5805
+ sm = cm .ScalarMappable ()
5806
+ C = sm ._strip_units (C )
5807
+ converter = sm ._converter
5808
+ units = sm ._units
5809
+
5810
+ C = np .asanyarray (C )
5811
+ C = cbook .safe_masked_invalid (C , copy = True )
5812
+ return C , units , converter
5813
+
5798
5814
def _pcolorargs (self , funcname , * args , shading = 'auto' , ** kwargs ):
5799
5815
# - create X and Y if not present;
5800
5816
# - reshape X and Y as needed if they are 1-D;
5801
5817
# - check for proper sizes based on `shading` kwarg;
5802
5818
# - reset shading if shading='auto' to flat or nearest
5803
5819
# depending on size;
5820
+ # - if C has units, get the converter
5804
5821
5805
5822
_valid_shading = ['gouraud' , 'nearest' , 'flat' , 'auto' ]
5806
5823
try :
@@ -5812,19 +5829,19 @@ def _pcolorargs(self, funcname, *args, shading='auto', **kwargs):
5812
5829
shading = 'auto'
5813
5830
5814
5831
if len (args ) == 1 :
5815
- C = np . asanyarray (args [0 ])
5832
+ C , units , converter = self . _convert_C_units (args [0 ])
5816
5833
nrows , ncols = C .shape [:2 ]
5817
5834
if shading in ['gouraud' , 'nearest' ]:
5818
5835
X , Y = np .meshgrid (np .arange (ncols ), np .arange (nrows ))
5819
5836
else :
5820
5837
X , Y = np .meshgrid (np .arange (ncols + 1 ), np .arange (nrows + 1 ))
5821
5838
shading = 'flat'
5822
5839
C = cbook .safe_masked_invalid (C , copy = True )
5823
- return X , Y , C , shading
5840
+ return X , Y , C , shading , units , converter
5824
5841
5825
5842
if len (args ) == 3 :
5826
5843
# Check x and y for bad data...
5827
- C = np . asanyarray (args [2 ])
5844
+ C , units , converter = self . _convert_C_units (args [2 ])
5828
5845
# unit conversion allows e.g. datetime objects as axis values
5829
5846
X , Y = args [:2 ]
5830
5847
X , Y = self ._process_unit_info ([("x" , X ), ("y" , Y )], kwargs )
@@ -5905,7 +5922,7 @@ def _interp_grid(X):
5905
5922
shading = 'flat'
5906
5923
5907
5924
C = cbook .safe_masked_invalid (C , copy = True )
5908
- return X , Y , C , shading
5925
+ return X , Y , C , shading , units , converter
5909
5926
5910
5927
@_preprocess_data ()
5911
5928
@_docstring .dedent_interpd
@@ -6057,8 +6074,9 @@ def pcolor(self, *args, shading=None, alpha=None, norm=None, cmap=None,
6057
6074
if shading is None :
6058
6075
shading = mpl .rcParams ['pcolor.shading' ]
6059
6076
shading = shading .lower ()
6060
- X , Y , C , shading = self ._pcolorargs ('pcolor' , * args , shading = shading ,
6061
- kwargs = kwargs )
6077
+ X , Y , C , shading , units , converter = self ._pcolorargs (
6078
+ 'pcolor' , * args , shading = shading , kwargs = kwargs
6079
+ )
6062
6080
linewidths = (0.25 ,)
6063
6081
if 'linewidth' in kwargs :
6064
6082
kwargs ['linewidths' ] = kwargs .pop ('linewidth' )
@@ -6094,6 +6112,8 @@ def pcolor(self, *args, shading=None, alpha=None, norm=None, cmap=None,
6094
6112
6095
6113
collection = mcoll .PolyQuadMesh (
6096
6114
coords , array = C , cmap = cmap , norm = norm , alpha = alpha , ** kwargs )
6115
+ collection ._units = units
6116
+ collection ._converter = converter
6097
6117
collection ._scale_norm (norm , vmin , vmax )
6098
6118
6099
6119
# Transform from native to data coordinates?
@@ -6313,15 +6333,18 @@ def pcolormesh(self, *args, alpha=None, norm=None, cmap=None, vmin=None,
6313
6333
shading = shading .lower ()
6314
6334
kwargs .setdefault ('edgecolors' , 'none' )
6315
6335
6316
- X , Y , C , shading = self ._pcolorargs ('pcolormesh' , * args ,
6317
- shading = shading , kwargs = kwargs )
6336
+ X , Y , C , shading , units , converter = self ._pcolorargs (
6337
+ 'pcolormesh' , * args , shading = shading , kwargs = kwargs
6338
+ )
6318
6339
coords = np .stack ([X , Y ], axis = - 1 )
6319
6340
6320
6341
kwargs .setdefault ('snap' , mpl .rcParams ['pcolormesh.snap' ])
6321
6342
6322
6343
collection = mcoll .QuadMesh (
6323
6344
coords , antialiased = antialiased , shading = shading ,
6324
6345
array = C , cmap = cmap , norm = norm , alpha = alpha , ** kwargs )
6346
+ collection ._units = units
6347
+ collection ._converter = converter
6325
6348
collection ._scale_norm (norm , vmin , vmax )
6326
6349
6327
6350
coords = coords .reshape (- 1 , 2 ) # flatten the grid structure; keep x, y
0 commit comments