python 散点图连成一片_将点添加到现有的matplotlib散点图

现在还不清楚为什么不能按照@b-fg的建议创建第二个scatter,但是可以编写这样的函数: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 code in the same cell, this is required for it to work, not sure why

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

请注意,我建议的功能是非常基本的,并且需要根据用例使其更加智能化。重要的是要认识到PathCollection中的facecolors数组不一定具有与点数相同的元素数,因此,如果您尝试一次添加多个点数,或者如果原始点数都是相同的颜色,则颜色可能会发生有趣的事情,等等。。。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值