Matplotlib:如果使用关键字sym,Boxplot异常值颜色会发生变化

2024-04-26 18:35:32 发布

您现在位置:Python中文网/ 问答频道 /正文

仅适用于Matplotlib<;1.4.0!在

我有一个奇怪的效果,如果我改变用来画它们的符号,离群值的颜色会改变。(Documentation for Boxplot)在我看来像个臭虫。在

即使我想使用“+”以外的其他符号,如何将所有异常值的颜色“重置”为蓝色?在

official Example为模型的最小工作示例:

#!/usr/bin/python

from pylab import *

# 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)

# Left Figure
boxplot(data)

# Right Figure
figure()
boxplot(data, sym='.')

enter image description here


Tags: ltdatamatplotlib颜色符号lowfigurecenter
3条回答

查看boxplot sym的默认值:

*sym* : [ default 'b+' ]  # blue +

所以默认值是蓝色+如果只指定一个点,它可以有不同的颜色。可能会想到这种行为是需要的。可能被描述为不一致的默认值,但不是bug。在

因此,如果您想要两个颜色相同的绘图,这不是一个错误:

^{pr2}$

在没有指定颜色的情况下,它似乎在旋转(在某些情况下需要的行为)。如果您想坚持固定颜色,请使用sym='b+'和sym='b.'

这确实很奇怪。可以通过指定符号上的颜色来解决此问题。使用boxplot(data, sym='b.')(b代表蓝色)。然而,在我看来这是一个bug,特别是因为奇怪的颜色只出现在较低的异常值上,而不是在较高的异常值上。在

正如BrenBarn和Joop正确指出的那样,最简单的方法是用字符指定颜色:boxplot(data, sym='b.')将颜色重置为蓝色,或者{}表示绘图中的绿色x。在

this Question的帮助下,is能够找到一种使离群值完全可定制的方法。(我想减小离群值的大小)

# insert this after lines '#Right figure', 'figure()'

r = boxplot(data, sym="w") # outlier are computed but not drawn

top_points = r["fliers"][0].get_data()[1]
bottom_points = r["fliers"][2].get_data()[1]
plot(np.ones(len(top_points)), top_points, "x", color="blue", markersize=1)
plot(np.ones(len(bottom_points)), bottom_points, ".", color="blue")
#if you have several boxplots this "np.ones(len(bottom_points))" is the number of plot you want to draw in, so 1s for the first [2,2,2,2,...] for the second ect.

enter image description here

相关问题 更多 >