How to create a boxplot from scratch using Q1, Q2, Q3, min/max information

29 Views Asked by At

I have a distribution but it is differentially private - which means I can't access individual points themselves or pass the distribution itself to plt.boxplot or sns.boxplot.

The only thing I have is calculating the critical info about the distribution:

  1. Q1
  2. Q2
  3. Q3
  4. Minimum
  5. Maximum
  6. Mean

I want to display a boxplot using only those. I am guessing this might involve drawing the actual rectangles, the lines for mean and median and the whiskers manually. Or not?

I would love some suggestions.

Ps. I don't care about outliers. The whisker lengths can be set regularly with combination of Q1/Q3 +/- 1.5 * IQR. But I need to show the minimum and max with single dots.

1

There are 1 best solutions below

1
letdatado On

see this code. It should work for you.

import matplotlib.pyplot as plt

def draw_boxplot(Q1, Q2, Q3, minimum, maximum, mean):
    fig, ax = plt.subplots()

    # Draw the box
    box_width = 0.5
    ax.bar(0, Q3 - Q2, width=box_width, bottom=Q2, color='white', edgecolor='black', zorder=3)
    ax.bar(0, Q2 - Q1, width=box_width, bottom=Q1, color='white', edgecolor='black', zorder=3)

    # Draw the median line
    ax.plot([-box_width / 2, box_width / 2], [Q2, Q2], color='black', linewidth=2, zorder=5)

    # Draw the mean line
    ax.plot([-box_width / 2, box_width / 2], [mean, mean], color='red', linestyle='dashed', linewidth=2, zorder=4)

    # Draw the whiskers
    ax.plot([0, 0], [Q1, minimum], color='black', linewidth=2, zorder=2)
    ax.plot([0, 0], [Q3, maximum], color='black', linewidth=2, zorder=2)

    # Draw the minimum and maximum points
    ax.scatter([0], [minimum], color='black', zorder=6)
    ax.scatter([0], [maximum], color='black', zorder=6)

    # Hide x-axis
    ax.xaxis.set_visible(False)

    plt.show()

# Example data
Q1 = 10
Q2 = 20
Q3 = 30
minimum = 5
maximum = 35
mean = 22

draw_boxplot(Q1, Q2, Q3, minimum, maximum, mean)