Fix inconsistent hue color mapping in plot.line#11164
Open
Mr-Neutr0n wants to merge 2 commits intopydata:mainfrom
Open
Fix inconsistent hue color mapping in plot.line#11164Mr-Neutr0n wants to merge 2 commits intopydata:mainfrom
Mr-Neutr0n wants to merge 2 commits intopydata:mainfrom
Conversation
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
5 tasks
for more information, see https://pre-commit.ci
Collaborator
|
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. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Fixes #10998
When
plot.linereceives 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 ofplot.scatter.Before:
ax.plotwas called with the full 2D array, so matplotlib assigned each column a new color from the property cycle regardless of hue grouping. Withcategories = ["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 theline()function, instead of passing the full 2D array toax.plot, iterate over unique hue values and plot each group with a color from the property cycle. Handles bothx=andy=orientations (where eitherxplt_valoryplt_valmay be 2D). Respects user-specifiedcolor/ckwargs.Test plan
test_plot.pypass (includingtest_line_plot_along_1d_coord,test_2d_line, and all line-related tests)"a"share one color,"b"another,"c"another, and the legend shows 3 deduplicated entries