1

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.

Trenton McKinney
  • 56,955
  • 33
  • 144
  • 158
  • This is essentially a duplicate of [FutureWarning: Pass the following variables as keyword args: x, y](https://stackoverflow.com/q/64130332/7758804) – Trenton McKinney Mar 24 '23 at 16:00

1 Answers1

2

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
Corralien
  • 109,409
  • 8
  • 28
  • 52
  • why do you use sns instead of sb (oh i know, i imported seaborn as sb instead of sns)? and also if i update to seaborn 0.12 both ways will work? how do i set the value of x? it will not automatically take it from the hp dataframe i created? thanks!! – Clarissa Chen Mar 24 '23 at 08:06
  • Oh sorry, the habit. In general, we use `import seaborn as sns` because that's what the [official documentation](https://seaborn.pydata.org/tutorial/introduction.html) recommends. To specify `x`, use `sns.boxplot(data=hp, x='mycolforx', y='mycolfory')` – Corralien Mar 24 '23 at 08:10
  • @ClarissaChen [Official FAQ: Why is seaborn imported as sns?](https://seaborn.pydata.org/faq.html#why-is-seaborn-imported-as-sns) and [Why import seaborn as sns?](https://stackoverflow.com/q/41499857/7758804) – Trenton McKinney Mar 24 '23 at 20:46