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

xr.plot infers sequential colormap on diverging levels #3524

Closed
mathause opened this issue Nov 13, 2019 · 2 comments · Fixed by #3913
Closed

xr.plot infers sequential colormap on diverging levels #3524

mathause opened this issue Nov 13, 2019 · 2 comments · Fixed by #3913

Comments

@mathause
Copy link
Collaborator

MCVE Code Sample

import numpy as np
import xarray as xr

data = np.random.randn(10, 10)
data = np.abs(data)

da = xr.DataArray(data)

# returns a diverging colormap
da.plot(vmax=2, center=0, extend="both")

# returns a sequential colormap
da.plot(levels=[-2, -1, 0, 1, 2], extend="both")

tst

Expected Output

A diverging colormap, maybe?

Problem Description

I was surprised by getting the viridis colormap until I realised that my data must all be positive and the colormap is infered from the data and not from levels. However, when specifying the range via vmax=2, center=0 it is not inferred from the data.

Output of xr.show_versions()

INSTALLED VERSIONS ------------------ commit: None python: 3.7.3 | packaged by conda-forge | (default, Jul 1 2019, 21:52:21) [GCC 7.3.0] python-bits: 64 OS: Linux OS-release: 4.12.14-lp151.28.25-default machine: x86_64 processor: x86_64 byteorder: little LC_ALL: None LANG: en_GB.UTF-8 LOCALE: en_US.UTF-8 libhdf5: 1.10.4 libnetcdf: 4.6.2

xarray: 0.14.0+44.g4dce93f1
pandas: 0.25.2
numpy: 1.17.3
scipy: 1.3.1
netCDF4: 1.5.0.1
pydap: None
h5netcdf: 0.7.4
h5py: 2.9.0
Nio: None
zarr: None
cftime: 1.0.4.2
nc_time_axis: 1.2.0
PseudoNetCDF: None
rasterio: 1.0.22
cfgrib: None
iris: None
bottleneck: 1.2.1
dask: 2.6.0
distributed: 2.6.0
matplotlib: 3.1.1
cartopy: 0.17.0
seaborn: 0.9.0
numbagg: None
setuptools: 41.4.0
pip: 19.3.1
conda: None
pytest: 5.2.2
IPython: 7.9.0
sphinx: 2.2.1

@dcherian
Copy link
Contributor

dcherian commented Nov 13, 2019

hmm... it's also weird if you specified vmax=2, vmin=-2. For some reason, auto-inferred colormaps are disabled in that case.

Here's where you should look if you want to jump in :)

xarray/xarray/plot/utils.py

Lines 141 to 154 in 810345c

def _determine_cmap_params(
plot_data,
vmin=None,
vmax=None,
cmap=None,
center=None,
robust=False,
extend=None,
levels=None,
filled=True,
norm=None,
):
"""
Use some heuristics to set good defaults for colorbar and range.

@shoyer
Copy link
Member

shoyer commented Mar 29, 2020

It might be worth taking a look at updating our heuristics. They were originally copied from seaborn, but seaborn has since updated them to something a little different.

dcherian added a commit that referenced this issue Apr 5, 2020
* Use divergent colormap if lowest and highest level span 0

Fixes #3524

* sort levels by default.

* better levels check
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants