Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Calling scatterplot with size and hue raises TypeError #3641

Open
ChristofKaufmann opened this issue Feb 28, 2024 · 3 comments
Open

Calling scatterplot with size and hue raises TypeError #3641

ChristofKaufmann opened this issue Feb 28, 2024 · 3 comments

Comments

@ChristofKaufmann
Copy link

This small example

import seaborn as sns
df = sns.load_dataset('attention', index_col=0)
sns.scatterplot(df, x='subject', y='score', s=df['solutions']*20, hue='attention')

results in a TypeError.

Traceback
TypeError                                 Traceback (most recent call last)
File example.py:3
      1 import seaborn as sns
      2 df = sns.load_dataset('attention', index_col=0)
----> 3 sns.scatterplot(df, x='subject', y='score', s=df['solutions']*20, hue='attention')

File ~/.local/opt/mambaforge/envs/ml/lib/python3.10/site-packages/seaborn/relational.py:624, in scatterplot(data, x, y, hue, size, style, palette, hue_order, hue_norm, sizes, size_order, size_norm, markers, style_order, legend, ax, **kwargs)
    621 color = kwargs.pop("color", None)
    622 kwargs["color"] = _default_color(ax.scatter, hue, color, kwargs)
--> 624 p.plot(ax, kwargs)
    626 return ax

File ~/.local/opt/mambaforge/envs/ml/lib/python3.10/site-packages/seaborn/relational.py:458, in _ScatterPlotter.plot(self, ax, kws)
    456 if self.legend:
    457     attrs = {"hue": "color", "size": "s", "style": None}
--> 458     self.add_legend_data(ax, _scatter_legend_artist, kws, attrs)
    459     handles, _ = ax.get_legend_handles_labels()
    460     if handles:

File ~/.local/opt/mambaforge/envs/ml/lib/python3.10/site-packages/seaborn/_base.py:1270, in VectorPlotter.add_legend_data(self, ax, func, common_kws, attrs, semantic_kws)
   1268     if attr in kws:
   1269         level_kws[attr] = kws[attr]
-> 1270 artist = func(label=label, **{"color": ".2", **common_kws, **level_kws})
   1271 if _version_predates(mpl, "3.5.0"):
   1272     if isinstance(artist, mpl.lines.Line2D):

File ~/.local/opt/mambaforge/envs/ml/lib/python3.10/site-packages/seaborn/utils.py:927, in _scatter_legend_artist(**kws)
    924     else:
    925         line_kws["markeredgecolor"] = edgecolor
--> 927 return mpl.lines.Line2D([], [], **line_kws)

File ~/.local/opt/mambaforge/envs/ml/lib/python3.10/site-packages/matplotlib/lines.py:393, in Line2D.__init__(self, xdata, ydata, linewidth, linestyle, color, gapcolor, marker, markersize, markeredgewidth, markeredgecolor, markerfacecolor, markerfacecoloralt, fillstyle, antialiased, dash_capstyle, solid_capstyle, dash_joinstyle, solid_joinstyle, pickradius, drawstyle, markevery, **kwargs)
    391 self.set_markevery(markevery)
    392 self.set_antialiased(antialiased)
--> 393 self.set_markersize(markersize)
    395 self._markeredgecolor = None
    396 self._markeredgewidth = None

File ~/.local/opt/mambaforge/envs/ml/lib/python3.10/site-packages/matplotlib/lines.py:1265, in Line2D.set_markersize(self, sz)
   1256 def set_markersize(self, sz):
   1257     """
   1258     Set the marker size in points.
   1259 
   (...)
   1263          Marker size, in points.
   1264     """
-> 1265     sz = float(sz)
   1266     if self._markersize != sz:
   1267         self.stale = True

File ~/.local/opt/mambaforge/envs/ml/lib/python3.10/site-packages/pandas/core/series.py:248, in _coerce_method.<locals>.wrapper(self)
    240     warnings.warn(
    241         f"Calling {converter.__name__} on a single element Series is "
    242         "deprecated and will raise a TypeError in the future. "
   (...)
    245         stacklevel=find_stack_level(),
    246     )
    247     return converter(self.iloc[0])
--> 248 raise TypeError(f"cannot convert the series to {converter}")

TypeError: cannot convert the series to <class 'float'>

This applies to versions:

  • 0.13.2
  • 0.13.1
  • 0.13.0

but not to 0.12.2. I did not test current git development version.

When calling scatterplot with hue a legend will be added. Together with s with different sizes this bug is triggered. The bug is not triggered, when...

  • setting legend=False
  • removing the hue argument (because no legend)
  • removing the s argument
  • setting the s argument to a scaler, like s=20

My interpretation is that the legend artist should receive a scaler, but gets an array. So maybe this issue has the same roots as #3575.

@mwaskom
Copy link
Owner

mwaskom commented Feb 28, 2024

Why not use seaborn's size parameter here?

@ChristofKaufmann
Copy link
Author

A colleague and I were so focused on the difference by the version, that we just didn't think of it. Thank you!

@mwaskom
Copy link
Owner

mwaskom commented Feb 29, 2024

I do think it’s a real issue and know the cause (0.13.0 started propagating more user kwargs to the legend artists, but artists don’t have a s parameter). Just mentioning size= as in most cases I’d think that’s preferred (you don’t need to scale the units yourself and you get a legend).

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

No branches or pull requests

2 participants