覆盖箱线图中的中位数/方差的数值 [英] Overlaying the numeric value of median/variance in boxplots
问题描述
在Python中使用箱形图时,有什么方法可以自动/轻松地覆盖中位数&的值.每个框顶部的方差(或至少是中位数的数值)?
When using box plots in Python, is there any way to automatically/easily overlay the value of the median & variance on top of each box (or at least the numerical value of the median)?
例如在下面的方框图中,我想在每个方框图中覆盖文本(中位数,+-std).
E.g. in the boxplot below, I would like to overlay the text (median, +- std) on each box plot.
推荐答案
假设您正在使用boxplot函数绘制箱形图,它将返回一个包含图成分的字典.请注意,该框代表内部四分位数范围(25至75个百分点),而不是标准差.
Assuming you are using the boxplot function to draw the boxplots, it returns a dictionary that holds the components of the graph. Note that the box represent the inner quartile range (25 to 75th percentile) and not the standard deviation.
>>> bp_dict = boxplot(data, vert=False) # draw horizontal boxplot
>>> bp_dict.keys()
>>> bp_dict.keys()
['medians', 'fliers', 'whiskers', 'boxes', 'caps']
这些包含构成每个绘图元素的Line2D对象.您可以使用Line2D.get_xydata方法获取中位数和框位置(以数据坐标表示),以找出放置文本的位置.
These contain the Line2D objects that form each of the plot elements. You can use the Line2D.get_xydata method to get the median and box positions (in data coords) to figure out where to position your text.
from pylab import *
# from http://matplotlib.org/examples/pylab_examples/boxplot_demo.html
# fake up some data
spread= rand(50) * 100
center = ones(25) * 50
flier_high = rand(10) * 100 + 100
flier_low = rand(10) * -100
data =concatenate((spread, center, flier_high, flier_low), 0)
# fake up some more data
spread= rand(50) * 100
center = ones(25) * 40
flier_high = rand(10) * 100 + 100
flier_low = rand(10) * -100
d2 = concatenate( (spread, center, flier_high, flier_low), 0 )
data.shape = (-1, 1)
d2.shape = (-1, 1)
#data = concatenate( (data, d2), 1 )
# Making a 2-D array only works if all the columns are the
# same length. If they are not, then use a list instead.
# This is actually more efficient because boxplot converts
# a 2-D array into a list of vectors internally anyway.
data = [data, d2, d2[::2,0]]
# multiple box plots on one figure
figure()
# get dictionary returned from boxplot
bp_dict = boxplot(data, vert=False)
for line in bp_dict['medians']:
# get position data for median line
x, y = line.get_xydata()[1] # top of median line
# overlay median value
text(x, y, '%.1f' % x,
horizontalalignment='center') # draw above, centered
for line in bp_dict['boxes']:
x, y = line.get_xydata()[0] # bottom of left line
text(x,y, '%.1f' % x,
horizontalalignment='center', # centered
verticalalignment='top') # below
x, y = line.get_xydata()[3] # bottom of right line
text(x,y, '%.1f' % x,
horizontalalignment='center', # centered
verticalalignment='top') # below
show()
这篇关于覆盖箱线图中的中位数/方差的数值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!