Pandas (Python)中箱图的数据限制和最大距离

2024-06-26 00:19:11 发布

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

我正在使用Python绘制数据(来自许多实验),我想使用boxplot库的pandas方法。你知道吗

执行df = pd.DataFrame(value,columns=['Col1'])结果如下:

enter image description here

问题来自于极值。在Matlab中,解决方案是使用“DataLimit”选项:

boxplot(bp1,'DataLim',[4.2,4.3])

来自Matlab文档:

Data Limits and Maximum Distances

'DataLim' — Extreme data limits [-Inf,Inf] (default) | two-element numeric vector

Extreme data limits, specified as the comma-separated pair consisting of 'DataLim' and a two-element numeric vector containing the lower and upper limits, respectively. The values specified for 'DataLim' are used by 'ExtremeMode' to determine which data points are extreme.

Python也有类似的功能吗?

绕车介绍: 但是,我有一个漫游(我真的不喜欢它,因为它改变了测量的统计分布):我只是手动排除“有问题的值”:

df = pd.DataFrame(value[100:],columns=['Col1'])
df.boxplot(column=['Col1'])

结果是:

enter image description here

这是因为我知道问题出在哪里。你知道吗


Tags: columnsanddataframedfdatavaluecol1inf
1条回答
网友
1楼 · 发布于 2024-06-26 00:19:11

您可以使用ylim约束轴,而不会从计算中忽略异常值:

data = np.concatenate((np.random.rand(50) * 100,  # spread
                       np.ones(25) * 50,  # center
                       np.random.rand(10) * 100 + 100,  # flier high
                       np.random.rand(10) * -100,  # flier low
                       np.random.rand(2) * 10_000))  # unwanted outlier
fig1, ax1 = plt.subplots()
ax1.boxplot(data)
plt.ylim([-100, 200])
plt.show()

相关问题 更多 >