Skip to content

Fix scatterplot legend with vector-valued s, linewidth or alpha - #4037

Open
twelfthlabor wants to merge 1 commit into
mwaskom:masterfrom
twelfthlabor:fix/scatter-legend-array-kwargs
Open

twelfthlabor wants to merge 1 commit into
mwaskom:masterfrom
twelfthlabor:fix/scatter-legend-array-kwargs

Conversation

@twelfthlabor

@twelfthlabor twelfthlabor commented Nov 16, 2026 •

Copy link
Copy Markdown

Fixes #3641

scatterplot and relplot raised when s, linewidth or alpha was given one value per point together with a semantic that draws a legend:

df = sns.load_dataset("attention", index_col=0)
sns.scatterplot(df, x="subject", y="score", s=df["solutions"] * 20, hue="attention")
# TypeError: float() argument must be a string or a real number, not 'Series'

Since 0.13.0 the keyword arguments are passed on to the legend artists, and _scatter_legend_artist gave the whole vector to a single Line2D. ax.scatter accepts a vector for these three properties, and the plot itself was drawn correctly, only the legend failed. Without a legend (no hue/size/style, or legend=False) the same call already worked.

A vector can't be shown by one legend entry, so _scatter_legend_artist now drops a non-scalar s, linewidth or alpha and the handle uses the same default as when the keyword isn't passed. Scalar values are passed on as before.

Not changed: relplot with col/row and a vector s still fails, because the vector isn't split by facet. That comes from FacetGrid and isn't related to the legend.

Tests: the new TestScatterPlotter::test_legend_vector_kws (scatterplot and relplot, for each of the three keywords) fails on master in all 6 cases and passes with the change. The full suite passes with MPLBACKEND=agg (2373 passed) except test_utils.py::test_ticklabels_overlap, which also fails for me on master and doesn't use this code. flake8 is clean for the changed lines.

@twelfthlabor
twelfthlabor force-pushed the fix/scatter-legend-array-kwargs branch from 49b6242 to 51cdd3d Compare November 16, 2026 15:03
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Calling scatterplot with size and hue raises TypeError

1 participant