Skip to content

Incompatible dimensions with pcolormesh when using 'nearest' shading #1889

@agijsberts

Description

@agijsberts

Description

Pull request #1646 made large changes to how pcolormesh interpolates its arguments. The new code appears to recalculate the edges when shading is nearest, thus adding an element to both x and y dimensions. But in the process it does not update the shading argument accordingly, causing MPL to complain about incompatible dimensions.

Code to reproduce

A minimal example demonstrating the exception is

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

ax = plt.axes(projection=ccrs.PlateCarree())

n=3
x = np.arange(n)+1
y = np.arange(n)+1
d = np.random.rand(n,n)

im = ax.pcolormesh(x, y, d, shading='nearest')

Traceback

The traceback when running the code

Traceback (most recent call last):
  File "test.py", line 12, in <module>
    im = ax.pcolormesh(x, y, d, shading='nearest')
  File "/home/ec2-user/miniconda3/envs/arpav/lib/python3.8/site-packages/cartopy/mpl/geoaxes.py", line 318, in wrapper
    return func(self, *args, **kwargs)
  File "/home/ec2-user/miniconda3/envs/arpav/lib/python3.8/site-packages/cartopy/mpl/geoaxes.py", line 1797, in pcolormesh
    result = matplotlib.axes.Axes.pcolormesh(self, *args, **kwargs)
  File "/home/ec2-user/miniconda3/envs/arpav/lib/python3.8/site-packages/matplotlib/__init__.py", line 1361, in inner
    return func(ax, *map(sanitize_sequence, args), **kwargs)
  File "/home/ec2-user/miniconda3/envs/arpav/lib/python3.8/site-packages/matplotlib/axes/_axes.py", line 6183, in pcolormesh
    X, Y, C, shading = self._pcolorargs('pcolormesh', *args,
  File "/home/ec2-user/miniconda3/envs/arpav/lib/python3.8/site-packages/matplotlib/axes/_axes.py", line 5711, in _pcolorargs
    raise TypeError('Dimensions of C %s are incompatible with'
TypeError: Dimensions of C (3, 3) are incompatible with X (4) and/or Y (4); see help(pcolormesh)

Environment

Conda environment on Amazon Linux with cartopy 0.20.0 from conda-forge and matplotlib-base 3.4.3.

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions