如何在Matplotlib Boxplot中更改晶须帽的长度

时间:2018-06-22 22:39:50

标签: python matplotlib boxplot

我正在尝试更改箱形图晶须的盖帽(最小和最大点)的长度,在下图中将其标记为红色 enter image description here

是否可以在不更改盒子尺寸的情况下更改晶须的最小标记和最大标记的长度?

编辑:我的意思是增加指示晶须的最小和最大端的线标记的长度,而不是通过增加置信区间来增加整个晶须本身的长度。在最新更新的图片中,我表明我希望增加黑色的最小和最大标记,以使其与我用红线指示的大小匹配。

3 个答案:

答案 0 :(得分:2)

直接来自boxplot example

的一些虚假数据
# fake up some more data
spread = np.random.rand(50) * 100
center = np.ones(25) * 40
flier_high = np.random.rand(10) * 100 + 100
flier_low = np.random.rand(10) * -100
d2 = np.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
pyplot.boxplot returns a dictionary个实例的

Line2D中,caps是您要更改的内容。此解决方案将使它们的长度增加0.5个x轴单位,设置其颜色和线宽。

plt.figure()
returns = plt.boxplot(data, 0, '')

caps = returns['caps']
n = .25
n = .25
for cap, color in zip(caps, ['xkcd:azul','aquamarine','crimson','darkorchid','coral','thistle']):
    #print(cap.properties()['xdata'])
    #cap.set_xdata(cap.get_xdata() + (-n,+n))
    #cap.set_color(color)
    #cap.set_linewidth(4.0)
    cap.set(color=color, xdata=cap.get_xdata() + (-n,+n), linewidth=4.0)


Artist Tutorial

答案 1 :(得分:1)

fig, axes = plt.subplots(nrows = 1, ncols = 2, figsize=(10, 5))

normal_caps = axes[0].boxplot(s, labels = ['Normal Caps'],
                capprops = dict(linestyle='-', linewidth=2, color='Black'))

big_caps = axes[1].boxplot(s, labels = ['Longer Caps'],
                capprops = dict(linestyle='-', linewidth=2, color='Black'))

for cap in big_caps['caps']:
    cap.set_xdata(cap.get_xdata() + np.array([-.15,.15]))

答案 2 :(得分:0)

在创建箱形图时可以通过添加参数<table class="table"> <tbody id="tbody"></tbody> </table>来实现

matplotlib.axes.Axes.boxplot

whis