Skip to content

Possible bug in boxplot() #1027

Closed
Closed

Description

@jairideout

Hello,

I came across a strange boxplot that is generated with the following data:

from pylab import boxplot, savefig
bp = boxplot([2.88497, 1.2143, 0.923436, 1.18696])
show()

The boxplot has the non-outlier max below the upper quartile. I plotted the same data using R's boxplot function and it does not exhibit the same behavior. MPL's boxplot is plotting the max value as an outlier (the '+' sign) but then get confused about how to plot the quartiles and non-outlier max. This can be fixed by changing the whisker length to something more than 1.5 (the default) so that the outlier is seen as the max.

I'm not sure if this is a bug or not, but the boxplot doesn't look right to me using the default settings for boxplot(). I am using matplotlib 1.1.0 on Mac OS X 10.7.4.

Thanks,
Jai

Activity

  1. seberg commented on Jul 26, 2012

    @seberg
    Contributor

    Thought I would have a look... First thing, there is a difference between R and python/matplotlib regarding quartile calculations. Given the data [1,2,3,4] matplotlib will give 1.75 as the 25th Percentile while R will give 1.5.
    The real problem is that of the boxplots generally not being well defined here as there is no data point between the 75th percentile and the maximum whisker length. In fact R will do just the same as matplotlib and draw the whisker to the center point if this happens (ie, here just set range=0.5 for simplicity in R)...
    You could add a warning or such, but I think there is no bug as such here, its just the boxplot itself is not well defined for this input data.

  2. dmcdougall commented on Jul 26, 2012

    @dmcdougall
    Member

    Does Matlab have functionality to produce box plots? If so, what is the output?

  3. seberg commented on Jul 26, 2012

    @seberg
    Contributor

    Matlab uses the same method as R for the calculation of the precentiles (thinking about it, this definition means that for the usual whisker length the problem cannot arise). In the case of no well defined whiskers (ie. setting the whisker length to 0.5 from 1.5) it will simply not draw one at all, or maybe 0 length... (matplotlib and R draw a bogus one). But its not like matplotlib calculations are wrong I think, just a different definition for the percentiles.

  4. tacaswell commented on Nov 26, 2013

    @tacaswell
    Member

    @phobson as long as you are planning to work on box plot this week can you also take a look at this issue?

  5. phobson commented on Nov 26, 2013

    @phobson
    Member

    @tacaswell happily :)

  6. mdboom commented on Jan 27, 2014

    @mdboom
    Member

    @phobson: Is this addressed by #2643?

  7. phobson commented on Jan 27, 2014

    @phobson
    Member

    @mdboom
    Yes. Passing the OP's array into matplotlib.cbook.boxplot_stats (which boxplot now uses) returns the following:

    In [5]: mpl.cbook.boxplot_stats([2.88497, 1.2143, 0.923436, 1.18696])
    Out[5]:
    [{u'cihi': 1.6016774725,
      u'cilo': 0.79958252749999981,
      u'fliers': array([ 2.88497]),
      u'iqr': 0.51088850000000008,
      u'label': '0',
      u'mean': 1.5524165000000001,
      u'med': 1.2006299999999999,
      u'q1': 1.1210789999999999,
      u'q3': 1.6319675,
      u'whishi': 1.6319675,
      u'whislo': 0.92343600000000003}]

    The upper quartile is the same at the upper whisker (which is ok, IMO) and the max values is reported as an outlier above the upper quartile.

    I vote to close this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions