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

cbar_kwargs are ignored for plot.contourf #2982

Open
jbusecke opened this issue May 22, 2019 · 3 comments
Open

cbar_kwargs are ignored for plot.contourf #2982

jbusecke opened this issue May 22, 2019 · 3 comments

Comments

@jbusecke
Copy link
Contributor

Code Sample, a copy-pastable example if possible

I just stumbled on some weird behaviour, possibly a bug. When plotting with the contourf method, xarray ignores the extend value passed to cbar_kwargs

import numpy as np
import xarray as xr
%matplotlib inline

test_data = np.random.rand(10, 10)
da = xr.DataArray(test_data)
h = da.plot.contourf(vmax=0.5, cbar_kwargs={"extend": "neither"})
print(h.colorbar.extend)

download-2

When doing a similar plot with matplotlib this happens

# matplotlib example
import matplotlib.pyplot as plt
ha = plt.contourf(test_data, vmax=0.3)
cb = plt.colorbar(ha, extend='neither')

download-3

Is xarray adjusting the colorbar to the vmin and vmax inputs but not carrying the cbar_kwargs for that step?

This makes it especially hard to produce movies with (e.g. xmovie) because the colorbar flickers.

If someone could point me to the right location in the code and tests, I would be happy to put together a PR.

Output of xr.show_versions()

INSTALLED VERSIONS ------------------ commit: None python: 3.6.7 | packaged by conda-forge | (default, Feb 28 2019, 09:07:38) [GCC 7.3.0] python-bits: 64 OS: Linux OS-release: 2.6.32-696.30.1.el6.x86_64 machine: x86_64 processor: x86_64 byteorder: little LC_ALL: None LANG: en_US LOCALE: en_US.ISO8859-1 libhdf5: 1.10.4 libnetcdf: 4.6.2

xarray: 0.12.0
pandas: 0.24.2
numpy: 1.16.2
scipy: 1.2.1
netCDF4: 1.5.0.1
pydap: None
h5netcdf: None
h5py: None
Nio: None
zarr: 2.3.1
cftime: 1.0.3.4
nc_time_axis: 1.2.0
PseudonetCDF: None
rasterio: None
cfgrib: None
iris: None
bottleneck: None
dask: 1.1.5
distributed: 1.26.1
matplotlib: 3.0.3
cartopy: 0.17.0
seaborn: 0.9.0
setuptools: 40.8.0
pip: 19.0.3
conda: None
pytest: 4.4.2
IPython: 7.1.1
sphinx: None

@dcherian
Copy link
Contributor

If you pass extend='neither' directly it should work. There is a _process_cmap_params_cbar_kwargs in plot/utils.py (I think) that deals with this.

@jbusecke
Copy link
Contributor Author

If I understand correctly then it gets piped through cmap_kwargs, which seems odd to me. Do you agree that this is a bug? Or am I missing a case where it would be preferable to pass extend directly to plot.contourf?

@dcherian
Copy link
Contributor

dcherian commented May 23, 2019

Yes looks like _add_colorbar in plot/utils.py works around this :) in this line:

cbar_kwargs.setdefault('extend', cmap_params['extend'])

Now I'm confused why your initial statement didn't work.

It's a little funny because _determine_cmap_params autosets extend based on vmin, vmax. I think you should be able to modify _process_cmap_... to do the right thing so that cmap_params needn't be provided to _add_colorbar in _plot2d.

I agree extend is really a cbar_kwarg. Can you send in a PR to fix this?

By do the right thing I mean move extend to cbar_kwargs

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

No branches or pull requests

2 participants