如何在散点图中将离群值更改为其他颜色 [英] How to change outliers to some other colors in a scatter plot
问题描述
如果我有这样的散点图
If I have a scatter plot like this
我想知道是否有办法将明显的离群值(如顶部的三个)更改为同一图中的其他一些颜色?
I was wondering is there any way to change the obvious outliers, like the three on the top, to some other colors in the same plot?
推荐答案
首先,您需要找到异常值"的条件.一旦有了这些,就可以掩盖绘图中那些不需要的点.
根据条件选择数组的子集可以很容易地以numpy的方式完成,例如如果a
是一个numpy数组,则a[a <= 1]
将返回所有值都大于1的数组"cut out".
First, you need to find a criterion for "outliers". Once you have that, you could mask those unwanted points in your plot.
Selecting a subset of an array based on a condition can be easily done in numpy, e.g. if a
is a numpy array, a[a <= 1]
will return the array with all values bigger than 1 "cut out".
然后可以按照以下步骤进行绘制
Plotting could then be done as follows
import numpy as np
import matplotlib.pyplot as plt
num= 1000
x= np.linspace(0,100, num=num)
y= np.random.normal(size=num)
fig=plt.figure()
ax=fig.add_subplot(111)
# plot points inside distribution's width
ax.scatter(x[np.abs(y)<1], y[np.abs(y)<1], marker="s", color="#2e91be")
# plot points outside distribution's width
ax.scatter(x[np.abs(y)>=1], y[np.abs(y)>=1], marker="d", color="#d46f9f")
plt.show()
生产
在这里,我们根据正态分布绘制点,对分布宽度以外的所有点进行不同的着色.
Here, we plot points from a normal distribution, colorizing all points outside the distribution's width differently.
这篇关于如何在散点图中将离群值更改为其他颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!