Skip to content

Commit a010c54

Browse files
authored
Merge pull request #18549 from jklymak/fix-pcolorarg-convert-before-interp
FIX: unit-convert pcolorargs before interpolating
2 parents 1dcfce6 + 75097a2 commit a010c54

File tree

2 files changed

+23
-9
lines changed

2 files changed

+23
-9
lines changed

lib/matplotlib/axes/_axes.py

+7-9
Original file line numberDiff line numberDiff line change
@@ -5422,8 +5422,7 @@ def imshow(self, X, cmap=None, norm=None, aspect=None,
54225422
self.add_image(im)
54235423
return im
54245424

5425-
@staticmethod
5426-
def _pcolorargs(funcname, *args, shading='flat'):
5425+
def _pcolorargs(self, funcname, *args, shading='flat', **kwargs):
54275426
# - create X and Y if not present;
54285427
# - reshape X and Y as needed if they are 1-D;
54295428
# - check for proper sizes based on `shading` kwarg;
@@ -5454,6 +5453,9 @@ def _pcolorargs(funcname, *args, shading='flat'):
54545453
# Check x and y for bad data...
54555454
C = np.asanyarray(args[2])
54565455
X, Y = [cbook.safe_masked_invalid(a) for a in args[:2]]
5456+
# unit conversion allows e.g. datetime objects as axis values
5457+
X, Y = self._process_unit_info([("x", X), ("y", Y)], kwargs)
5458+
54575459
if funcname == 'pcolormesh':
54585460
if np.ma.is_masked(X) or np.ma.is_masked(Y):
54595461
raise ValueError(
@@ -5702,12 +5704,10 @@ def pcolor(self, *args, shading=None, alpha=None, norm=None, cmap=None,
57025704
if shading is None:
57035705
shading = rcParams['pcolor.shading']
57045706
shading = shading.lower()
5705-
X, Y, C, shading = self._pcolorargs('pcolor', *args, shading=shading)
5707+
X, Y, C, shading = self._pcolorargs('pcolor', *args, shading=shading,
5708+
kwargs=kwargs)
57065709
Ny, Nx = X.shape
57075710

5708-
# unit conversion allows e.g. datetime objects as axis values
5709-
X, Y = self._process_unit_info([("x", X), ("y", Y)], kwargs)
5710-
57115711
# convert to MA, if necessary.
57125712
C = ma.asarray(C)
57135713
X = ma.asarray(X)
@@ -5976,12 +5976,10 @@ def pcolormesh(self, *args, alpha=None, norm=None, cmap=None, vmin=None,
59765976
kwargs.setdefault('edgecolors', 'None')
59775977

59785978
X, Y, C, shading = self._pcolorargs('pcolormesh', *args,
5979-
shading=shading)
5979+
shading=shading, kwargs=kwargs)
59805980
Ny, Nx = X.shape
59815981
X = X.ravel()
59825982
Y = Y.ravel()
5983-
# unit conversion allows e.g. datetime objects as axis values
5984-
X, Y = self._process_unit_info([("x", X), ("y", Y)], kwargs)
59855983

59865984
# convert to one dimensional arrays
59875985
C = C.ravel()

lib/matplotlib/tests/test_axes.py

+16
Original file line numberDiff line numberDiff line change
@@ -1201,6 +1201,22 @@ def test_pcolornearest(fig_test, fig_ref):
12011201
ax.pcolormesh(x2, y2, Z, shading='nearest')
12021202

12031203

1204+
@check_figures_equal(extensions=["png"])
1205+
def test_pcolornearestunits(fig_test, fig_ref):
1206+
ax = fig_test.subplots()
1207+
x = [datetime.datetime.fromtimestamp(x * 3600) for x in range(10)]
1208+
y = np.arange(0, 3)
1209+
np.random.seed(19680801)
1210+
Z = np.random.randn(2, 9)
1211+
ax.pcolormesh(x, y, Z, shading='flat')
1212+
1213+
ax = fig_ref.subplots()
1214+
# specify the centers
1215+
x2 = [datetime.datetime.fromtimestamp((x + 0.5) * 3600) for x in range(9)]
1216+
y2 = y[:-1] + np.diff(y) / 2
1217+
ax.pcolormesh(x2, y2, Z, shading='nearest')
1218+
1219+
12041220
@check_figures_equal(extensions=["png"])
12051221
def test_pcolordropdata(fig_test, fig_ref):
12061222
ax = fig_test.subplots()

0 commit comments

Comments
 (0)