将点实时添加到matlibplot散点图 [英] Add points to matlibplot scatter plot live

查看:34
本文介绍了将点实时添加到matlibplot散点图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将点实时"添加到 matplotlib 中的散点图中,以便这些点在计算后立即显示在图表上.是否可以?如果没有,是否有一个与 python 兼容的类似绘图平台可以做到这一点?谢谢!

I would like to add points "live" to a scatter plot in matplotlib, so that the points appear on the graph as soon as they are computed. Is it possible? If not, is there a python-compatible similar plotting platform where this can be done? Thanks!

推荐答案

您可以将新点追加到 ax.scatter 返回值的 offsets 数组中.

You can append new points to the offsets array of the return value of ax.scatter.

您需要使绘图与 plt.ion()交互,并使用 fig.canvas.update()更新绘图.

You need to make the plot interactive with plt.ion() and update the plot with fig.canvas.update().

这从二维标准正态分布中提取并将点添加到散点图:

This draws from a 2d standard normal distribution and adds the point to the scatter plot:

import matplotlib.pyplot as plt
import numpy as np

plt.ion()

fig, ax = plt.subplots()

plot = ax.scatter([], [])
ax.set_xlim(-5, 5)
ax.set_ylim(-5, 5)

while True:
    # get two gaussian random numbers, mean=0, std=1, 2 numbers
    point = np.random.normal(0, 1, 2)
    # get the current points as numpy array with shape  (N, 2)
    array = plot.get_offsets()

    # add the points to the plot
    array = np.append(array, point)
    plot.set_offsets(array)

    # update x and ylim to show all points:
    ax.set_xlim(array[:, 0].min() - 0.5, array[:,0].max() + 0.5)
    ax.set_ylim(array[:, 1].min() - 0.5, array[:, 1].max() + 0.5)
    # update the figure
    fig.canvas.draw()

这篇关于将点实时添加到matlibplot散点图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆