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

Skip to content

plt.pcolormesh shape mismatch when shading='gouraud' #8422

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
egranstedt opened this issue Apr 3, 2017 · 8 comments · May be fixed by #26320
Open

plt.pcolormesh shape mismatch when shading='gouraud' #8422

egranstedt opened this issue Apr 3, 2017 · 8 comments · May be fixed by #26320
Labels
Difficulty: Medium https://matplotlib.org/devdocs/devel/contribute.html#good-first-issues Good first issue Open a pull request against these issues if there are no active ones! keep Items to be ignored by the “Stale” Github Action topic: pcolor/pcolormesh

Comments

@egranstedt
Copy link

Bug report

Bug summary

When running plt.pcolormesh(X, Y, C, shading='flat'), if C.shape == (nrows, ncols), then if X and Y are 2-D arrays, their shapes must be either (nrows, ncols) or (nrows+1, ncols+1). If shading='gouraud', however, the latter results in an error. The code snippet below uses 1-D arrays for X and Y, but the same error occurs when they are the equivalent 2-D arrays.

Code for reproduction

import numpy as np
import matplotlib.pyplot as plt
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))
plt.pcolormesh(xe, ye, c, shading='flat')  # works
plt.pcolormesh(x, y, c, shading='gouraud')  # works
plt.pcolormesh(xe, ye, c, shading='gouraud')  # doesn't work

Actual outcome

Traceback (most recent call last):

  File "<ipython-input-67-154f761638cd>", line 1, in <module>
    plt.pcolormesh(xe, ye, c, shading='gouraud')

  File "/home/egranstedt/anaconda3/lib/python3.6/site-packages/matplotlib/pyplot.py", line 3245, in pcolormesh
    ret = ax.pcolormesh(*args, **kwargs)

  File "/home/egranstedt/anaconda3/lib/python3.6/site-packages/matplotlib/__init__.py", line 1892, in inner
    return func(ax, *args, **kwargs)

  File "/home/egranstedt/anaconda3/lib/python3.6/site-packages/matplotlib/axes/_axes.py", line 5569, in pcolormesh
    X, Y, C = self._pcolorargs('pcolormesh', *args, allmatch=allmatch)

  File "/home/egranstedt/anaconda3/lib/python3.6/site-packages/matplotlib/axes/_axes.py", line 5187, in _pcolorargs
    C.shape, Nx, Ny, funcname))

TypeError: Dimensions of C (13, 9) are incompatible with X (10) and/or Y (14); see help(pcolormesh)

Expected outcome

I expected pcolormesh would accept the same arrays for X and Y irrespective of the setting of the shading` keyword argument.

Matplotlib version

  • The error occurs with Matplotlib version 2.0.0 in the python 3.6 interpreter on Linux (installed via anaconda), but also seems to exist in older versions of Matplotlib (e.g., 1.5.1)
@tacaswell tacaswell added this to the 2.1 (next point release) milestone Apr 4, 2017
@tacaswell tacaswell modified the milestones: 2.1 (next point release), 2.1.1 (next bug fix release) Sep 24, 2017
@tacaswell tacaswell added new-contributor-friendly Difficulty: Medium https://matplotlib.org/devdocs/devel/contribute.html#good-first-issues labels Sep 24, 2017
@tacaswell
Copy link
Member

Exact work:

  • sort out if we can handle the N+1 case for gourand shading
  • if so, fix the bug, if not document the limitation

@tacaswell tacaswell modified the milestones: 2.1.1 (next bug fix release), 2.2 (next feature release) Oct 9, 2017
@tacaswell tacaswell added the Good first issue Open a pull request against these issues if there are no active ones! label Oct 16, 2017
@AtharvaKhare
Copy link
Contributor

AtharvaKhare commented Oct 20, 2017

Dimensions of c are wrong. Try this:

import numpy as np
import matplotlib.pyplot as plt
xe = np.linspace(-4.5, 4.5, 10)
ye = np.linspace(-6.5, 6.5, 14)
c = np.exp(-0.5*0.125*(xe.reshape(1, -1)**2 + ye.reshape(-1, 1)**2))
plt.pcolormesh(xe, ye, c, shading='gouraud') 
plt.show()

@dopplershift
Copy link
Contributor

The actual problem here is that if you don't use the Gouraud shading, pcolormesh is capable of automatically adjusting dimensions so that the original example works. The question is what it will take for Gouraud shading to work when given the slightly mis-shaped data.

@Jwink3101
Copy link

Just checking if there has been an on this? I followed pull request 9594 but it isn't clear it got done.

There are two distinct modes of operation here. When flat shading, pcolormesh is like a histogram where you define edges and then center fill color. When then gouraud, it is defined at the vertices. This should be consistent!

@QuLogic
Copy link
Member

QuLogic commented Feb 13, 2020

@jklymak since you just finished working on the 'flat' case, I wonder if you have any thoughts here (and on the linked PR)?

@jklymak
Copy link
Member

jklymak commented Feb 13, 2020

I think interpolating the grid midpoints is "fine" to make gouraud work, maybe with a warning. The linked PR died because the author insisted on a kwarg controlling whether that would happen or not, which I didn't agree with.

@wyojustin wyojustin linked a pull request Jul 15, 2023 that will close this issue
1 task
Copy link

github-actions bot commented Nov 3, 2023

This issue has been marked "inactive" because it has been 365 days since the last comment. If this issue is still present in recent Matplotlib releases, or the feature request is still wanted, please leave a comment and this label will be removed. If there are no updates in another 30 days, this issue will be automatically closed, but you are free to re-open or create a new issue if needed. We value issue reports, and this procedure is meant to help us resurface and prioritize issues that have not been addressed yet, not make them disappear. Thanks for your help!

@github-actions github-actions bot added the status: inactive Marked by the “Stale” Github Action label Nov 3, 2023
@jklymak jklymak added keep Items to be ignored by the “Stale” Github Action and removed status: inactive Marked by the “Stale” Github Action labels Nov 3, 2023
@jklymak
Copy link
Member

jklymak commented Nov 3, 2023

This is still an issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Difficulty: Medium https://matplotlib.org/devdocs/devel/contribute.html#good-first-issues Good first issue Open a pull request against these issues if there are no active ones! keep Items to be ignored by the “Stale” Github Action topic: pcolor/pcolormesh
Projects
None yet
Development

Successfully merging a pull request may close this issue.

9 participants