Skip to content
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

pcolormesh fails with gouraud shading #1651

Closed
davidcortesortuno opened this issue Sep 8, 2020 · 3 comments
Closed

pcolormesh fails with gouraud shading #1651

davidcortesortuno opened this issue Sep 8, 2020 · 3 comments

Comments

@davidcortesortuno
Copy link

This refers to a question I posted on Stackoverflow https://stackoverflow.com/questions/63776199/cartopy-slow-rendering-with-non-orthographic-projection

When using a 100x100 array (or any size) and using pcolormesh, adding the shading='gouraud' argument fails but using 'flat' is fine.
By not specifying the shading argument, the rendering is super slow compared to using an Orthographic projection. It seems the C array in geoaxes.py is not well defined for the gouraud shading?

Code to reproduce

import numpy as np 
import matplotlib.pyplot as plt
import cartopy
import cartopy.crs as ccrs
import time

# Data
# Notice: we can use either phi ∈ [-180, 180] OR phi ∈ [0, 360]
phi = np.linspace(0, 2 * np.pi, 100)
lat = np.linspace(-np.pi / 2, np.pi / 2, 100)
# NOTICE: that PI is defined in the -z direction
theta = (lat + np.pi / 2)[::-1]
data = np.zeros((len(theta), len(phi)), dtype=np.float64)
for j, Th in enumerate(theta):
    for i, Ph in enumerate(phi):
        data[j, i] = Ph  # plot by longitude

# Plot
t = time.time()
# Set up figure
fig = plt.figure(figsize=(8, 4))
ax = plt.axes(projection=ccrs.InterruptedGoodeHomolosine())

vlim = np.max(np.abs(data))
p = ax.pcolormesh(phi * 180 / np.pi, lat * 180 / np.pi,
                  data,
                  transform=ccrs.PlateCarree(),
                  cmap='RdBu',vmin=0, vmax=vlim)
ax.autoscale_view()
gl = ax.gridlines(draw_labels=False)
plt.colorbar(p)
plt.show()
print(time.time() - t)

Traceback

-----------------------------------------------------------------
ValueError                      Traceback (most recent call last)
<ipython-input-9-b199a9d7c32c> in <module>
      2 ax = plt.axes(projection=ccrs.InterruptedGoodeHomolosine())
      3 vlim = np.max(np.abs(data))
----> 4 p = ax.pcolormesh(phi * 180 / np.pi, lat * 180 / np.pi,
      5                   data,
      6                   transform=ccrs.PlateCarree(),

~/miniconda3/lib/python3.8/site-packages/cartopy/mpl/geoaxes.py in wrapper(self, *args, **kwargs)
    308 
    309         kwargs['transform'] = transform
--> 310         return func(self, *args, **kwargs)
    311     return wrapper
    312 

~/miniconda3/lib/python3.8/site-packages/cartopy/mpl/geoaxes.py in pcolormesh(self, *args, **kwargs)
   1559 
   1560         """
-> 1561         result = self._pcolormesh_patched(*args, **kwargs)
   1562         self.autoscale_view()
   1563         return result

~/miniconda3/lib/python3.8/site-packages/cartopy/mpl/geoaxes.py in _pcolormesh_patched(self, *args, **kwargs)
   1672                     isinstance(self.projection, wrap_proj_types):
   1673 
-> 1674                 C = C.reshape((Ny - 1, Nx - 1))
   1675                 transformed_pts = transformed_pts.reshape((Ny, Nx, 2))
   1676 

~/miniconda3/lib/python3.8/site-packages/numpy/ma/core.py in reshape(self, *s, **kwargs)
   4650         """
   4651         kwargs.update(order=kwargs.get('order', 'C'))
-> 4652         result = self._data.reshape(*s, **kwargs).view(type(self))
   4653         result._update_from(self)
   4654         mask = self._mask

ValueError: cannot reshape array of size 10000 into shape (99,99)
Full environment definition

Operating system

Linux. openSUSE Tumbleweed 20200829

Cartopy version

0.18

conda list

# packages in environment at /home/david/miniconda3:
#
# Name                    Version                   Build  Channel
_libgcc_mutex             0.1                        main  
argon2-cffi               20.1.0                   pypi_0    pypi
astroid                   2.4.2                    pypi_0    pypi
attrs                     19.3.0                   pypi_0    pypi
backcall                  0.2.0                    pypi_0    pypi
beautifulsoup4            4.9.1                    pypi_0    pypi
bleach                    3.1.5                    pypi_0    pypi
ca-certificates           2020.7.22                     0  
cartopy                   0.18.0                   pypi_0    pypi
certifi                   2020.6.20                pypi_0    pypi
cffi                      1.14.0           py38he30daa8_1  
chardet                   3.0.4                 py38_1003  
conda                     4.8.4                    py38_0  
conda-package-handling    1.6.1            py38h7b6447c_0  
cryptography              2.9.2            py38h1ba5d50_0  
cycler                    0.10.0                   pypi_0    pypi
cython                    0.29.21                  pypi_0    pypi
decorator                 4.4.2                    pypi_0    pypi
defusedxml                0.6.0                    pypi_0    pypi
entrypoints               0.3                      pypi_0    pypi
flake8                    3.8.3                    pypi_0    pypi
geos                      3.8.1                he6710b0_0  
greenlet                  0.4.16                   pypi_0    pypi
icu                       58.2                 he6710b0_3  
idna                      2.9                        py_1  
iniconfig                 1.0.1                    pypi_0    pypi
ipykernel                 5.3.4                    pypi_0    pypi
ipython                   7.17.0                   pypi_0    pypi
ipython-genutils          0.2.0                    pypi_0    pypi
ipywidgets                7.5.1                    pypi_0    pypi
isort                     4.3.21                   pypi_0    pypi
jedi                      0.17.2                   pypi_0    pypi
jinja2                    2.11.2                   pypi_0    pypi
json5                     0.9.5                    pypi_0    pypi
jsonschema                3.2.0                    pypi_0    pypi
jupyter                   1.0.0                    pypi_0    pypi
jupyter-client            6.1.6                    pypi_0    pypi
jupyter-console           6.1.0                    pypi_0    pypi
jupyter-core              4.6.3                    pypi_0    pypi
jupyterlab                2.2.5                    pypi_0    pypi
jupyterlab-server         1.2.0                    pypi_0    pypi
jupytext                  1.6.0                    pypi_0    pypi
kiwisolver                1.2.0                    pypi_0    pypi
lazy-object-proxy         1.4.3                    pypi_0    pypi
ld_impl_linux-64          2.33.1               h53a641e_7  
libedit                   3.1.20181209         hc058e9b_0  
libffi                    3.3                  he6710b0_1  
libgcc-ng                 9.1.0                hdf63c60_0  
libstdcxx-ng              9.1.0                hdf63c60_0  
libxml2                   2.9.10               he19cac6_1  
libxslt                   1.1.34               hc22bd24_0  
lxml                      4.5.2            py38hefd8a0e_0  
markdown-it-py            0.5.3                    pypi_0    pypi
markupsafe                1.1.1                    pypi_0    pypi
matplotlib                3.3.1                    pypi_0    pypi
mccabe                    0.6.1                    pypi_0    pypi
mistune                   0.8.4                    pypi_0    pypi
more-itertools            8.5.0                    pypi_0    pypi
msgpack                   1.0.0                    pypi_0    pypi
multipole-inversion       0.1                      pypi_0    pypi
mypy                      0.782                    pypi_0    pypi
mypy-extensions           0.4.3                    pypi_0    pypi
nbconvert                 5.6.1                    pypi_0    pypi
nbformat                  5.0.7                    pypi_0    pypi
ncurses                   6.2                  he6710b0_1  
neovim                    0.3.1                    pypi_0    pypi
notebook                  6.1.3                    pypi_0    pypi
numpy                     1.19.1                   pypi_0    pypi
openssl                   1.1.1g               h7b6447c_0  
packaging                 20.4                     pypi_0    pypi
pandocfilters             1.4.2                    pypi_0    pypi
parso                     0.7.1                    pypi_0    pypi
pathlib                   1.0.1                    pypi_0    pypi
pep8                      1.7.1                    pypi_0    pypi
pexpect                   4.8.0                    pypi_0    pypi
pickleshare               0.7.5                    pypi_0    pypi
pillow                    7.2.0                    pypi_0    pypi
pip                       20.0.2                   py38_3  
pluggy                    0.13.1                   pypi_0    pypi
proj                      6.2.1                haa6030c_0  
prometheus-client         0.8.0                    pypi_0    pypi
prompt-toolkit            3.0.6                    pypi_0    pypi
psutil                    5.7.2                    pypi_0    pypi
ptyprocess                0.6.0                    pypi_0    pypi
py                        1.9.0                    pypi_0    pypi
pycodestyle               2.6.0                    pypi_0    pypi
pycosat                   0.6.3            py38h7b6447c_1  
pycparser                 2.20                       py_0  
pyflakes                  2.2.0                    pypi_0    pypi
pygments                  2.6.1                    pypi_0    pypi
pylint                    2.5.3                    pypi_0    pypi
pynvim                    0.4.1                    pypi_0    pypi
pyopenssl                 19.1.0                   py38_0  
pyparsing                 2.4.7                    pypi_0    pypi
pyrsistent                0.16.0                   pypi_0    pypi
pyshp                     2.1.0                    pypi_0    pypi
pysocks                   1.7.1                    py38_0  
pytest                    6.0.1                    pypi_0    pypi
python                    3.8.3                hcff3b4d_0  
python-dateutil           2.8.1                    pypi_0    pypi
pyvtk                     0.5.18                   pypi_0    pypi
pyyaml                    5.3.1                    pypi_0    pypi
pyzmq                     19.0.2                   pypi_0    pypi
qtconsole                 4.7.6                    pypi_0    pypi
qtpy                      1.9.0                    pypi_0    pypi
readline                  8.0                  h7b6447c_0  
requests                  2.23.0                   py38_0  
ruamel_yaml               0.15.87          py38h7b6447c_0  
scipy                     1.5.2                    pypi_0    pypi
send2trash                1.5.0                    pypi_0    pypi
setuptools                46.4.0                   py38_0  
shapely                   1.8.dev0                 pypi_0    pypi
six                       1.14.0                   py38_0  
soupsieve                 2.0.1                    pypi_0    pypi
sqlite                    3.31.1               h62c20be_1  
terminado                 0.8.3                    pypi_0    pypi
testpath                  0.4.4                    pypi_0    pypi
tk                        8.6.8                hbc83047_0  
toml                      0.10.1                   pypi_0    pypi
tornado                   6.0.4                    pypi_0    pypi
tqdm                      4.46.0                     py_0  
traitlets                 4.3.3                    pypi_0    pypi
typed-ast                 1.4.1                    pypi_0    pypi
typing-extensions         3.7.4.2                  pypi_0    pypi
urllib3                   1.25.8                   py38_0  
wcwidth                   0.2.5                    pypi_0    pypi
webencodings              0.5.1                    pypi_0    pypi
wheel                     0.34.2                   py38_0  
widgetsnbextension        3.5.1                    pypi_0    pypi
wrapt                     1.12.1                   pypi_0    pypi
xz                        5.2.5                h7b6447c_0  
yaml                      0.1.7                had09818_2  
zlib                      1.2.11               h7b6447c_3 

pip list

Package                Version            
---------------------- -------------------
argon2-cffi            20.1.0             
astroid                2.4.2              
attrs                  19.3.0             
backcall               0.2.0              
beautifulsoup4         4.9.1              
bleach                 3.1.5              
Cartopy                0.18.0             
certifi                2020.6.20          
cffi                   1.14.0             
chardet                3.0.4              
conda                  4.8.4              
conda-package-handling 1.7.0              
cryptography           2.9.2              
cycler                 0.10.0             
Cython                 0.29.21            
decorator              4.4.2              
defusedxml             0.6.0              
entrypoints            0.3                
flake8                 3.8.3              
greenlet               0.4.16             
idna                   2.9                
iniconfig              1.0.1              
ipykernel              5.3.4              
ipython                7.17.0             
ipython-genutils       0.2.0              
ipywidgets             7.5.1              
isort                  4.3.21             
jedi                   0.17.2             
Jinja2                 2.11.2             
json5                  0.9.5              
jsonschema             3.2.0              
jupyter                1.0.0              
jupyter-client         6.1.6              
jupyter-console        6.1.0              
jupyter-core           4.6.3              
jupyterlab             2.2.5              
jupyterlab-server      1.2.0              
jupytext               1.6.0              
kiwisolver             1.2.0              
lazy-object-proxy      1.4.3              
lxml                   4.5.2              
markdown-it-py         0.5.3              
MarkupSafe             1.1.1              
matplotlib             3.3.1              
mccabe                 0.6.1              
mistune                0.8.4              
more-itertools         8.5.0              
msgpack                1.0.0              
multipole-inversion    0.1                
mypy                   0.782              
mypy-extensions        0.4.3              
nbconvert              5.6.1              
nbformat               5.0.7              
neovim                 0.3.1              
notebook               6.1.3              
numpy                  1.19.1             
packaging              20.4               
pandocfilters          1.4.2              
parso                  0.7.1              
pathlib                1.0.1              
pep8                   1.7.1              
pexpect                4.8.0              
pickleshare            0.7.5              
Pillow                 7.2.0              
pip                    20.0.2             
pluggy                 0.13.1             
prometheus-client      0.8.0              
prompt-toolkit         3.0.6              
psutil                 5.7.2              
ptyprocess             0.6.0              
py                     1.9.0              
pycodestyle            2.6.0              
pycosat                0.6.3              
pycparser              2.20               
pyflakes               2.2.0              
Pygments               2.6.1              
pylint                 2.5.3              
pynvim                 0.4.1              
pyOpenSSL              19.1.0             
pyparsing              2.4.7              
pyrsistent             0.16.0             
pyshp                  2.1.0              
PySocks                1.7.1              
pytest                 6.0.1              
python-dateutil        2.8.1              
PyVTK                  0.5.18             
PyYAML                 5.3.1              
pyzmq                  19.0.2             
qtconsole              4.7.6              
QtPy                   1.9.0              
requests               2.23.0             
ruamel-yaml            0.15.87            
scipy                  1.5.2              
Send2Trash             1.5.0              
setuptools             46.4.0.post20200518
Shapely                1.8.dev0           
six                    1.14.0             
soupsieve              2.0.1              
terminado              0.8.3              
testpath               0.4.4              
toml                   0.10.1             
tornado                6.0.4              
tqdm                   4.46.0             
traitlets              4.3.3              
typed-ast              1.4.1              
typing-extensions      3.7.4.2            
urllib3                1.25.8             
wcwidth                0.2.5              
webencodings           0.5.1              
wheel                  0.34.2             
widgetsnbextension     3.5.1              
wrapt                  1.12.1   
@greglucas
Copy link
Contributor

The most recent MPL release (3.3) changed the way pcolormesh handles the data to draw the mesh. They used to silently drop the last row/column of data to change the shape of C. See here https://matplotlib.org/3.3.1/gallery/images_contours_and_fields/pcolormesh_grids.html
for some details on that change.

I think that using an MPL version less than 3.3 may solve this issue. But, there is PR #1646 that should bring all of the shading arguments into alignment again on cartopy's side as well.

@jypeter
Copy link

jypeter commented Sep 10, 2021

I have just ran into the same issue. If I execute the Block plots example after adding shading='gouraud' to the pcolormesh call, I get

> python -i advanced_plotting-2.py

Traceback (most recent call last):
  File "advanced_plotting-2.py", line 27, in <module>
    plt.pcolormesh(gridlons, gridlats, temperature, shading='gouraud', transform=rotated_pole)
  File "/home/share/unix_files/cdat/miniconda3_21-02/envs/cdatm_py3/lib/python3.8/site-packages/matplotlib/pyplot.py", line 2801, in pcolormesh
    __ret = gca().pcolormesh(
  File "/home/share/unix_files/cdat/miniconda3_21-02/envs/cdatm_py3/lib/python3.8/site-packages/cartopy/mpl/geoaxes.py", line 310, in wrapper
    return func(self, *args, **kwargs)
  File "/home/share/unix_files/cdat/miniconda3_21-02/envs/cdatm_py3/lib/python3.8/site-packages/cartopy/mpl/geoaxes.py", line 1561, in pcolormesh
    result = self._pcolormesh_patched(*args, **kwargs)
  File "/home/share/unix_files/cdat/miniconda3_21-02/envs/cdatm_py3/lib/python3.8/site-packages/cartopy/mpl/geoaxes.py", line 1597, in _pcolormesh_patched
    X, Y, C, shading = self._pcolorargs('pcolormesh', *args,
  File "/home/share/unix_files/cdat/miniconda3_21-02/envs/cdatm_py3/lib/python3.8/site-packages/matplotlib/axes/_axes.py", line 5625, in _pcolorargs
    raise TypeError('Dimensions of C %s are incompatible with'
TypeError: Dimensions of C (22, 36) are incompatible with X (37) and/or Y (23); see help(pcolormesh)
>>>

I'm using

 >conda list | grep -E '(cartopy|iris|matplotlib)'
cartopy                   0.18.0           py38h9b6dd9e_7    conda-forge
iris                      3.0.1            py38h578d9bd_1    conda-forge
iris-sample-data          2.3.0              pyh9f0ad1d_0    conda-forge
matplotlib                3.3.4            py38h578d9bd_0    conda-forge
matplotlib-base           3.3.4            py38h0efea84_0    conda-forge

@rcomer
Copy link
Member

rcomer commented Feb 21, 2024

The code from the OP now (Cartopy 0.22, Matplotlib 3.8.2) gives me

image

So I guess this was fixed, probably by #1646.

@rcomer rcomer closed this as completed Feb 21, 2024
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

4 participants