matplotlib / matplotlib

matplotlib: plotting with Python
https://matplotlib.org/stable/
20.22k stars 7.62k forks source link

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

Open egranstedt opened 7 years ago

egranstedt commented 7 years ago

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 theshading` keyword argument.

Matplotlib version

tacaswell commented 7 years ago

Exact work:

AtharvaKhare commented 7 years ago

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 commented 7 years ago

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 commented 5 years ago

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 commented 4 years ago

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

jklymak commented 4 years ago

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.

github-actions[bot] commented 12 months ago

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!

jklymak commented 12 months ago

This is still an issue.