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

Skip to content

plot.pcolormesh fails with shading='gouraud' #3002

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

Closed
TomNicholas opened this issue Jun 6, 2019 · 5 comments
Closed

plot.pcolormesh fails with shading='gouraud' #3002

TomNicholas opened this issue Jun 6, 2019 · 5 comments

Comments

@TomNicholas
Copy link
Member

xarray.plot.pcolormesh() fails when you pass the matplotlib.pyplot.pcolormesh() keyword argument shading='gouraud' to it.

Code Sample, a copy-pastable example if possible

import matplotlib.pyplot as plt
import numpy as np
import xarray as xr

lon, lat = np.meshgrid(np.linspace(-20, 20, 5), np.linspace(0, 30, 4))
lon += lat/10
lat += lon/10

da = xr.DataArray(np.arange(20).reshape(4, 5), dims=['y', 'x'],
                  coords = {'lat': (('y', 'x'), lat),
                            'lon': (('y', 'x'), lon)})

da.plot.pcolormesh('lon', 'lat', shading='gouraud')
plt.show()

Problem description

This gives an error:

Traceback (most recent call last):
  File "mwe.py", line 17, in <module>
    da.plot.pcolormesh('lon', 'lat', shading='gouraud')
  File "/home/tegn500/Documents/Work/Code/xarray/xarray/plot/plot.py", line 721, in plotmethod
    return newplotfunc(**allargs)
  File "/home/tegn500/Documents/Work/Code/xarray/xarray/plot/plot.py", line 662, in newplotfunc
    **kwargs)
  File "/home/tegn500/Documents/Work/Code/xarray/xarray/plot/plot.py", line 864, in pcolormesh
    primitive = ax.pcolormesh(x, y, z, **kwargs)
  File "/home/tegn500/anaconda3/envs/py36/lib/python3.6/site-packages/matplotlib/__init__.py", line 1805, in inner
    return func(ax, *args, **kwargs)
  File "/home/tegn500/anaconda3/envs/py36/lib/python3.6/site-packages/matplotlib/axes/_axes.py", line 5971, in pcolormesh
    X, Y, C = self._pcolorargs('pcolormesh', *args, allmatch=allmatch)
  File "/home/tegn500/anaconda3/envs/py36/lib/python3.6/site-packages/matplotlib/axes/_axes.py", line 5559, in _pcolorargs
    C.shape, Nx, Ny, funcname))
TypeError: Dimensions of C (4, 5) are incompatible with X (6) and/or Y (5); see help(pcolormesh)

Expected Output

This should give almost the same image as in the documentation, just with smoother shading:

Figure_1

@dcherian
Copy link
Contributor

dcherian commented Jun 6, 2019

I found this on googling: matplotlib/matplotlib#8422

@TomNicholas
Copy link
Member Author

Oh thanks @dcherian !

That's confusing because this works:

plt.pcolormesh(da['lon'].values, da['lat'].values, da.values, shading='gouraud')

but it looks like matplotlib is the problem, I'll close this.

@dcherian
Copy link
Contributor

dcherian commented Jun 6, 2019

This might be an infer_interval_breaks issue?

@TomNicholas
Copy link
Member Author

I tried

da.plot.pcolormesh('lon', 'lat', shading='gouraud', infer_intervals=True)

but got the same error unfortunately.

@johnomotani
Copy link
Contributor

I had the same problem. I found that

da.plot.pcolormesh('lon', 'lat', shading='gouraud', infer_intervals=False)

does work. Would be nice if shading='gouraud' worked by default though.

I guess the problem is that 'gouraud' needs the coordinate positions at the grid points, and I assume infer_intervals=True is creating coordinate values at the grid-cell corners (so an n x m DataArray is plotted using coordinates with lengh (n+1) and (m+1). I haven't looked into what infer_intervals actually does though. Would it be safe to just turn it off if shading='gouraud' is used?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants