我正在绘制来自各种测试的一些数据。有时在测试中,我碰巧有一个异常值(例如0.1),而所有其他值都小三个数量级。
使用matplotlib,我针对[0, max_data_value]
范围进行绘图
如何仅放大数据而不显示异常值,这会弄乱绘图中的x轴?
我是否应该简单地采用95%并在x轴上设置[0, 95_percentile]
范围?
参考方案
没有针对异常值的“最佳”测试。理想情况下,您应该合并先验信息(例如,“由于blah ...,此参数不应超过x ...”)。
对于离群值的大多数测试都使用中位数绝对偏差,而不是第95个百分位数或其他基于方差的度量。否则,计算出来的方差/ stddev将被异常值严重偏斜。
这是一个实现更常见异常值测试的函数。
def is_outlier(points, thresh=3.5):
"""
Returns a boolean array with True if points are outliers and False
otherwise.
Parameters:
-----------
points : An numobservations by numdimensions array of observations
thresh : The modified z-score to use as a threshold. Observations with
a modified z-score (based on the median absolute deviation) greater
than this value will be classified as outliers.
Returns:
--------
mask : A numobservations-length boolean array.
References:
----------
Boris Iglewicz and David Hoaglin (1993), "Volume 16: How to Detect and
Handle Outliers", The ASQC Basic References in Quality Control:
Statistical Techniques, Edward F. Mykytka, Ph.D., Editor.
"""
if len(points.shape) == 1:
points = points[:,None]
median = np.median(points, axis=0)
diff = np.sum((points - median)**2, axis=-1)
diff = np.sqrt(diff)
med_abs_deviation = np.median(diff)
modified_z_score = 0.6745 * diff / med_abs_deviation
return modified_z_score > thresh
作为使用它的示例,您将执行以下操作:
import numpy as np
import matplotlib.pyplot as plt
# The function above... In my case it's in a local utilities module
from sci_utilities import is_outlier
# Generate some data
x = np.random.random(100)
# Append a few "bad" points
x = np.r_[x, -3, -10, 100]
# Keep only the "good" points
# "~" operates as a logical not operator on boolean numpy arrays
filtered = x[~is_outlier(x)]
# Plot the results
fig, (ax1, ax2) = plt.subplots(nrows=2)
ax1.hist(x)
ax1.set_title('Original')
ax2.hist(filtered)
ax2.set_title('Without Outliers')
plt.show()
Matplotlib-固定x轴缩放比例和自动缩放y轴 - python我只想绘制部分数组,固定x部分,但让y部分自动缩放。我尝试如下所示,但是它不起作用。有什么建议么?import numpy as np import matplotlib.pyplot as plt data=[np.arange(0,101,1),300-0.1*np.arange(0,101,1)] plt.figure() plt.scatter(da…
Matplotlib-更改自动轴范围 - python我将自动轴范围用于数据。例如,当我使用-29到+31之间的x数据时,ax = plt.gca() xsta, xend = ax.get_xlim() 我得到-30和40,这不能正确描述数据范围。我希望看到轴范围四舍五入为5,即极限为-30和35。有可能这样做吗?或者,是否有可能获取x轴数据的精确范围(-29,31),然后编写算法手动更改该值(使用set_x…
Matplotlib:保存图形时白色边距和隐藏轴 - python我一直在尝试保存用matplotlib制作的图,但遇到了一些问题:不仅遇到了常见的白色边距问题(我在网上找到了一些解决方案),而且看来我的坐标轴和标签都在当我保存图像时,它们消失了,尽管当我要求Python show()结果时它们看起来很好。这是MWE,是我用show()得到的结果的打印屏幕(这是我想要的结果),以及将图形保存到.png时得到的结果(我相信白…
如何在matplotlib中将轴标签对齐到右侧或顶部 - python默认情况下,matplotlib将轴标签绘制在轴的中心。我想以水平轴和垂直轴都与轴的末端对齐的方式移动标签。例如,对于水平轴,我希望看到:+--------------------+ | | | | | | | | | | +--------------------+ label 是否可以通过matplotlib的全局设置来执行此操作? python大神给出…
更改散点图中不同虚拟值的点的颜色 - python在我的数据集中,我有一个Price列用于房价,还有5个虚拟列用于城市中的不同位置。我要做的是用不同的颜色在散点图上显示数据点。例如,在一个包含所有房屋价格的散点图上,我想要:当dummy1表示房屋位于Area1等于1时,所有价格点均为红色。当dummy2表示房屋位于Area2等于2时,所有价格点均为蓝色。依此类推,直到最后一列。如何创建该图?我可以使用plt…