Ren Lyke
Ren Lyke

Reputation: 283

Box Whisker Plot using seaborn does not show the box and whiskers

I am trying to build a box and whisker plots with seaborn. With my min value at '-200,000' and max value at '1,400,000'. Both of these are the outliers. However i only get the graph somewhat similar to a scatter plot.

Below is my code

import pandas as pd
import numpy as np
import xlrd

import matplotlib.pyplot as plt
import seaborn as sns

pi_analysis = pd.read_excel(r'C:\PI\PI Analysis.xlsx',
                           sheet_name = 'Raw Data'
                           , header = 0
                           )
print(pi_analysis)
group_segement= pi_analysis[['Segment', 'TOTAL AMOUNT']].groupby('Segment').sum()

print(group_segement)
group_segement_mean= pi_analysis[['Segment', 'TOTAL AMOUNT']].groupby('Segment')
group_segement_mean.mean().head()
group_segement_mean.describe()

sns.boxplot(x="Segment", y="TOTAL AMOUNT",data=pi_analysis)

enter image description here

Attached is the image. Have tried to change the access. It did not work. Any suggestions how to display the box and whiskers.

New image after changing the scale.

enter image description here

This is code section. However it still does not give me the complete view.

ax=sns.boxplot(x='Segment',y='TOTAL AMOUNT',data=pi_analysis)
ax.set_ylim(-10*10^8,10*10^8)

Regards, Ren.

Upvotes: 4

Views: 9315

Answers (2)

flashliquid
flashliquid

Reputation: 578

The compressed boxes you are seeing are a result of the extreme outliers being accommodated by the scaling. It's very easy to drop the outliers

Seaborn boxplots will take the matplotlib argument:

showfliers=False

This will result in plots of only the box and whiskers, with the outliers not shown.

The last line of your code would then be:

sns.boxplot(x="Segment", y="TOTAL AMOUNT",data=pi_analysis, showfliers=False) 

Upvotes: 3

Diziet Asahi
Diziet Asahi

Reputation: 40707

As @g-anderson was alluding in his comment, the boxplot is there, it's just too small for you to see. Consider the following code:

d = np.random.random(size=(100,))
d[0] = 100
d[-1] = -100

fig, ax = plt.subplots()
sns.boxplot(data=d, orient='vertical')

enter image description here

If you want to see the boxplot, you could simply rescale the y-axis to a more relevant range:

fig, ax = plt.subplots()
sns.boxplot(data=d, orient='vertical')
ax.set_ylim(-1,2)

enter image description here

Upvotes: 0

Related Questions