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

Contour plot with norm and int or no levels fails. #3735

Closed
juseg opened this issue Jan 31, 2020 · 1 comment · Fixed by #3914
Closed

Contour plot with norm and int or no levels fails. #3735

juseg opened this issue Jan 31, 2020 · 1 comment · Fixed by #3914

Comments

@juseg
Copy link
Contributor

juseg commented Jan 31, 2020

MCVE Code Sample

import numpy as np
import xarray as xr
import matplotlib as mpl
data = xr.DataArray(np.arange(24).reshape(4, 6))
data.plot.contour(norm=mpl.colors.Normalize(vmin=5, vmax=10))

Yields:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File ".../xarray/xarray/plot/plot.py", line 816, in plotmethod
    return newplotfunc(**allargs)
  File ".../xarray/xarray/plot/plot.py", line 694, in newplotfunc
    cmap_params, cbar_kwargs = _process_cmap_cbar_kwargs(
  File ".../xarray/xarray/plot/utils.py", line 772, in _process_cmap_cbar_kwargs
    cmap_params = _determine_cmap_params(**cmap_kwargs)
  File ".../xarray/xarray/plot/utils.py", line 278, in _determine_cmap_params
    cmap, newnorm = _build_discrete_cmap(cmap, levels, extend, filled)
  File ".../xarray/xarray/plot/utils.py", line 94, in _build_discrete_cmap
    n_colors = len(levels) + ext_n - 1

Expected Output

A plot.

Problem Description

I think this has to do with:

levels = 7 # this is the matplotlib default

And the line where the code fails:

n_colors = len(levels) + ext_n - 1

There should be an easy fix, I think.

Output of xr.show_versions()

INSTALLED VERSIONS

commit: None
python: 3.8.1 (default, Jan 22 2020, 06:38:00)
[GCC 9.2.0]
python-bits: 64
OS: Linux
OS-release: 4.19.98-1-MANJARO
machine: x86_64
processor:
byteorder: little
LC_ALL: None
LANG: fr_CH.utf8
LOCALE: fr_CH.UTF-8
libhdf5: 1.10.5
libnetcdf: 4.7.3

xarray: 0.14.1
pandas: 0.25.3
numpy: 1.18.1
scipy: 1.4.1
netCDF4: 1.5.3
pydap: None
h5netcdf: None
h5py: None
Nio: None
zarr: None
cftime: 1.0.4.2
nc_time_axis: None
PseudoNetCDF: None
rasterio: 1.1.1
cfgrib: None
iris: None
bottleneck: None
dask: 2.8.0
distributed: None
matplotlib: 3.1.2
cartopy: 0.17.0
seaborn: None
numbagg: None
setuptools: 44.0.0
pip: 19.3
conda: None
pytest: 5.3.4
IPython: 7.11.1
sphinx: 2.2.1

@mathause
Copy link
Collaborator

mathause commented Feb 3, 2020

I think the problem is in _determine_cmap_params:

if levels is not None and norm is None:

This may have to be:

if levels is not None:

Introducing this change does not break any test (py.test xarray/tests/test_plot.py) and runs your example successfully. However, it then causes the same problem as in #3744.

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