这是一个循环更新图的示例。它会更新图中的数据,并且不会每次都重新绘制整个图。它确实会阻止执行,但是,如果您有兴趣运行有限的一组模拟并将结果保存在某处,则对您来说可能不是问题。
%matplotlib notebookimport numpy as npimport matplotlib.pyplot as pltimport timedef pltsin(ax, colors=['b']): x = np.linspace(0,1,100) if ax.lines: for line in ax.lines: line.set_xdata(x) y = np.random.random(size=(100,1)) line.set_ydata(y) else: for color in colors: y = np.random.random(size=(100,1)) ax.plot(x, y, color) fig.canvas.draw()fig,ax = plt.subplots(1,1)ax.set_xlabel('X')ax.set_ylabel('Y')ax.set_xlim(0,1)ax.set_ylim(0,1)for f in range(5): pltsin(ax, ['b', 'r']) time.sleep(1)我把它放在这里的nbviewer上。
有一个IPython
Widget版本
nbagg,目前正在Matplotlib存储库中进行。如果可以的话,那可能是最好的使用方式
nbagg。
编辑:更新以显示多个图



