Thanks to visit codestin.com
Credit goes to github.com

Skip to content

fixed gouraud error in pcolormesh #26320

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
10 changes: 7 additions & 3 deletions lib/matplotlib/axes/_axes.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Comment on lines +5824 to +5825
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I haven't looked at these shapes in detail, but shouldn't these be consistent?

Suggested change
X = (X[1:, 1:] + X[:1, :-1]) / 2
Y = (Y[1:, :-1] + Y[:-1, :-1]) / 2
X = (X[1:, 1:] + X[:-1, :-1]) / 2
Y = (Y[1:, 1:] + Y[:-1, :-1]) / 2

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Might work, but I thought is made more since to change the X and Y boundary points independently.

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
Expand Down
21 changes: 19 additions & 2 deletions lib/matplotlib/tests/test_axes.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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])
Expand Down