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
Ran on both windows 10 and Google Colab.
lmplot
and alarmed that it's in that book!.
lmplot
draws separate scatterplots for each level of hue
, but the matplotlib kwargs
are just passed straight through to scatter, so those vectors are going to have different lengths.seaborn 0.11.1
and matplotlib 3.4.2
sns.scatterplot
import seaborn as sns
tips = sns.load_dataset('tips')
sns.scatterplot(data=tips, x="total_bill", y="tip", hue="sex", size='size')
sns.scatterplot(data=tips, x="total_bill", y="tip", hue="sex", s=tips["size"].mul(20))
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')
sns.relplot(data=tips, x='total_bill', y='tip', hue='sex', s=tips['size'].mul(20))