Matplotlib boxplot 仅显示最大和最小传单
Matplotlib boxplot show only max and min fliers
我正在使用 plt.boxplot() 命令制作标准的 Matplotlib 箱线图。
我创建箱线图的代码行是:
bp = plt.boxplot(data, whis=[5, 95], showfliers=True)
因为我的数据有很大的分布,所以我得到了很多胡须范围之外的传单。为了获得更清晰的出版物质量图,我想最多只显示单个传单。并在分钟。数据的值,而不是所有传单。这可能吗?我在文档中没有看到任何内置选项来执行此操作。
(我可以将胡须的范围设置为 max/min,但这不是我想要的。我希望将胡须保持在第 5 个和第 95 个百分位数)。
下面是我正在处理的图。注意传单的密度。
plt.boxplot()
returns 字典,其中键 fliers
包含作为 line2d 对象的上下传单。您可以在绘制之前操纵它们:
仅限 matplotlib >= 1.4.0
bp = plt.boxplot(data, whis=[5, 95], showfliers=True)
# Get a list of Line2D objects, representing a single line from the
# minimum to the maximum flier points.
fliers = bp['fliers']
# Iterate over it!
for fly in fliers:
fdata = fly.get_data()
fly.set_data([fdata[0][0],fdata[0][-1]],[fdata[1][0],fdata[1][-1]])
旧版本
如果您使用的是旧版本的 matplotlib,则每个箱线图的传单由 两 行表示,而不是一行。因此,循环看起来像这样:
import numpy as np
for i in range(len(fliers)):
fdata = fliers[i].get_data()
# Get the index of the maximum y in data if
# i is 0 or even, else get index of minimum y.
if i%2 == 0:
id = np.where(fdata[1] == fdata[1].max())[0][0]
else:
id = np.where(fdata[1] == fdata[1].min())[0][0]
fliers[i].set_data([fdata[0][id], fdata[1][id]])
另请注意,matplotlib <1.4x 中不存在 showfliers
参数,并且 whisk
参数不接受列表。
当然(对于简单的应用程序)您可以绘制没有传单的箱线图并将最大点和最小点添加到图中:
bp = plt.boxplot(data, whis=[5, 95], showfliers=False)
sc = plt.scatter([1, 1], [data.min(), data.max()])
其中 [1, 1]
是点的 x 位置。
fliers = bp['fliers']
for i in range(len(fliers)): # iterate through the Line2D objects for the fliers for each boxplot
box = fliers[i] # this accesses the x and y vectors for the fliers for each box
box.set_data([[box.get_xdata()[0],box.get_xdata()[0]],[np.min(box.get_ydata()),np.max(box.get_ydata())]])
# note that you can use any two values from the xdata vector
结果图,仅显示最大和最小传单:
我正在使用 plt.boxplot() 命令制作标准的 Matplotlib 箱线图。 我创建箱线图的代码行是:
bp = plt.boxplot(data, whis=[5, 95], showfliers=True)
因为我的数据有很大的分布,所以我得到了很多胡须范围之外的传单。为了获得更清晰的出版物质量图,我想最多只显示单个传单。并在分钟。数据的值,而不是所有传单。这可能吗?我在文档中没有看到任何内置选项来执行此操作。
(我可以将胡须的范围设置为 max/min,但这不是我想要的。我希望将胡须保持在第 5 个和第 95 个百分位数)。
下面是我正在处理的图。注意传单的密度。
plt.boxplot()
returns 字典,其中键 fliers
包含作为 line2d 对象的上下传单。您可以在绘制之前操纵它们:
仅限 matplotlib >= 1.4.0
bp = plt.boxplot(data, whis=[5, 95], showfliers=True)
# Get a list of Line2D objects, representing a single line from the
# minimum to the maximum flier points.
fliers = bp['fliers']
# Iterate over it!
for fly in fliers:
fdata = fly.get_data()
fly.set_data([fdata[0][0],fdata[0][-1]],[fdata[1][0],fdata[1][-1]])
旧版本
如果您使用的是旧版本的 matplotlib,则每个箱线图的传单由 两 行表示,而不是一行。因此,循环看起来像这样:
import numpy as np
for i in range(len(fliers)):
fdata = fliers[i].get_data()
# Get the index of the maximum y in data if
# i is 0 or even, else get index of minimum y.
if i%2 == 0:
id = np.where(fdata[1] == fdata[1].max())[0][0]
else:
id = np.where(fdata[1] == fdata[1].min())[0][0]
fliers[i].set_data([fdata[0][id], fdata[1][id]])
另请注意,matplotlib <1.4x 中不存在 showfliers
参数,并且 whisk
参数不接受列表。
当然(对于简单的应用程序)您可以绘制没有传单的箱线图并将最大点和最小点添加到图中:
bp = plt.boxplot(data, whis=[5, 95], showfliers=False)
sc = plt.scatter([1, 1], [data.min(), data.max()])
其中 [1, 1]
是点的 x 位置。
fliers = bp['fliers']
for i in range(len(fliers)): # iterate through the Line2D objects for the fliers for each boxplot
box = fliers[i] # this accesses the x and y vectors for the fliers for each box
box.set_data([[box.get_xdata()[0],box.get_xdata()[0]],[np.min(box.get_ydata()),np.max(box.get_ydata())]])
# note that you can use any two values from the xdata vector
结果图,仅显示最大和最小传单: