Skip to content

Instantly share code, notes, and snippets.

@mattwigway
Last active January 4, 2018 23:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mattwigway/fc5362a243282eba1b4e1292e58c8c51 to your computer and use it in GitHub Desktop.
Save mattwigway/fc5362a243282eba1b4e1292e58c8c51 to your computer and use it in GitHub Desktop.
Matplotlib boxplot with custom summary stats
# To the extent possible under law, Matthew Wigginton Conway has waived all copyright and
# related or neighboring rights to this work. This work is published from: United States.
def customBoxPlot (whiskerLo, boxLo, med, boxHi, whiskerHi, **kwargs):
fakeData = [np.array([42, 42]) for i in whiskerLo]
bp = plt.boxplot(fakeData, **kwargs)
for i in range(len(whiskerLo)):
# https://stackoverflow.com/questions/27214537
# This can't possibly be the best way to do this...
# lower whisker
bp['whiskers'][2 * i].set_ydata([whiskerLo[i], boxLo[i]])
# upper whisker
bp['whiskers'][2 * i + 1].set_ydata([boxHi[i], whiskerHi[i]])
bp['medians'][i].set_ydata(med[i])
bp['boxes'][i].set_ydata([
boxLo[i],
boxLo[i],
boxHi[i],
boxHi[i],
boxLo[i]
])
bp['caps'][2 * i].set_ydata([whiskerLo[i], whiskerLo[i]])
bp['caps'][2 * i + 1].set_ydata([whiskerHi[i], whiskerHi[i]])
hgt = max(whiskerHi) - min(whiskerLo)
plt.ylim(min(whiskerLo) - 0.1 * hgt, max(whiskerHi) + 0.1 * hgt)
return bp
customBoxPlot([2, .9], [4, 5.2], [6, 5.8], [8, 6.6], [10, 6.9])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment