Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Matplotlib box plot fliers not showing

I was wondering if anyone had an issue with Matplotlib's box plot fliers not showing?

I literally copy-pasted this example here into a python script: http://blog.bharatbhole.com/creating-boxplots-with-matplotlib/

...but the box plot fliers (outliers) are not showing. Does anyone know why I might not be seeing them? Sorry if this is a silly question, but I cannot for the life of me figure out why it doesn't work.

## Create data
np.random.seed(10)
collectn_1 = np.random.normal(100, 10, 200)
collectn_2 = np.random.normal(80, 30, 200)
collectn_3 = np.random.normal(90, 20, 200)
collectn_4 = np.random.normal(70, 25, 200)

## combine these different collections into a list    
data_to_plot = [collectn_1, collectn_2, collectn_3, collectn_4]

# Create a figure instance
fig = plt.figure(1, figsize=(9, 6))

# Create an axes instance
ax = fig.add_subplot(111)

# Create the boxplot
bp = ax.boxplot(data_to_plot)

I also tried adding showfliers=True to the last line of that script, but it's still not working.

This is what I get as an output:

enter image description here

like image 214
maths_student15 Avatar asked Mar 06 '15 21:03

maths_student15


People also ask

What are fliers in boxplot?

fliers : points representing data that extend beyond the whiskers (fliers). means : points or lines representing the means.

What does not represent in a box plot?

The box of the plot is a rectangle which encloses the middle half of the sample, with an end at each quartile. The length of the box is thus the interquartile range of the sample. The other dimension of the box does not represent anything in particular. A line is drawn across the box at the sample median.

What is whis in boxplot?

boxplot(X,notch,' sym ',vert,whis) enables you to specify the length of the "whiskers." whis defines the maximum length of the whiskers as a function of the inter-quartile range (default = 1.5). Each whisker extends to the most extreme data value within whis * IQR of the box.


2 Answers

From the look of your plot, it seems you have imported the seaborn module. There is an issue with matplotlib boxplot fliers not showing up when seaborn is imported, even when fliers are explicitly enabled. Your code seem to be working fine when seaborn is not imported:

Boxplot with fliers, no seaborn

When seaborn is imported, you could do the following:

Solution 1:

Assuming you have imported seaborn like this:

import seaborn as sns

you can use the seaborn boxplot function:

sns.boxplot(data_to_plot, ax=ax)

resulting in:

Seaborn boxplot with fliers

Solution 2:

In case you want to keep using the matplotlib boxplot function (from Automatic (whisker-sensitive) ylim in boxplots):

ax.boxplot(data_to_plot, sym='k.')

resulting in:

Matplotlib boxplot with fliers

like image 74
Puggie Avatar answered Oct 18 '22 19:10

Puggie


You might not see fliers if the flier marker was set to None. The page you linked to has a for flier in bp['fliers']: loop, which sets the flier marker style, color and alpha:

import numpy as np
import matplotlib.pyplot as plt

np.random.seed(10)
collectn_1 = np.random.normal(100, 10, 200)
collectn_2 = np.random.normal(80, 30, 200)
collectn_3 = np.random.normal(90, 20, 200)
collectn_4 = np.random.normal(70, 25, 200)

## combine these different collections into a list    
data_to_plot = [collectn_1, collectn_2, collectn_3, collectn_4]

# Create a figure instance
fig = plt.figure(1, figsize=(9, 6))

# Create an axes instance
ax = fig.add_subplot(111)

# Create the boxplot
bp = ax.boxplot(data_to_plot, showfliers=True)

for flier in bp['fliers']:
    flier.set(marker='o', color='#e7298a', alpha=0.5)

plt.show()

yields

enter image description here

like image 31
unutbu Avatar answered Oct 18 '22 20:10

unutbu