What is the difference between these seaborn.boxplot implementations?
Question:
My current prof entered this code in our tutorial jupyter notebook:
f = plt.figure(figsize=(24, 4))
sb.boxplot(data = hp, orient = "h")
but in previous year’s tutorial videos, this was the code:
f, axes = plt.subplots(1, 1, figsize=(24,4))
sb.boxplot(hp, orient = "h")
I have tried to run the 2nd code but it shows up as an error
The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().
This was the error that I got. I am not sure why it can run on the previous year’s tutorial videos but can’t on my notebook.
Answers:
Your error is not about matplotlib
, but seaborn
. You probably use a 0.11 version.
The boxplot
function signature for 0.11 series is:
sns.boxplot(*, x=None, y=None, hue=None, data=None, ..., **kwargs)
So when you use sns.boxplot(hp, orient='h')
, hp
is the value for x
and not data
parameter. The first version of your code works because you explicitly named the data
parameter.
Since 0.12 version, the signature is:
sns.boxplot(data=None, *, x=None, y=None, ..., **kwargs)
The only unnamed argument allowed is data
, so both of your code will work.
Solution: update to Seaborn 0.12 or use data=hp
as parameter of boxplot
(and other functions).
Summary:
# version 0.11
sns.boxplot(data=hp, orient='h') # works
sns.boxplot(hp, orient='h') # failed
# version 0.12
sns.boxplot(data=hp, orient='h') # works
sns.boxplot(hp, orient='h') # works
My current prof entered this code in our tutorial jupyter notebook:
f = plt.figure(figsize=(24, 4))
sb.boxplot(data = hp, orient = "h")
but in previous year’s tutorial videos, this was the code:
f, axes = plt.subplots(1, 1, figsize=(24,4))
sb.boxplot(hp, orient = "h")
I have tried to run the 2nd code but it shows up as an error
The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().
This was the error that I got. I am not sure why it can run on the previous year’s tutorial videos but can’t on my notebook.
Your error is not about matplotlib
, but seaborn
. You probably use a 0.11 version.
The boxplot
function signature for 0.11 series is:
sns.boxplot(*, x=None, y=None, hue=None, data=None, ..., **kwargs)
So when you use sns.boxplot(hp, orient='h')
, hp
is the value for x
and not data
parameter. The first version of your code works because you explicitly named the data
parameter.
Since 0.12 version, the signature is:
sns.boxplot(data=None, *, x=None, y=None, ..., **kwargs)
The only unnamed argument allowed is data
, so both of your code will work.
Solution: update to Seaborn 0.12 or use data=hp
as parameter of boxplot
(and other functions).
Summary:
# version 0.11
sns.boxplot(data=hp, orient='h') # works
sns.boxplot(hp, orient='h') # failed
# version 0.12
sns.boxplot(data=hp, orient='h') # works
sns.boxplot(hp, orient='h') # works