如何在散点图中包围不同的数据集? [英] How do I encircle different data sets in scatter plot?
本文介绍了如何在散点图中包围不同的数据集?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在寻找的东西是这样的:
另外,我如何用(阴影)颜色填充圆圈?
您可能会得到路径通过凸包 scipy.spatial.ConvexHull
。 import matplotlib.pyplot as plt
import numpy as np; np.random.seed(1)
from scipy.spatial import ConvexHull
x1,y1 = np.random.normal(loc = 5,scale = 2,size =(2,15 ))
x2,y2 = np.random.normal(loc = 8,scale = 2.5,size =(2,13))
plt.scatter(x1,y1)
plt.scatter(x2,y2)
def encircle(x,y,ax = None,** kw):
如果不是ax:ax = plt.gca()
p = np.c_ [x,y]
hull = ConvexHull(p)
poly = plt.Polygon(p [hull.vertices,:],** kw)
ax .add_patch(poly)
encircle(x1,y1,ec =k,fc =gold,alpha = 0.2)
包围(x2,y2,ec =orange ,fc =none)
plt.show()
< a href =https://i.stack.imgur.com/Wvqhn.png =nofollow noreferrer>
另一个选择是围绕点云的平均值绘制一个圆。
import matplotlib.pyplot as plt
import numpy as np; np.random.seed(1)
from scipy.spatial import ConvexHull
x1,y1 = np.random.normal(loc = 5,scale = 2,size =(2,15 ))
x2,y2 = np.random.normal(loc = 8,scale = 2.5,size =(2,13))
plt.scatter(x1,y1)
plt.scatter(x2,y2)
def encircle2(x,y,ax = None,** kw):
如果不是ax:ax = plt。 gca()
p = np.c_ [x,y]
mean = np.mean(p,axis = 0)
d = p-mean
r = np.max(np .sqrt(d [:,0] ** 2 + d [:,1] ** 2))
circ = plt.Circle(mean,radius = 1.05 * r,** kw)
ax.add_patch(circ)
encircle2(x1,y1,ec =k,fc =gold,alpha = 0.2)
encircle2(x2,y2,ec =orange ),fc =none)
plt.gca()。relim()
plt.gca()。autoscale_view()
plt.show()
How do I encircle different data sets in scatter plot?
What I'm looking for is something like this:
Also, how do I thereafter fill in the circle with a (shaded) colour?
解决方案
You may get the path that incoporates all points via a convex hull scipy.spatial.ConvexHull
.
import matplotlib.pyplot as plt
import numpy as np; np.random.seed(1)
from scipy.spatial import ConvexHull
x1, y1 = np.random.normal(loc=5, scale=2, size=(2,15))
x2, y2 = np.random.normal(loc=8, scale=2.5, size=(2,13))
plt.scatter(x1, y1)
plt.scatter(x2, y2)
def encircle(x,y, ax=None, **kw):
if not ax: ax=plt.gca()
p = np.c_[x,y]
hull = ConvexHull(p)
poly = plt.Polygon(p[hull.vertices,:], **kw)
ax.add_patch(poly)
encircle(x1, y1, ec="k", fc="gold", alpha=0.2)
encircle(x2, y2, ec="orange", fc="none")
plt.show()
Another option is to draw a circle around the mean of the point cloud.
import matplotlib.pyplot as plt
import numpy as np; np.random.seed(1)
from scipy.spatial import ConvexHull
x1, y1 = np.random.normal(loc=5, scale=2, size=(2,15))
x2, y2 = np.random.normal(loc=8, scale=2.5, size=(2,13))
plt.scatter(x1, y1)
plt.scatter(x2, y2)
def encircle2(x,y, ax=None, **kw):
if not ax: ax=plt.gca()
p = np.c_[x,y]
mean = np.mean(p, axis=0)
d = p-mean
r = np.max(np.sqrt(d[:,0]**2+d[:,1]**2 ))
circ = plt.Circle(mean, radius=1.05*r,**kw)
ax.add_patch(circ)
encircle2(x1, y1, ec="k", fc="gold", alpha=0.2)
encircle2(x2, y2, ec="orange", fc="none")
plt.gca().relim()
plt.gca().autoscale_view()
plt.show()
这篇关于如何在散点图中包围不同的数据集?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文