Skip to content

Bug in legend of dataset.plot.scatter #4126

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

Closed
yohai opened this issue Jun 7, 2020 · 3 comments · Fixed by #4411
Closed

Bug in legend of dataset.plot.scatter #4126

yohai opened this issue Jun 7, 2020 · 3 comments · Fixed by #4411

Comments

@yohai
Copy link
Contributor

yohai commented Jun 7, 2020

When using Dataset.scatter with hue being a variable of dtype string, the legend turns out to be wrong.

MCVE Code Sample

import xarray as xr
import numpy as np
dd = xr.Dataset({'y': (['x'], np.arange(8)),
                 'label': (['x'], list('AABBCCDD'))},
                coords={'x': np.linspace(0,1,8)})
dd.plot.scatter(x='x', y='y', hue='label')

Output is (note the legend):
Figure_1

Playing around it seems that it always chooses the first 4 values as the legend labels (note that the order of colors of the points is correct):

import xarray as xr
import numpy as np
dd = xr.Dataset({'y': (['x'], np.arange(8)),
                 'label': (['x'], list('ABBACDDC'))},
                coords={'x': np.linspace(0,1,8)})
dd.plot.scatter(x='x', y='y', hue='label')

Figure_2

And if there are only 3 labels in total it chooses the first 3:

import xarray as xr
import numpy as np
dd = xr.Dataset({'y': (['x'], np.arange(6)),
                 'label': (['x'], list('ABBACC'))},
                coords={'x': np.linspace(0,1,6)})
dd.plot.scatter(x='x', y='y', hue='label')

Figure_1

Expected Output

Legend in first two plots should read 'ABCD' and last plot 'ABC'

Versions

Output of xr.show_versions()

INSTALLED VERSIONS

commit: None
libhdf5: 1.10.4
libnetcdf: None

xarray: 0.15.1
pandas: 0.25.1
numpy: 1.17.2
scipy: 1.3.1
netCDF4: None
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: 1.2.1
dask: 2.5.2
distributed: 2.5.2
matplotlib: 3.1.1
cartopy: None
seaborn: 0.9.0
numbagg: None
setuptools: 41.4.0
pip: 19.2.3
conda: 4.8.1
pytest: 5.2.1
IPython: 7.8.0
sphinx: 2.2.0

@yohai
Copy link
Contributor Author

yohai commented Jun 7, 2020

calling @dcherian for the rescue!

@phausamann
Copy link
Contributor

phausamann commented Sep 4, 2020

The issue seems to be that the legend labels are overwritten incorrectly here. Removing the labels parameter fixes the problem for me.

phausamann added a commit to phausamann/xarray that referenced this issue Sep 7, 2020
dcherian added a commit that referenced this issue Sep 10, 2020
* Fix incorrect legend labels for Dataset.plot.scatter
Closes #4126

* Update xarray/tests/test_plot.py

Co-authored-by: Deepak Cherian <[email protected]>
@yohai
Copy link
Contributor Author

yohai commented Sep 13, 2020

thanks @dcherian & @phausamann !

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