proplot-dev / proplot

🎨 A succinct matplotlib wrapper for making beautiful, publication-quality graphics
https://proplot.readthedocs.io
MIT License
1.12k stars 103 forks source link

Support masked coordinates for pcolormesh #184

Closed zxdawn closed 2 years ago

zxdawn commented 4 years ago

Description

Currently, the official pcolormesh doesn't support NaN or masked coordinates. There're several solutions:

  1. Define a new function
  2. Handle the masked values manually

Steps to reproduce

import proplot as plot
import numpy as np

# Figure and sample data
state = np.random.RandomState(51423)
x = y = np.array([-10., -5., 0., 5., 10.])
data = state.rand(y.size, x.size)
fig, axs = plot.subplots()

x[:1] = np.nan
y[:1] = np.nan
axs.pcolormesh(x, y, data)

Expected behavior: Like pcolor: test_pcolormesh

Actual behavior:

Traceback (most recent call last):
  File "/home/xin/Desktop/test.py", line 12, in <module>
    axs.pcolormesh(x, y, data)
  File "/home/xin/Software/miniconda3/envs/s5p/lib/python3.7/site-packages/proplot/ui.py", line 682, in _iterator
    result.append(func(*args, **kwargs))
  File "/home/xin/Software/miniconda3/envs/s5p/lib/python3.7/site-packages/proplot/axes/plot.py", line 3867, in wrapper
    return driver(self, func, *args, **kwargs)
  File "/home/xin/Software/miniconda3/envs/s5p/lib/python3.7/site-packages/proplot/axes/plot.py", line 897, in standardize_2d
    return func(self, x, y, *Zs, **kwargs)
  File "/home/xin/Software/miniconda3/envs/s5p/lib/python3.7/site-packages/proplot/axes/plot.py", line 3867, in wrapper
    return driver(self, func, *args, **kwargs)
  File "/home/xin/Software/miniconda3/envs/s5p/lib/python3.7/site-packages/proplot/internals/warnings.py", line 97, in wrapper
    return func_orig(*args, **kwargs)
  File "/home/xin/Software/miniconda3/envs/s5p/lib/python3.7/site-packages/proplot/axes/plot.py", line 2880, in cmap_changer
    obj = func(self, *args, **kwargs)
  File "/home/xin/Software/miniconda3/envs/s5p/lib/python3.7/site-packages/matplotlib/__init__.py", line 1565, in inner
    return func(ax, *map(sanitize_sequence, args), **kwargs)
  File "/home/xin/Software/miniconda3/envs/s5p/lib/python3.7/site-packages/matplotlib/axes/_axes.py", line 6093, in pcolormesh
    X, Y, C = self._pcolorargs('pcolormesh', *args, allmatch=allmatch)
  File "/home/xin/Software/miniconda3/envs/s5p/lib/python3.7/site-packages/matplotlib/axes/_axes.py", line 5660, 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

Proplot version

0.6.3

lukelbd commented 2 years ago

I think I'll close this as this, as this feature request is probably more appropriate for matplotlib, and simply switching from pcolormesh to pcolor is an easy enough workaround for now.

See the matplotlib docs on differences between pcolor() and pcolormesh().