如何在matplotlib boxplot中更改晶须的长度

问题描述 投票:3回答:3

我正在尝试更改下图中标记为红色的boxplot whisker的帽子长度(最小和最大点)enter image description here

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

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

python matplotlib boxplot
3个回答
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 Line2Dcaps是你想要改变的。该解决方案将使它们的长度增加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
投票
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]))

0
投票

可以通过在创建箱形图时添加参数whis来实现

matplotlib.axes.Axes.boxplot

whis : float, sequence, or string (default = 1.5)
As a float, determines the reach of the whiskers to the beyond the first and  
third quartiles. In other words, where IQR is the interquartile range (Q3-Q1), 
the upper whisker will extend to last datum less than Q3 + whis*IQR). 
Similarly, the lower whisker will extend to the first datum greater than Q1 - 
whis*IQR. Beyond the whiskers, data are considered outliers and are plotted as 
individual points. Set this to an unreasonably high value to force the whiskers 
to show the min and max values. Alternatively, set this to an ascending 
sequence of percentile (e.g., [5, 95]) to set the whiskers at specific 
percentiles of the data. Finally, whis can be the string 'range' to force the
whiskers to the min and max of the data.
© www.soinside.com 2019 - 2024. All rights reserved.