Issue
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.
Solution
- As per seaborn issue 2621, lmplot with scatter_kws or relplot with s: ValueError: s must be a scalar, or float array-like with the same size as x and y, the seaborn main developer states this should not work with
lmplot
and alarmed that it's in that book!.- I believe the reason it stopped "working" is that matplotlib added some input validation to catch cases like this.
lmplot
draws separate scatterplots for each level ofhue
, but the matplotlibkwargs
are just passed straight through to scatter, so those vectors are going to have different lengths.
- Using
seaborn 0.11.1
andmatplotlib 3.4.2
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')
sns.scatterplot(data=tips, x="total_bill", y="tip", hue="sex", s=tips["size"].mul(20))
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')
sns.relplot(data=tips, x='total_bill', y='tip', hue='sex', s=tips['size'].mul(20))
Answered By - Trenton McKinney
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.