Skip to content

Instantly share code, notes, and snippets.

@msharp
Last active August 29, 2015 14:23
Show Gist options
  • Save msharp/38ff2d1109c1b38d5e44 to your computer and use it in GitHub Desktop.
Save msharp/38ff2d1109c1b38d5e44 to your computer and use it in GitHub Desktop.
Box plots in 538 style
# Pylab BOXPLOTS in 538 style
import matplotlib.pyplot as plt
import itertools
def box_plot(boxes, figsize=(5,4), ymax=None, title=None, xlabels=[]):
def setBoxColors(bp):
colours = ["#30a2da","#6d904f","#8b8b8b","#fc4f30","#e5ae38"]
n_boxes = len(bp['boxes'])
fte = list(itertools.islice(itertools.cycle(colours), n_boxes))
lw = (2,3)
for ix in range(n_boxes):
setp(bp['boxes'][ix], color=fte[ix], linewidth=lw[1])
setp(bp['medians'][ix], color=fte[ix], linewidth=lw[1])
setp(bp['fliers'][ix], marker='None')
setp(bp['caps'][ix*2], color=fte[ix], linewidth=lw[1])
setp(bp['caps'][ix*2+1], color=fte[ix], linewidth=lw[1])
setp(bp['whiskers'][ix*2], color=fte[ix], linewidth=lw[0])
setp(bp['whiskers'][ix*2+1], color=fte[ix], linewidth=lw[0])
def positions(num_boxes):
return [x for x in range(-1, 99, 3)][1:num_boxes+1]
# setup
fig = figure(figsize=figsize)
ax = axes()
hold(True)
# multiple boxplots with different colours
bp = boxplot(boxes, positions=positions(len(boxes)), widths=2)
setBoxColors(bp)
# set axes limits and labels
xlim(0, 1+len(boxes)*3)
if ymax:
ylim(0, ymax)
if title:
ax.set_title(title)
if len(xlabels) > 0:
ax.set_xticklabels(xlabels)
show()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment