diff --git a/lib/matplotlib/axes/_axes.py b/lib/matplotlib/axes/_axes.py index 982331b4ea45..b4fadf0ed35a 100644 --- a/lib/matplotlib/axes/_axes.py +++ b/lib/matplotlib/axes/_axes.py @@ -5820,9 +5820,13 @@ def _pcolorargs(self, funcname, *args, shading='auto', **kwargs): f" see help({funcname})") else: # ['nearest', 'gouraud']: if (Nx, Ny) != (ncols, nrows): - raise TypeError('Dimensions of C %s are incompatible with' - ' X (%d) and/or Y (%d); see help(%s)' % ( - C.shape, Nx, Ny, funcname)) + if (Nx, Ny) == (ncols + 1, nrows + 1): + X = (X[1:, 1:] + X[:1, :-1]) / 2 + Y = (Y[1:, :-1] + Y[:-1, :-1]) / 2 + else: + raise TypeError('Dimensions of C %s are incompatible with' + ' X (%d) and/or Y (%d); see help(%s)' % ( + C.shape, Nx, Ny, funcname)) if shading == 'nearest': # grid is specified at the center, so define corners # at the midpoints between the grid centers and then use the diff --git a/lib/matplotlib/tests/test_axes.py b/lib/matplotlib/tests/test_axes.py index 947c706e900f..bf77a3abf63d 100644 --- a/lib/matplotlib/tests/test_axes.py +++ b/lib/matplotlib/tests/test_axes.py @@ -1399,6 +1399,19 @@ def test_pcolormesh_alpha(): ax4.pcolormesh(Qx, Qy, Z, cmap=cmap, shading='gouraud', zorder=1) +@check_figures_equal() +def test_pcolormesh_gouraud(fig_test, fig_ref): + ax_test = fig_test.subplots() + ax_ref = fig_ref.subplots() + x = np.arange(-4, 5) + y = np.arange(-6, 7) + xe = np.linspace(-4.5, 4.5, 10) + ye = np.linspace(-6.5, 6.5, 14) + c = np.exp(-0.5*0.125*(x.reshape(1, -1)**2 + y.reshape(-1, 1)**2)) + ax_ref.pcolormesh(x, y, c, shading='gouraud') + ax_test.pcolormesh(xe, ye, c, shading='gouraud') + + @pytest.mark.parametrize("dims,alpha", [(3, 1), (4, 0.5)]) @check_figures_equal(extensions=["png"]) def test_pcolormesh_rgba(fig_test, fig_ref, dims, alpha): @@ -1476,9 +1489,13 @@ def test_pcolorargs(): with pytest.raises(TypeError): ax.pcolormesh(X, Y, Z.T) with pytest.raises(TypeError): - ax.pcolormesh(x, y, Z[:-1, :-1], shading="gouraud") + ax.pcolormesh(x, y, Z[:-2, :-2], shading="gouraud") + with pytest.raises(TypeError): + ax.pcolormesh(X, Y, Z[:-2, :-2], shading="gouraud") + with pytest.raises(TypeError): + ax.pcolormesh(x[1:], y[1:], Z, shading="gouraud") with pytest.raises(TypeError): - ax.pcolormesh(X, Y, Z[:-1, :-1], shading="gouraud") + ax.pcolormesh(X[1:, 1:], Y[1:, 1:], Z, shading="gouraud") x[0] = np.NaN with pytest.raises(ValueError): ax.pcolormesh(x, y, Z[:-1, :-1])