question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

cmap.set_under() does not work as expected

See original GitHub issue

When using matplotlib, the set_under() method can be used to set values below the range of a colormap to a certain color, for example

import matplotlib
from matplotlib import pyplot
import numpy

dat = numpy.linspace(0, 1)[numpy.newaxis, :]*numpy.linspace(0, 1)[:, numpy.newaxis]

cmap = matplotlib.cm.viridis
#cmap.set_under('w')

pyplot.contourf(dat, vmin=.3, cmap=cmap)
pyplot.colorbar()
pyplot.show()

produces matplotlib_no-set_under

while uncommenting the cmap.set_under() call produces matplotlib

However, using xarray to do the same thing,

import matplotlib
from matplotlib import pyplot
from xarray import DataArray
import numpy

da = DataArray(numpy.linspace(0, 1)[numpy.newaxis, :]*numpy.linspace(0, 1)[:, numpy.newaxis])

cmap = matplotlib.cm.viridis
cmap.set_under('w')

da.plot.contourf(vmin=.3, cmap=cmap)
pyplot.show()

produces xarray where it seems the call to cmap.set_under('w') had no effect. Expected behaviour would be output like the second plot.

Output of xr.show_versions()

In [2]: xarray.show_versions()                                                                                                                                                                                                                

INSTALLED VERSIONS
------------------
commit: None
python: 3.6.9 (default, Nov  7 2019, 10:44:02) 
[GCC 8.3.0]
python-bits: 64
OS: Linux
OS-release: 5.0.0-37-generic
machine: x86_64
processor: x86_64
byteorder: little
LC_ALL: None
LANG: en_GB.UTF-8
LOCALE: en_GB.UTF-8
libhdf5: 1.10.0
libnetcdf: 4.6.0

xarray: 0.14.1
pandas: 0.24.2
numpy: 1.16.3
scipy: 1.2.1
netCDF4: 1.3.1
pydap: None
h5netcdf: None
h5py: 2.9.0
Nio: None
zarr: None
cftime: None
nc_time_axis: None
PseudoNetCDF: None
rasterio: None
cfgrib: None
iris: None
bottleneck: None
dask: 2.1.0
distributed: None
matplotlib: 3.1.1
cartopy: None
seaborn: None
numbagg: None
setuptools: 41.0.1
pip: 19.3.1
conda: None
pytest: 4.4.1
IPython: 7.6.1
sphinx: None

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:5 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
mathausecommented, Jan 9, 2020

As a workaround (until #3601) is finished you can do:

import matplotlib
from matplotlib import pyplot
from xarray import DataArray
import numpy

da = DataArray(numpy.linspace(0, 1)[numpy.newaxis, :]*numpy.linspace(0, 1)[:, numpy.newaxis])

cmap = matplotlib.cm.viridis

h = da.plot.contourf(vmin=.3, cmap=cmap)

cmap = h.get_cmap()
cmap.set_under('w')

h.set_cmap(cmap)
1reaction
dcheriancommented, Dec 3, 2019

No that’s for dataset plotting and is a norm for size of points plotted by scatter.

I would look here: https://github.com/pydata/xarray/blob/ed05f9862622b00f40f7b9b99ccdb0ab3766ff0f/xarray/plot/utils.py#L77-L101

Read more comments on GitHub >

github_iconTop Results From Across the Web

python - Reset default matplotlib colormap values after using ...
The behaviour you describe is expected. There is only a single colormap present, which is plt.cm.rainbow . When you first set_under('w') and ...
Read more >
matplotlib.colors.Colormap — Matplotlib 3.6.2 documentation
When this colormap exists on a scalar mappable and colorbar_extend is not False, colorbar creation ... This function is not implemented for the...
Read more >
Based on a Select widget update a second Select widget, then ...
I am trying to put together an app where user can: decide between colormap collections (for example between Matplotlib and Colorcet) using a ......
Read more >
REDCap Change Log - Eastern Virginia Medical School
(Ticket #128417b); Bug fix: The datediff() function might not work as expected when using different data types in the parameters (e.g., date and...
Read more >
Package google.cloud.aiplatform.v1 | Vertex AI
state is set to CANCELLED . Any files already outputted by the job are not deleted. Authorization Scopes. Requires the following OAuth scope:...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found