From e602b0cfddf174ed2b109c2e18afbcc9ffe17e44 Mon Sep 17 00:00:00 2001 From: Jody Klymak Date: Wed, 23 Sep 2020 06:47:47 -0700 Subject: [PATCH 1/2] FIX: unit-convert pcolorargs before interpolating --- lib/matplotlib/axes/_axes.py | 16 +++++++--------- 1 file changed, 7 insertions(+), 9 deletions(-) diff --git a/lib/matplotlib/axes/_axes.py b/lib/matplotlib/axes/_axes.py index 39cce44bffa6..430417d7367b 100644 --- a/lib/matplotlib/axes/_axes.py +++ b/lib/matplotlib/axes/_axes.py @@ -5422,8 +5422,7 @@ def imshow(self, X, cmap=None, norm=None, aspect=None, self.add_image(im) return im - @staticmethod - def _pcolorargs(funcname, *args, shading='flat'): + def _pcolorargs(self, funcname, *args, shading='flat', **kwargs): # - create X and Y if not present; # - reshape X and Y as needed if they are 1-D; # - check for proper sizes based on `shading` kwarg; @@ -5454,6 +5453,9 @@ def _pcolorargs(funcname, *args, shading='flat'): # Check x and y for bad data... C = np.asanyarray(args[2]) X, Y = [cbook.safe_masked_invalid(a) for a in args[:2]] + # unit conversion allows e.g. datetime objects as axis values + X, Y = self._process_unit_info([("x", X), ("y", Y)], kwargs) + if funcname == 'pcolormesh': if np.ma.is_masked(X) or np.ma.is_masked(Y): raise ValueError( @@ -5702,12 +5704,10 @@ def pcolor(self, *args, shading=None, alpha=None, norm=None, cmap=None, if shading is None: shading = rcParams['pcolor.shading'] shading = shading.lower() - X, Y, C, shading = self._pcolorargs('pcolor', *args, shading=shading) + X, Y, C, shading = self._pcolorargs('pcolor', *args, shading=shading, + kwargs=kwargs) Ny, Nx = X.shape - # unit conversion allows e.g. datetime objects as axis values - X, Y = self._process_unit_info([("x", X), ("y", Y)], kwargs) - # convert to MA, if necessary. C = ma.asarray(C) X = ma.asarray(X) @@ -5976,12 +5976,10 @@ def pcolormesh(self, *args, alpha=None, norm=None, cmap=None, vmin=None, kwargs.setdefault('edgecolors', 'None') X, Y, C, shading = self._pcolorargs('pcolormesh', *args, - shading=shading) + shading=shading, kwargs=kwargs) Ny, Nx = X.shape X = X.ravel() Y = Y.ravel() - # unit conversion allows e.g. datetime objects as axis values - X, Y = self._process_unit_info([("x", X), ("y", Y)], kwargs) # convert to one dimensional arrays C = C.ravel() From 75097a26bbf9e4558bfd62323524696519f1c422 Mon Sep 17 00:00:00 2001 From: Jody Klymak Date: Wed, 23 Sep 2020 07:06:49 -0700 Subject: [PATCH 2/2] TST: add a test --- lib/matplotlib/tests/test_axes.py | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/lib/matplotlib/tests/test_axes.py b/lib/matplotlib/tests/test_axes.py index 4b793668ec82..db8f2d7de5a8 100644 --- a/lib/matplotlib/tests/test_axes.py +++ b/lib/matplotlib/tests/test_axes.py @@ -1201,6 +1201,22 @@ def test_pcolornearest(fig_test, fig_ref): ax.pcolormesh(x2, y2, Z, shading='nearest') +@check_figures_equal(extensions=["png"]) +def test_pcolornearestunits(fig_test, fig_ref): + ax = fig_test.subplots() + x = [datetime.datetime.fromtimestamp(x * 3600) for x in range(10)] + y = np.arange(0, 3) + np.random.seed(19680801) + Z = np.random.randn(2, 9) + ax.pcolormesh(x, y, Z, shading='flat') + + ax = fig_ref.subplots() + # specify the centers + x2 = [datetime.datetime.fromtimestamp((x + 0.5) * 3600) for x in range(9)] + y2 = y[:-1] + np.diff(y) / 2 + ax.pcolormesh(x2, y2, Z, shading='nearest') + + @check_figures_equal(extensions=["png"]) def test_pcolordropdata(fig_test, fig_ref): ax = fig_test.subplots()