简体   繁体   English

如何在散点图中将异常值更改为其他一些颜色

[英]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?我想知道有什么方法可以将明显的异常值(例如顶部的三个)更改为同一图中的其他颜色?

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, eg if a is a numpy array, a[a <= 1] will return the array with all values bigger than 1 "cut out".在 numpy 中可以轻松地根据条件选择数组的子集,例如,如果a是 numpy 数组,则a[a <= 1]将返回所有值大于 1 的数组“切出”。

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

producing生产

在此处输入图片说明

Here, we plot points from a normal distribution, colorizing all points outside the distribution's width differently.在这里,我们从正态分布绘制点,对分布宽度之外的所有点进行不同的着色。

ImportanceOfBeingErnest has a great answer. ImportanceOfBeingErnest有一个很好的答案。 Here's a one-liner I use if I have an array corresponding to enum categories for the data points (especially useful when visualizing data pre divided into classes).如果我有一个与数据点的枚举类别相对应的数组(在可视化预先划分为类的数据时特别有用),那么这是我使用的单行。

import numpy as np
import matplotlib.pyplot as plt

num = 1000
x= np.random.rand(1,100)
y= np.random.rand(1,100)*2

# Creating a simple data point classification criteria, classes in this case will be 0, 1 and 2
classes = np.round(y)

# Passing in the classes for the "c" argument is super convinient
plt.scatter(x,y, c=classes,cmap=plt.cm.Set1)
plt.show()

Corresponding scatter plot that divides the graph into 3 colored regions:将图形划分为 3 个彩色区域的相应散点图:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM