How to plot a box plot with two summary statistics using Matplotlib?

Question:

I have summary statistics for my data:

Summary 1: min = 0, 1st quarter = 5, median = 200, mean = 455, 3rd
quarter = 674, max = 980

Summary 2: min = 1, 1st quarter = 7.5, median = 254, mean = 586, 3rd
quarter = 851, max = 1021

I want to plot box plot using matplotlib from these statistics by plotting Summary 1 and 2 side by side.

I can plot the graph (box plot) for each of the summary separately (two graph) but couldn’t do it in single plot.

I am using below code for separate box plot:

import matplotlib.pyplot as plt

stats = [{
    "label": 'Summary 1',  # not required
    "mean":  455,  # not required
    "med": 200,
    "q1": 5,
    "q3": 674,
    "whislo": 0,  # required (min)
    "whishi": 980,  # required (max)
    "fliers": []  # required if showfliers=True
    }]


fs = 10  # fontsize

fig, axes = plt.subplots(nrows=1, ncols=1, figsize=(6, 6), sharey=True)
axes.bxp(stats)
axes.set_title('Boxplot for Summary 1', fontsize=fs)
plt.show()  

Can anyone tell me how can I do it?

Asked By: Piyush Ghasiya

||

Answers:

Looking at the value of stats in the source code of the example on the matplotlib docs, you need to put them both into the same list.

import matplotlib.pyplot as plt

stats = [{
    "label": 'Summary 1',  # not required
    "mean":  455,  # not required
    "med": 200,
    "q1": 5,
    "q3": 674,
    "whislo": 0,  # required (min)
    "whishi": 980,  # required (max)
    "fliers": []  # required if showfliers=True
    },
         {
    "label": 'Summary 2',  # not required
    "mean":586,  # not required
    "med": 254,
    "q1": 7.5,
    "q3": 851,
    "whislo": 1,  # required (min)
    "whishi": 1021,  # required (max)
    "fliers": []  # required if showfliers=True
    }]


fs = 10  # fontsize

fig, axes = plt.subplots(nrows=1, ncols=1, figsize=(6, 6), sharey=True)
axes.bxp(stats)
axes.bxp(stats)
axes.set_title('Boxplot for Summary 1', fontsize=fs)
plt.show()          

boxplots

Answered By: ramzeek
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.