2

I am trying to create a boxplot with jitter using the boxplot and stripplot in seaborn. Unfortunately my data has some outliers so I decide to exclude them in the final plot.

For the boxplot, it's easy to use showfliers=False argument to ignore the outliers. However stripplot does not have the similar argument. Since my dataset has outliers with extreme values, the y-axis is over-stretched, making it difficult to see the boxes.

Sample code:

import seaborn as sns
tips = sns.load_dataset("tips")
fig, ax = plt.subplots()
ax = sns.boxplot(x="day", y="total_bill", data=tips, showfliers=False)
ax = sns.stripplot(x="day", y="total_bill", data=tips)
fig.show()

enter image description here

Would it be easier just to filter out the outliers in the original dataframe before plotting?

Xiaoyu Lu
  • 3,280
  • 1
  • 22
  • 34
  • Yes, that is one way but outliers can be useful to analyze in some cases. What you can actually do is change the orientation of the plot and get a wider plot instead of taller – enterML Dec 05 '18 at 16:25

1 Answers1

3

This should solve the problem: You can get the limits for y_axis after creating the boxplot and then use that to set the ylim of the figure.

import seaborn as sns
tips = sns.load_dataset("tips")
fig, ax = plt.subplots()
ax = sns.boxplot(x="day", y="total_bill", data=tips, showfliers=False)
ylims=ax.get_ylim()
ax = sns.stripplot(x="day", y="total_bill", data=tips)
ax.set(ylim=ylims)
fig.show()