为了只用百分位数值和异常值(如果有的话)绘制箱形图,我做了一个
customized_box_plot
的函数,它基本上修改了基本箱形图中的属性(从一个很小的样本数据中生成),使其根据你的百分位数值来适应。
The
customized_box_plot
function
def customized_box_plot(percentiles, axes, redraw = True, *args, **kwargs):
Generates a customized boxplot based on the given percentile values
box_plot = axes.boxplot([[-9, -4, 2, 4, 9],]*n_box, *args, **kwargs)
# Creates len(percentiles) no of box plots
min_y, max_y = float('inf'), -float('inf')
for box_no, (q1_start,
q2_start,
q3_start,
q4_start,
q4_end,
fliers_xy) in enumerate(percentiles):
# Lower cap
box_plot['caps'][2*box_no].set_ydata([q1_start, q1_start])
# xdata is determined by the width of the box plot
# Lower whiskers
box_plot['whiskers'][2*box_no].set_ydata([q1_start, q2_start])
# Higher cap
box_plot['caps'][2*box_no + 1].set_ydata([q4_end, q4_end])
# Higher whiskers
box_plot['whiskers'][2*box_no + 1].set_ydata([q4_start, q4_end])
# Box
box_plot['boxes'][box_no].set_ydata([q2_start,
q2_start,
q4_start,
q4_start,
q2_start])
# Median
box_plot['medians'][box_no].set_ydata([q3_start, q3_start])
# Outliers
if fliers_xy is not None and len(fliers_xy[0]) != 0:
# If outliers exist
box_plot['fliers'][box_no].set(xdata = fliers_xy[0],
ydata = fliers_xy[1])
min_y = min(q1_start, min_y, fliers_xy[1].min())
max_y = max(q4_end, max_y, fliers_xy[1].max())
else:
min_y = min(q1_start, min_y)
max_y = max(q4_end, max_y)
# The y axis is rescaled to fit the new box plot completely with 10%
# of the maximum value at both ends
axes.set_ylim([min_y*1.1, max_y*1.1])
# If redraw is set to true, the canvas is updated.
if redraw:
ax.figure.canvas.draw()
return box_plot
使用逆向逻辑(代码在最后),我从这里提取了百分位数的值例子
>>> percentiles
(-1.0597368367634488, 0.3977683984966961, 1.0298955252405229, 1.6693981537742526, 3.4951447843464449)
(-0.90494930553559483, 0.36916539612108634, 1.0303658700697103, 1.6874542731392828, 3.4951447843464449)
(0.13744105279440233, 1.3300645202649739, 2.6131540656339483, 4.8763411136047647, 9.5751914834437937)
(0.22786243898199182, 1.4120860286080519, 2.637650402506837, 4.9067126578493259, 9.4660357513550899)
(0.0064696168078617741, 0.30586770128093388, 0.70774153557312702, 1.5241965711101928, 3.3092932063051976)
(0.007009744579241136, 0.28627373934008982, 0.66039691869500572, 1.4772725266672091, 3.221716765477217)
(-2.2621660374110544, 5.1901313713883352, 7.7178532139979357, 11.277744848353247, 20.155971739152388)
(-2.2621660374110544, 5.1884411864079532, 7.3357079047721054, 10.792299385806913, 18.842012119715388)
(2.5417888074435702, 5.885996170695587, 7.7271286220368598, 8.9207423361593179, 10.846938621419374)
(2.5971767318505856, 5.753551925927133, 7.6569980004033464, 8.8161056254143233, 10.846938621419374)
请注意,为了保持简短,我没有显示离群值向量,它将是每个百分位数的第6个元素。
还要注意的是,所有通常的附加kwargs/args都可以使用,因为它们只是被传递到boxplot
方法里面。
>>> fig, ax = plt.subplots()
>>> b = customized_box_plot(percentiles, ax, redraw=True, notch=0, sym='+', vert=1, whis=1.5)
>>> plt.show()
该boxplot
方法返回一个字典,将boxplot的组件映射到所创建的各个matplotlib.lines.Line2D
实例。
引用matplotlib.pyplot.boxplot
文档中的话。
这个字典有以下的键(假设是垂直的boxplots)。
boxes:boxplot的主体,显示四分位数和中位数的置信区间(如果启用)。
中位数:每个盒子的中位数的水平线。
晶须:延伸到最极端的n个离群数据点的垂直线。 帽:晶须两端的水平线。
异常值:代表超出晶须的数据的点(异常值)。
平均值:代表平均值的点或线。
For 例子 observe the boxplot
of a tiny sample data of [-9, -4, 2, 4, 9]
>>> b = ax.boxplot([[-9, -4, 2, 4, 9],])
{'boxes': [<matplotlib.lines.Line2D at 0x7fe1f5b21350>],
'caps': [<matplotlib.lines.Line2D at 0x7fe1f54d4e50>,
<matplotlib.lines.Line2D at 0x7fe1f54d0e50>],
'fliers': [<matplotlib.lines.Line2D at 0x7fe1f5b317d0>],
'means': [],
'medians': [<matplotlib.lines.Line2D at 0x7fe1f63549d0>],
'whiskers': [<matplotlib.lines.Line2D at 0x7fe1f5b22e10>,
<matplotlib.lines.Line2D at 0x7fe20c54a510>]}
>>> plt.show()
The matplotlib.lines.Line2D
对象有两个方法,我将在我的函数中广泛使用。set_xdata
( or set_ydata
)和get_xdata
( or get_ydata
).
使用这些方法,我们可以改变基础箱形图的组成线的位置,以符合你的百分位值(这就是customized_box_plot
函数的作用)。在改变了组成线的位置后,你可以用figure.canvas.draw()
重新绘制画布。
总结了从百分位数到各种Line2D
对象的坐标的映射关系。
The max ( q4_end
- end of 4th quartile ) corresponds to the top most cap Line2D
object.
The min ( q1_start
- start of the 1st quartile ) corresponds to the lowermost most cap Line2D
object.
The median corresponds to the ( q3_start
) median Line2D
object.
The 2 whiskers lie between the ends of the boxes and extreme caps ( q1_start
and q2_start
- lower whisker; q4_start
and q4_end
- upper whisker )
The box is actually an interesting n
shaped line bounded by a cap at the lower portion. The extremes of the n
shaped line correspond to the q2_start
and the q4_start
.
The Central x coordinates ( for multiple box plots are usually 1, 2, 3... )
The library automatically calculates the bounding x coordinates based on the width specified.
INVERSE FUNCTION TO RETRIEVE THE PERCENTILES FROM THE boxplot DICT:
def get_percentiles_from_box_plots(bp):
percentiles = []
for i in range(len(bp['boxes'])):
percentiles.append((bp['caps'][2*i].get_ydata()[0],
bp['boxes'][i].get_ydata()[0],
bp['medians'][i].get_ydata()[0],
bp['boxes'][i].get_ydata()[2],
bp['caps'][2*i + 1].get_ydata()[0],
(bp['fliers'][i].get_xdata(),
bp['fliers'][i].get_ydata())))
return percentiles
我之所以没有做一个完全自定义的boxplot方法,是因为,内置的盒式图提供的许多功能不能完全复制。
另外,如果我可能不必要地解释了一些可能太明显的东西,请原谅我。