Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
411 views
in Technique[技术] by (71.8m points)

python - How to change outliers to some other colors in a scatter plot

If I have a scatter plot like thisenter image description here

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

producing

enter image description here

Here, we plot points from a normal distribution, colorizing all points outside the distribution's width differently.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...