seaborn.lmplot raises ValueError when set both `hue` and `scatter_kws`'s `s`

Question:

This code is provided in the book "Pandas for everyone" (3.4.3.2 Size and Shape).

import seaborn as sns
from matplotlib import pyplot as plt

tips = sns.load_dataset('tips')
sns.lmplot(
    x='total_bill',
    y='tip',
    data=tips,
    fit_reg=False,
    hue='sex',
    scatter_kws={'s': tips['size'] * 10}
)
plt.show()

When I run this code, it results ValueError at matplotlib/axes/_axes.py line 4508.

ValueError: s must be a scalar, or float array-like with the same size
as x and y

This error won’t be raised if I omit the hue argument. It seems that the data (tips['total_bill'] and tips['tip']) are split by sex, but tips['size'] is not split, so the lengths are different.

How can I manage to plot the figure without error?

Versions

  • Python 3.7
  • matplotlib 3.4.2
  • seaborn 0.11.1

Ran on both windows 10 and Google Colab.

Asked By: noobar

||

Answers:

Use sns.scatterplot

import seaborn as sns

tips = sns.load_dataset('tips')

sns.scatterplot(data=tips, x="total_bill", y="tip", hue="sex", size='size')

enter image description here

sns.scatterplot(data=tips, x="total_bill", y="tip", hue="sex", s=tips["size"].mul(20))

enter image description here

Use sns.relplot

import seaborn as sns

tips = sns.load_dataset('tips')

sns.relplot(data=tips, kind='scatter', x="total_bill", y="tip", hue="sex", size='size')

enter image description here

sns.relplot(data=tips, x='total_bill', y='tip', hue='sex', s=tips['size'].mul(20))

enter image description here

Answered By: Trenton McKinney