SciTools / cartopy

Cartopy - a cartographic python library with matplotlib support
https://scitools.org.uk/cartopy/docs/latest
BSD 3-Clause "New" or "Revised" License
1.43k stars 365 forks source link

pcolormesh for equal-area grid working on version 0.15.1 but not on 0.16.0 #1157

Closed MtsOg closed 6 years ago

MtsOg commented 6 years ago

Description

I'm trying to plot data on a EASE2 southern hemispheric equal-area grid but pcolormesh is working only with version 0.15.1 of cartopy in this case (see image). The same code with version 0.16.0 leads to error.

For this example I have been using a grid with a very low resolution so the values could be directly implemented in the code (a simple sample of the grid with high resolution did not reproduce the error.

with_0_15_1__lowres

Code to reproduce

import numpy as np
import matplotlib.pyplot as plt
import cartopy.crs as ccrs
from cartopy import feature

lons = np.array([[0,0,0,0,0,0],
 [0,0, -108.43494882292201, -71.56505117707799,0,0],
 [0 ,-161.565051177078 ,-135.0,-45.0, -18.43494882292201,0],
 [0 ,161.565051177078 ,135.0, 45.0 ,18.43494882292201,0],
 [0,0,108.43494882292201, 71.56505117707799,0,0],
 [0,0,0,0,0,0]])

lats = np.array([[0,0,0,0,0,0],
 [0, 0, -46.417302961467186, -46.417302961467186, 0, 0],
 [0, -46.417302961467186 ,-70.91269950770489, -70.91269950770489,-46.417302961467186, 0],
 [0, -46.417302961467186, -70.91269950770489, -70.91269950770489,-46.417302961467186 ,0],
 [0, 0, -46.417302961467186 ,-46.417302961467186 ,0 ,0],
 [0,0,0,0,0,0]])

lats = np.ma.array(lats, mask = lats == 0)
lons = np.ma.array(lons, mask = lons == 0)

B = np.random.rand(len(lats),len(lons))

plt.cla()
plt.clf()
ax = plt.axes(projection=ccrs.SouthPolarStereo(central_longitude=0.))
ax.set_extent([-180, 180., -90., -70.], crs=ccrs.PlateCarree())
cs = ax.pcolormesh(lons, lats, B, cmap='RdYlBu_r', transform=ccrs.PlateCarree())
plt.colorbar(cs)
plt.tight_layout()
plt.show()

Traceback

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
/home/mauger/cartopy_bug/test2.py in <module>()
     36 ax = plt.axes(projection=ccrs.SouthPolarStereo(central_longitude=0.))
     37 ax.set_extent([-180, 180., -90., -70.], crs=ccrs.PlateCarree())
---> 38 cs = ax.pcolormesh(lons, lats, B, cmap='RdYlBu_r', transform=ccrs.PlateCarree())
     39 plt.colorbar(cs)
     40 plt.tight_layout()

/home/mauger/.venv/python2/lib/python2.7/site-packages/cartopy/mpl/geoaxes.pyc in pcolormesh(self, *args, **kwargs)
   1449                              ' consider using PlateCarree/RotatedPole.')
   1450         kwargs.setdefault('transform', t)
-> 1451         result = self._pcolormesh_patched(*args, **kwargs)
   1452         self.autoscale_view()
   1453         return result

/home/mauger/.venv/python2/lib/python2.7/site-packages/cartopy/mpl/geoaxes.pyc in _pcolormesh_patched(self, *args, **kwargs)
   1486         # convert to one dimensional arrays
   1487         C = C.ravel()
-> 1488         coords = np.column_stack((X.flat, Y.flat)).astype(float, copy=False)
   1489 
   1490         collection = mcoll.QuadMesh(

ValueError: setting an array element with a sequence.
Full environment definition ### Operating system ### Cartopy version 0.16.0 ### conda list ``` ``` ### pip list adium-theme-ubuntu 0.3.4 alabaster 0.7.7 astroid 1.4.4 Babel 1.3 basemap 1.0.7 beautifulsoup4 4.4.1 Cartopy 0.15.1 ccsm 0.9.12.3 CDApplet 1.0 CDBashApplet 1.0 cf-units 0+unknown chardet 2.3.0 Cheetah 2.4.4 cloud-sptheme 1.6 compizconfig-python 0.9.12.3 coverage 3.7.1 cryptography 1.2.3 cvxopt 1.1.4 cycler 0.9.0 Cython 0.23.4 dap 2.2.6.7 dblatex 0.3.7.post1 decorator 4.0.6 dnspython 1.12.0 docutils 0.12 ecdsa 0.13 enum34 1.1.2 FormEncode 1.3.0 GDAL 1.11.3 gyp 0.1 h5py 2.6.0 html5lib 0.999 httplib2 0.9.1 idna 2.0 ipaddress 1.0.16 ipython 2.4.1 jdcal 1.0 Jinja2 2.8 joblib 0.9.4 lazy-object-proxy 1.2.1 ldap3 1.0.3 logilab-common 1.1.0 lxml 3.5.0 mailnag 1.1.0 MarkupSafe 0.23 matplotlib 1.5.1 meld 3.14.2 mercurial 3.7.3 mpi4py 1.3.1 ndg-httpsclient 0.4.0 netCDF4 1.2.4 nose 1.3.7 numexpr 2.4.3 numpy 1.11.0 openpyxl 2.3.0 PAM 0.4.2 pandas 0.17.1 paramiko 1.16.0 Paste 1.7.5.1 PasteDeploy 1.5.2 PasteScript 1.7.5 patsy 0.4.1 pbr 1.8.0 pexpect 4.0.1 Pillow 3.1.2 pip 18.1 pkg-resources 0.0.0 psycopg2 2.6.1 ptyprocess 0.5 py 1.4.31 pyasn1 0.1.9 pycrypto 2.6.1 pycurl 7.43.0 Pygments 2.1 pygobject 3.20.0 pyinotify 0.9.6 pylint 1.5.2 PyMySQL 0.7.2 pyOpenSSL 0.15.1 pyparsing 2.0.3 pyproj 1.9.5.1 pyshp 1.2.12 pyspatialite 3.0.1 Pyste 0.9.10 pytest 2.8.7 python-apt 1.1.0b1+ubuntu0.16.4.2 python-dateutil 2.4.2 python-openid 2.2.5 pytz 2014.10 pyxdg 0.25 PyYAML 3.11 pyzmq 15.2.0 requests 2.9.1 roman 2.0.0 rpy2 2.7.8 scgi 1.13 scikit-learn 0.17 scipy 0.17.0 scour 0.32 setuptools 40.4.3 Shapely 1.5.13 simi 1.12.0 simplegeneric 0.8.1 simplejson 3.8.1 singledispatch 3.4.0.3 six 1.10.0 Sphinx 1.3.6 sphinx-bootstrap-theme 0.4.8 sphinx-rtd-theme 0.1.9 sphinxcontrib-programoutput 0.8.dev20150607 SQLAlchemy 1.0.11 statsmodels 0.6.1 stdeb 0.8.5 stevedore 1.12.0 tables 3.2.2 Tempita 0.5.2 tornado 4.2.1 Unidecode 0.4.19 unity-lens-photos 1.0 urllib3 1.13.1 vboxapi 1.0 virtualenv 15.0.1 virtualenv-clone 0.2.5 virtualenvwrapper 4.3.1 wheel 0.32.0 wrapt 1.8.0 wxPython 3.0.2.0 wxPython-common 3.0.2.0 xlrd 0.9.4 xlwt 0.7.5 ``` ```
ajdawson commented 6 years ago

Please can you post the full traceback please? What you posted looks like a partially complete traceback.

made with a class adapted from https://github.com/TUW-GEO/ease_grid, hereafter ease_grid

I can't reproduce this because your example depends on code that is not provided. Can you reduce it to a simpler case without the dependencies? It looks like you are ultimately using the ease_grid object to generate lats/lons so maybe the example would still work with a subset of those?

MtsOg commented 6 years ago

The 3 last lines of the traceback were missing, sorry for that. I've updated my post with a very low resolution version of the grid so it can be reproduced.

QuLogic commented 6 years ago

Are you sure that example works with 0.15.1? For me it fails the same way as 0.16.0:

Traceback (most recent call last):
  File "issue1157.py", line 35, in <module>
    cs = ax.pcolormesh(lons, lats, B, cmap='RdYlBu_r', transform=ccrs.PlateCarree())
  File "//cartopy/lib/cartopy/mpl/geoaxes.py", line 1391, in pcolormesh
    result = self._pcolormesh_patched(*args, **kwargs)
  File "//cartopy/lib/cartopy/mpl/geoaxes.py", line 1435, in _pcolormesh_patched
    X, Y, C = self._pcolorargs('pcolormesh', *args, allmatch=allmatch)
  File "//matplotlib/axes/_axes.py", line 5541, in _pcolorargs
    'x and y arguments to pcolormesh cannot have '
ValueError: x and y arguments to pcolormesh cannot have non-finite values or be of type numpy.ma.core.MaskedArray with masked values
QuLogic commented 6 years ago

matplotlib 1.5.1

Actually, I think you might need to update Matplotlib.

MtsOg commented 6 years ago

Oh ok I have the same error for both version now. But it looks like it is more an error from my way of plotting than a cartopy error. I will investigate that, thanks for the help.

dopplershift commented 6 years ago

Closing this out since it sounds like it's not actually a Cartopy issue. Feel free to re-open if you find out otherwise.