Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python-Matplotlib boxplot. How to show percentiles 0,10,25,50,75,90 and 100?

I would like to plot an EPSgram (see below) using Python and Matplotlib.

The boxplot function only plots quartiles (0, 25, 50, 75, 100). So, how can I add two more boxes?

EPSGram boxplot

like image 620
user1123132 Avatar asked Dec 30 '11 15:12

user1123132


People also ask

How do you adjust a boxplot in Python?

Steps. Set the figure size and adjust the padding between and around the subplots. Make a Pandas dataframe, i.e., two-dimensional, size-mutable, potentially heterogeneous tabular data. Make a box and whisker plot, using boxplot() method with width tuple to adjust the box in boxplot.

How do you draw a boxplot using MatPlotLib in Python?

Creating Box Plotpyplot module of matplotlib library provides boxplot() function with the help of which we can create box plots. The data values given to the ax. boxplot() method can be a Numpy array or Python list or Tuple of arrays. Let us create the box plot by using numpy.


1 Answers

I put together a sample, if you're still curious. It uses scipy.stats.scoreatpercentile, but you may be getting those numbers from elsewhere:

from random import random
import numpy as np
import matplotlib.pyplot as plt
from scipy.stats import scoreatpercentile

x = np.array([random() for x in xrange(100)])

# percentiles of interest
perc = [min(x), scoreatpercentile(x,10), scoreatpercentile(x,25),
               scoreatpercentile(x,50), scoreatpercentile(x,75),
               scoreatpercentile(x,90), max(x)]
midpoint = 0 # time-series time

fig = plt.figure()
ax = fig.add_subplot(111)
# min/max
ax.broken_barh([(midpoint-.01,.02)], (perc[0], perc[1]-perc[0]))
ax.broken_barh([(midpoint-.01,.02)], (perc[5], perc[6]-perc[5]))
# 10/90
ax.broken_barh([(midpoint-.1,.2)], (perc[1], perc[2]-perc[1]))
ax.broken_barh([(midpoint-.1,.2)], (perc[4], perc[5]-perc[4]))
# 25/75
ax.broken_barh([(midpoint-.4,.8)], (perc[2], perc[3]-perc[2]))
ax.broken_barh([(midpoint-.4,.8)], (perc[3], perc[4]-perc[3]))

ax.set_ylim(-0.5,1.5)
ax.set_xlim(-10,10)
ax.set_yticks([0,0.5,1])
ax.grid(True)
plt.show()

Output of the code above

like image 137
maxm Avatar answered Sep 23 '22 15:09

maxm