2015-09-18 77 views
2

我想将点“活”添加到matplotlib中的散点图,以便一旦它们被计算出来,点就出现在图上。可能吗? 如果没有,是否有一个python兼容的类似的绘图平台,可以做到这一点? 谢谢!将点添加到matlibplot散点图live

+2

您是否在寻找[这](https://docs.python.org/2/library/turtle html的)? –

回答

6

您可以将新点添加到返回值为ax.scatteroffsets数组中。

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

这吸引了来自二维标准正态分布,并增加了点到散点图:

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() 
+0

它的工作原理。但是,我怎样才能使图表自动重新缩放?谢谢! – geodude

+0

我添加了几行来更新xlim和ylim – MaxNoe

+0

谢谢!有用。 – geodude