xarray icon indicating copy to clipboard operation
xarray copied to clipboard

Inconsistent behavior for `hue` between scatter and line

Open ianhi opened this issue 3 months ago • 1 comments

What happened?

The behavior of hue results in different colors ds.plot.scatter vs ds.plot.line

Both docstrings describe hue as:

hue (Hashable, optional) – Dimension or coordinate for which you want multiple lines plotted. If plotting against a 2D coordinate, hue must be a dimension.

Which they both faithfully do, but despite being named hue they have inconsistent color behavior.

What did you expect to happen?

consistent behavior where each line that corresponds to the same hue has the same color (like in scatter). What I actually got is:

Image

Minimal Complete Verifiable Example

# /// script
# requires-python = ">=3.11"
# dependencies = [
#   "xarray[complete]==2025.10.0"
# ]
# ///

import matplotlib.pyplot as plt

import xarray as xr

xr.show_versions()
# your reproducer code ...


import numpy as np

T = np.linspace(0, 2 * np.pi, 50)
data = np.sin(np.ones([6, 50]) * T)
# data = np.sin(T)
# data =data[None, :], (6,50)
for i in range(6):
    data[i, :] += i

categories = ["a", "a", "b", "b", "c", "c"]
da = xr.DataArray(data, dims=("category", "T"), coords={"category": categories, "T": T})
fig, axs = plt.subplots(1, 2, figsize=(10, 4))
da.plot.scatter(hue="category", ax=axs[0])

da.plot.line(hue="category", ax=axs[1])
axs[0].set_title("scatter")
axs[1].set_title("line")
plt.show()

Steps to reproduce

uv run above script

MVCE confirmation

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

Relevant log output


Anything else we need to know?

I think this is related to, but distinct from https://github.com/pydata/xarray/issues/10674

Environment

INSTALLED VERSIONS

commit: None python: 3.13.0 (main, Oct 16 2024, 08:05:40) [Clang 18.1.8 ] python-bits: 64 OS: Darwin OS-release: 24.6.0 machine: arm64 processor: arm byteorder: little LC_ALL: None LANG: en_US.UTF-8 LOCALE: ('en_US', 'UTF-8') libhdf5: 1.14.6 libnetcdf: 4.9.3

xarray: 2025.9.2.dev1+geed12c498 pandas: 2.3.3 numpy: 2.3.5 scipy: 1.16.3 netCDF4: 1.7.3 pydap: 3.5.8 h5netcdf: 1.7.3 h5py: 3.15.1 zarr: 3.1.5 cftime: 1.6.5 nc_time_axis: 1.4.1 iris: None bottleneck: 1.6.0 dask: 2025.11.0 distributed: 2025.11.0 matplotlib: 3.10.7 cartopy: 0.25.0 seaborn: 0.13.2 numbagg: 0.9.3 fsspec: 2025.12.0 cupy: None pint: None sparse: 0.17.0 flox: 0.10.7 numpy_groupies: 0.11.3 setuptools: None pip: None conda: None pytest: None mypy: None IPython: None sphinx: None

ianhi avatar Dec 08 '25 21:12 ianhi

There is https://github.com/pydata/xarray/pull/7173 that will work in similar fashion as plot.scatter.

plot.line basically just calls ax.plot in a loop, which is trickier to match with ax.scatter.

Illviljan avatar Dec 08 '25 23:12 Illviljan