Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Seaborn boxplots shifted incorrectly along x-axis

from matplotlib import pyplot as plt
import pandas as pd
import seaborn as sns

df = pd.DataFrame({})
df[soi_name]=soi
df[outcome_name]=outcome
soi,outcome = utils.format_cols(soi, outcome,'continuous',agg_method)
sns.factorplot(data=df, x=outcome_name,y=soi_name,hue=outcome_name,kind='box')
plt.savefig(ofilepath)

enter image description here

So the code snippet used to generate this boxplot is just above. outcome is a binary float type pandas series. soi is a float type pandas series. This x-axis shift occurs for boxplots and violinplots. When I generate factor plots with the following code:

df = pd.DataFrame({})
df[soi_name]=soi
df[outcome_name]=outcome
sns.factorplot(data=df, x=outcome_name,y=soi_name,hue=outcome_name)
plt.savefig(ofilepath)

... enter image description here

I get my desired outputs. Any ideas with why the shift for the box plots might be happening?

like image 257
LogCapy Avatar asked Mar 02 '18 23:03

LogCapy


People also ask

How do I change the boxplot orientation in Seaborn?

We can turn the boxplot into a horizontal boxplot by two methods first, we need to switch x and y attributes and pass it to the boxplot( ) method, and the other is to use the orient=”h” option and pass it to the boxplot() method.

How do you get rid of outliers in Seaborn?

To remove outliers from a seaborn boxplot then set the fliersize parameter to 0. By detro - Last Updated Aug. 22, 2022, 10:11 a.m.

What does Seaborn boxplot show?

Draw a box plot to show distributions with respect to categories. A box plot (or box-and-whisker plot) shows the distribution of quantitative data in a way that facilitates comparisons between variables or across levels of a categorical variable.


1 Answers

You have two different hues, such that the first is plotted left of the position and the second is plotted right of the position. This is usually desired, see e.g. this plot from the documentation

enter image description here

import matplotlib.pyplot as plt
import seaborn as sns

tips = sns.load_dataset("tips")
ax = sns.boxplot(x="day", y="total_bill", hue="time",
                  data=tips, linewidth=2.5)

plt.show()

Here, you wouldn't want to have the blue and orange boxplots overlapping, would you?

But you can, if you want. Use the dodge=False argument for that purpose.

enter image description here

import matplotlib.pyplot as plt
import seaborn as sns

tips = sns.load_dataset("tips")
ax = sns.boxplot(x="day", y="total_bill", hue="time",
                  dodge=False, data=tips, linewidth=2.5)

plt.show()
like image 55
ImportanceOfBeingErnest Avatar answered Sep 22 '22 06:09

ImportanceOfBeingErnest