Skip to content

plot.scatter(hue_style="discrete") does nothing #7907

Closed
@mgunyho

Description

@mgunyho

What happened?

I was trying to do a scatterplot of my data with one dimension determining the color. The dimension has only a few values so I used hue_style="discrete" to have a different color for each value. However, the resulting scatterplot has a continuous colorbar, which is the same as when I pass hue_style="continuous":

image

What did you expect to happen?

The colorbar should have discrete colors. I was also expecting the colors to be from the default matplotlib color palette, C0, C1, etc, when there's less than 10 items, like this:

image

Although the examples in the documentation show the discrete case also using viridis.

What I was really expecting is a plot like one would get by passing add_colorbar=False, add_legend=True:

image

But that may be a bit too automagical.

Minimal Complete Verifiable Example

import matplotlib.pyplot as plt
import numpy as np
import xarray as xr

x = xr.DataArray(
    np.random.default_rng().random((10, 3)),
    coords=[
        ("idx", np.linspace(0, 1, 10)),
        ("color", [1, 2, 3]),
    ]
)
y = x + np.random.default_rng().random(x.shape)

ds = xr.Dataset({
   "x": x,
   "y": y,
})

# the output is the same regardless of hue_style="discrete" or "continuous" or just leaving it out
ds.plot.scatter(x="x", y="y", hue="color", hue_style="discrete", ax=plt.figure().gca())

MVCE confirmation

  • Minimal example — the example is as focused as reasonably possible to demonstrate the underlying issue in xarray.
  • Complete example — the example is self-contained, including all data and the text of any traceback.
  • Verifiable example — the example copy & pastes into an IPython prompt or Binder notebook, returning the result.
  • New issue — a search of GitHub Issues suggests this is not a duplicate.

Relevant log output

No response

Anything else we need to know?

This is the code for the "expected" plot:

from matplotlib.colors import ListedColormap

ds.plot.scatter(
    x="x",
    y="y",
    hue="color",
    hue_style="discrete",
    ax=plt.figure().gca(),

    # these lines added in addition to the MVCE
    cmap=ListedColormap(["C0", "C1", "C2"]),
    vmin=0.5, vmax=3.5,
    cbar_kwargs=dict(ticks=ds.color.data),
)

Environment

INSTALLED VERSIONS

commit: None
python: 3.8.10 (default, May 26 2023, 14:05:08)
[GCC 9.4.0]
python-bits: 64
OS: Linux
OS-release: 5.14.0-1059-oem
machine: x86_64
processor: x86_64
byteorder: little
LC_ALL: None
LANG: en_US.UTF-8
LOCALE: ('en_US', 'UTF-8')
libhdf5: None
libnetcdf: None

xarray: 2023.1.0
pandas: 1.4.3
numpy: 1.23.0
scipy: None
netCDF4: None
pydap: None
h5netcdf: None
h5py: None
Nio: None
zarr: None
cftime: None
nc_time_axis: None
PseudoNetCDF: None
rasterio: None
cfgrib: None
iris: None
bottleneck: None
dask: None
distributed: None
matplotlib: 3.5.3
cartopy: None
seaborn: None
numbagg: None
fsspec: None
cupy: None
pint: None
sparse: None
flox: None
numpy_groupies: None
setuptools: 44.0.0
pip: 20.0.2
conda: None
pytest: None
mypy: None
IPython: 8.12.2
sphinx: None

I also tried this on main at 3459e6f, the behavior is the same.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions