Skip to content

Fix inconsistent hue color mapping in plot.line#11164

Open
Mr-Neutr0n wants to merge 2 commits intopydata:mainfrom
Mr-Neutr0n:fix/line-hue-color-grouping
Open

Fix inconsistent hue color mapping in plot.line#11164
Mr-Neutr0n wants to merge 2 commits intopydata:mainfrom
Mr-Neutr0n:fix/line-hue-color-grouping

Conversation

@Mr-Neutr0n
Copy link
Contributor

Summary

Fixes #10998

When plot.line receives a hue coordinate with duplicate values (e.g., ["a", "a", "b", "b"]), lines sharing the same hue value now get the same color — matching the behavior of plot.scatter.

Before: ax.plot was called with the full 2D array, so matplotlib assigned each column a new color from the property cycle regardless of hue grouping. With categories = ["a", "a", "b", "b", "c", "c"], all 6 lines got different colors even though there are only 3 unique hue values.

After: Lines are grouped by unique hue value and plotted per-group with a consistent color. The legend is also deduplicated to show one entry per unique hue value.

Changes

  • xarray/plot/dataarray_plot.py — In the line() function, instead of passing the full 2D array to ax.plot, iterate over unique hue values and plot each group with a color from the property cycle. Handles both x= and y= orientations (where either xplt_val or yplt_val may be 2D). Respects user-specified color/c kwargs.

Test plan

  • All 519 existing tests in test_plot.py pass (including test_line_plot_along_1d_coord, test_2d_line, and all line-related tests)
  • Verified with the exact reproduction script from the issue: lines with hue "a" share one color, "b" another, "c" another, and the legend shows 3 deduplicated entries

When plot.line receives a hue coordinate with duplicate values (e.g.,
["a", "a", "b", "b"]), lines that share the same hue value should be
drawn with the same color—matching the behavior of plot.scatter.

Previously, ax.plot was called with the full 2D array, assigning each
column a new color from the property cycle regardless of hue grouping.
Now lines are grouped by unique hue values and plotted per-group with
a consistent color. The legend is also deduplicated to show one entry
per unique hue value.

Fixes pydata#10998
@ianhi
Copy link
Collaborator

ianhi commented Feb 13, 2026

Hi @Mr-Neutr0n thanks for the PR! Can you please post a before an after picture of what this produces.

Also did you explore if there was a simpler way to leverage the matplotlib API here, it seems like we've pulled in a lot of logic to work around something that might be easier to fix with a matplotlib argument. It also might not be, but I'd like to be sure.

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Inconsistent behavior for hue between scatter and line

2 participants