2011-12-30 124 views
7

我想繪製一個EPSgram(見下文)使用Python和Matplotlib。Python-Matplotlib boxplot。如何顯示百分位數0,10,25,50,75,90和100?

boxplot函數只繪製四分位數(0,25,50,75,100)。那麼,我怎樣才能添加兩個盒子呢?

EPSGram boxplot

+2

我不認爲有一個簡單的方法來做到這一點,但使用一些'broken_barh'你也許可以做自己。 – aganders3 2011-12-30 16:55:40

+5

作爲一種富有表現力的選擇,請考慮[小提琴情節](http://pyinsci.blogspot.com/2009/09/violin-plot-with-matplotlib.html)。 – 2011-12-30 18:45:06

回答

2

我組建了一個樣本,如果你還是好奇。它採用scipy.stats.scoreatpercentile,但你可能會從其他地方得到這些數字:

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