Skip to content

backport pr 18549 #18550

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 8 additions & 13 deletions lib/matplotlib/axes/_axes.py
Original file line number Diff line number Diff line change
Expand Up @@ -5535,8 +5535,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;
Expand Down Expand Up @@ -5567,6 +5566,10 @@ 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
self._process_unit_info(xdata=X, ydata=Y, kwargs=kwargs)
X = self.convert_xunits(X)
Y = self.convert_yunits(Y)
if funcname == 'pcolormesh':
if np.ma.is_masked(X) or np.ma.is_masked(Y):
raise ValueError(
Expand Down Expand Up @@ -5815,14 +5818,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
self._process_unit_info(xdata=X, ydata=Y, kwargs=kwargs)
X = self.convert_xunits(X)
Y = self.convert_yunits(Y)

# convert to MA, if necessary.
C = ma.asarray(C)
X = ma.asarray(X)
Expand Down Expand Up @@ -6091,14 +6090,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
self._process_unit_info(xdata=X, ydata=Y, kwargs=kwargs)
X = self.convert_xunits(X)
Y = self.convert_yunits(Y)

# convert to one dimensional arrays
C = C.ravel()
Expand Down
16 changes: 16 additions & 0 deletions lib/matplotlib/tests/test_axes.py
Original file line number Diff line number Diff line change
Expand Up @@ -1188,6 +1188,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()
Expand Down