Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to make a grouped boxplot graph in matplotlib

I have three algorithms, A, B, and C. I've run them on different datasets and would like to graph their runtimes on each as a grouped boxplot in Python.

As a visual example of what I want, I made a terrible drawing, but hopefully it gets the point across.

boxplot graph drawing

If my data in python looks like this:

import numpy as np
import random

data = {}
data['dataset1'] = {}
data['dataset2'] = {}
data['dataset3'] = {}

n = 5
for k,v in data.iteritems():
    upper = random.randint(0, 1000)
    v['A'] = np.random.uniform(0, upper, size=n)
    v['B'] = np.random.uniform(0, upper, size=n)
    v['C'] = np.random.uniform(0, upper, size=n)

How can I make my plot look like the picture I drew?

like image 256
lollercoaster Avatar asked Dec 04 '13 01:12

lollercoaster


1 Answers

It's easiest to do this with independent subplots:

import matplotlib.pyplot as plt
import numpy as np
import random

data = {}
data['dataset1'] = {}
data['dataset2'] = {}
data['dataset3'] = {}

n = 500
for k,v in data.iteritems():
    upper = random.randint(0, 1000)
    v['A'] = np.random.uniform(0, upper, size=n)
    v['B'] = np.random.uniform(0, upper, size=n)
    v['C'] = np.random.uniform(0, upper, size=n)

fig, axes = plt.subplots(ncols=3, sharey=True)
fig.subplots_adjust(wspace=0)

for ax, name in zip(axes, ['dataset1', 'dataset2', 'dataset3']):
    ax.boxplot([data[name][item] for item in ['A', 'B', 'C']])
    ax.set(xticklabels=['A', 'B', 'C'], xlabel=name)
    ax.margins(0.05) # Optional

plt.show()

enter image description here

like image 91
Joe Kington Avatar answered Sep 30 '22 02:09

Joe Kington