将点添加到现有的matplotlib散点图

将点添加到现有的matplotlib散点图,第1张

将点添加现有的matplotlib散点图

目前尚不清楚为什么

scatter
不接受按@ b-fg的建议创建第二个,但是可以编写如下函数:

def addPoint(scat, new_point, c='k'):    old_off = scat.get_offsets()    new_off = np.concatenate([old_off,np.array(new_point, ndmin=2)])    old_c = scat.get_facecolors()    new_c = np.concatenate([old_c, np.array(matplotlib.colors.to_rgba(c), ndmin=2)])    scat.set_offsets(new_off)    scat.set_facecolors(new_c)    scat.axes.figure.canvas.draw_idle()

这允许您将新点添加到现有点

PathCollection

例:

fig, ax = plt.subplots()scat = ax.scatter([0,1,2],[3,4,5],cmap=matplotlib.cm.spring, c=[0,2,1])fig.canvas.draw()  # if running all the pre in the same cell, this is required for it to work, not sure whyaddPoint(scat, [3,6], 'c')addPoint(scat, [3.1,6.1], 'pink')addPoint(scat, [3.2,6.2], 'r')addPoint(scat, [3.3,6.3], 'xkcd:teal')ax.set_xlim(-1,4)ax.set_ylim(2,7)

请注意,我提议的功能非常基础,根据使用情况,需要使其变得更加智能。重要的是要认识到

facecolors
a
PathCollection
中的数组不一定具有与点数相同的元素数,因此,如果您尝试一次添加多个点,或者原始点都是相同的颜色等



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

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

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

发表评论

登录后才能评论

评论列表(0条)

保存