Skip to content

pcolormesh: shading='nearest' and non-monotonic coordinates #18317

Closed
@mathause

Description

@mathause

Bug report

Bug summary

#16258 deprecates shading="flat" when data and coordinates have the same shape and introduces shading="nearest" which interpolates the coordinates. However, the logic to interpolate the coordinates breaks down if they are not monotonically increasing.

def _interp_grid(X):

Of course non-monotonic coordinates are a bit exotic but they make sense for geographic data.

Code for reproduction

import numpy as np

def _interp_grid(X):
    # helper for below
    if np.shape(X)[1] > 1:
        dX = np.diff(X, axis=1)/2.
        X = np.hstack((X[:, [0]] - dX[:, [0]],
                       X[:, :-1] + dX,
                       X[:, [-1]] + dX[:, [-1]]))
    return X

x = np.array([1, 2, 0])
y = np.array([1, 2])

X, Y = np.meshgrid(x, y)

_interp_grid(X)

Actual outcome

Results in

array([[ 0.5,  1.5,  1. , -1. ],
       [ 0.5,  1.5,  1. , -1. ]])

While having cooridinates [1, 2, 0] may seem just as wrong as coordinates [0.5, 1.5, 1, -1] the latter can cause some real-world problems in conjunction with cartopy (SciTools/cartopy#1638):

shading_rasm_both

Expected outcome

  • A warning for non-monotonic data
  • A convenient option to restore the old behaviour, e.g. shading="drop"

cc @greglucas

Matplotlib version

  • Operating system: Linux Mint
  • Matplotlib version: '3.3.1'
  • Matplotlib backend (print(matplotlib.get_backend())): TkAgg
  • Python version: 3.8
  • Jupyter version (if applicable):
  • Other libraries:

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions