Keyreall
Keyreall

Reputation: 97

Draw boxplot seaborn without groups

I want to draw boxplot in seaborn (not matplotlib), but it gives me an error "The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all()."

I didn't find anything like my data on online documentation or other tutorials. My data looks so simple it is just a bunch of observation: e.g

  1.       2.     ...     ... 
1.010.   2.343.   ...
2.030.   2.534.   ...
2.433.   2.748.   ...
1.933.   2.432.   ...
  ...     ...

I converted my data to pandas.DataFrame and it looks like what I wrote above (number of observation is the first string(pandas.columns) and values are below). It doesn't work sns.boxplot(data). May be there are some arguments or something else. Could you help me? Thanks.

Upvotes: 0

Views: 356

Answers (2)

JohanC
JohanC

Reputation: 80329

Supposing you are using seaborn 0.11.1, you can directly call sns.boxplot:

import seaborn as sns
import pandas as pd

df = pd.DataFrame({'1': [1.010, 2.030, 2.433, 1.933], 
                   '2': [ 2.343, 2.534, 2.748, 2.432]})
sns.boxplot(data=df)

boxplot from simple dataframe

Upvotes: 1

Tom McLean
Tom McLean

Reputation: 6327

A boxplot needs a list of list of numbers to plot, or if it is a dataframe it will plot each column as a boxplot, but each column needs to have the same length of data. Try converting your series to a list and plotting like so:

import seaborn as sns

fig, ax = plt.subplots()
sns.boxplot(ax=ax, data = data_df_series.tolist())

If this doesnt work, what is the data type of your series? Try doing:

df.dtype

Additonally, are there any NaN values?

Also, you do not need to convert your data to a pandas dataframe, as it will just be converted to a list before being plotted beforehand. If it is not already a pandas dataframe, you can keep it as whatever it was before (i.e. a list or a numpy array)

Upvotes: 1

Related Questions