如何在散点图中包围不同的数据集?

如何在散点图中包围不同的数据集?,第1张

如何在散点图中包围不同的数据集?

可能会获得通过凸包将所有点合并的路径

scipy.spatial.ConvexHull

import matplotlib.pyplot as pltimport numpy as np; np.random.seed(1)from scipy.spatial import ConvexHullx1, 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()

另一种选择是在点云的平均值周围画一个圆。

import matplotlib.pyplot as pltimport numpy as np; np.random.seed(1)from scipy.spatial import ConvexHullx1, 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()



欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5663038.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-16
下一篇 2022-12-16

发表评论

登录后才能评论

评论列表(0条)

保存