dee cue
dee cue

Reputation: 1053

Manually Drawing Box Plot Using Matplotlib with Outliers

By referring to this Is it possible to draw a matplotlib boxplot given the percentile values instead of the original inputs?, I would like to draw a single box-plot given the five-number-summary and the outliers.

The answer given from the question is for multiple box-plots, and I have modified the code to suit for a single box-plot.

This is the code that I tried:

def custom_boxplot(mini, q1, q2, q3, maxm, *outliers):
    """
    Input: 
        Five-number summary separated into different arguments;
        The following arguments after the summary are the outliers.
    Output:
        A boxplot drawn in the console.
    """
    figure = plt.figure(figsize=(8,8))
    ax = plt.gca()
    bp = plt.boxplot([mini, q1, q2, q3, maxm])
    fliers = bp['fliers']
    for v in outliers:
        fliers[0].set(xdata = 1, ydata = v)
    _all = [mini, q1, q2, q3, maxm] + list(outliers)
    _min, _max = min(_all), max(_all)
    ax.set_ylim([_min*0.9, _max*1.1])

    figure.canvas.draw()

However, when I tried to run with the following line

custom_boxplot(43.2, 43.5, 51.05, 56.8, 69.3, 13.8, 21.2)

It outputted the box-plot with only one outlier from the last argument. I expected two data points of outlier are drawn in the box-plot for 13.8 and 21.2.

I believe the error is somewhere around here:

...
    for v in outliers:
        fliers[0].set(xdata = 1, ydata = v)
...

I understand that because I only have one boxplot, I can do the subscripting like fliers[0] to get the first box from the boxplot. xdata = 1 because again I am setting this for the first box, and then ydata=v for setting the y-value of the outlier. Where is the mistake in my code?

Upvotes: 1

Views: 1296

Answers (1)

Scott Boston
Scott Boston

Reputation: 153520

Let's try this:

def custom_boxplot(mini, q1, q2, q3, maxm, *outliers):
    """
    Input: 
        Five-number summary separated into different arguments;
        The following arguments after the summary are the outliers.
    Output:
        A boxplot drawn in the console.
    """
    figure = plt.figure(figsize=(8,8))
    ax = plt.gca()
    bp = plt.boxplot([mini, q1, q2, q3, maxm])
    fliers = bp['fliers']
    fliers[0].set(xdata = [1]*len(outliers), ydata = outliers)
    _all = [mini, q1, q2, q3, maxm] + list(outliers)
    _min, _max = min(_all), max(_all)
    ax.set_ylim([_min*0.9, _max*1.1])

Output:

enter image description here

Upvotes: 2

Related Questions