在 Jupyter/iPython 中动态更新绘图的当前正确方法是什么? [英] What is the currently correct way to dynamically update plots in Jupyter/iPython?

查看:36
本文介绍了在 Jupyter/iPython 中动态更新绘图的当前正确方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在对如何在 ipython notebook 中的循环中动态更新绘图(在一个 cell 内),给出了如何在 Python 循环中动态更新 Jupyter notebook 中的绘图的示例.但是,这是通过在每次迭代时销毁并重新创建绘图来实现的,并且其中一个线程中的注释指出可以通过使用 new-ish %matplotlib nbagg 魔法来改善这种情况,它提供嵌入在笔记本中的交互式图形,而不是静态图像.

In the answers to how to dynamically update a plot in a loop in ipython notebook (within one cell), an example is given of how to dynamically update a plot inside a Jupyter notebook within a Python loop. However, this works by destroying and re-creating the plot on every iteration, and a comment in one of the threads notes that this situation can be improved by using the new-ish %matplotlib nbagg magic, which provides an interactive figure embedded in the notebook, rather than a static image.

然而,据我所知,这个美妙的新 nbagg 功能似乎完全没有记录,而且我无法找到如何使用它来动态更新绘图的示例.因此,我的问题是,如何使用 nbagg 后端有效地更新 Jupyter/Python notebook 中的现有绘图?由于在 matplotlib 中动态更新绘图通常是一个棘手的问题,一个简单的工作示例将是一个巨大的帮助.指向有关该主题的任何文档的指针也将非常有帮助.

However, this wonderful new nbagg feature seems to be completely undocumented as far as I can tell, and I'm unable to find an example of how to use it to dynamically update a plot. Thus my question is, how does one efficiently update an existing plot in a Jupyter/Python notebook, using the nbagg backend? Since dynamically updating plots in matplotlib is a tricky issue in general, a simple working example would be an enormous help. A pointer to any documentation on the topic would also be extremely helpful.

要清楚我的要求:我想要做的是运行一些模拟代码进行几次迭代,然后绘制其当前状态的图,然后再运行几次迭代,然后更新绘图以反映当前状态,等等.所以我们的想法是绘制一个图,然后在没有用户任何交互的情况下更新图中的数据,而不会破坏和重新创建整个事物.

To be clear what I'm asking for: what I want to do is to run some simulation code for a few iterations, then draw a plot of its current state, then run it for a few more iterations, then update the plot to reflect the current state, and so on. So the idea is to draw a plot and then, without any interaction from the user, update the data in the plot without destroying and re-creating the whole thing.

这里是从上面链接问题的答案中稍微修改的代码,它通过每次重新绘制整个图形来实现这一点.我想获得相同的结果,但更有效地使用 nbagg.

Here is some slightly modified code from the answer to the linked question above, which achieves this by re-drawing the whole figure every time. I want to achieve the same result, but more efficiently using nbagg.

%matplotlib inline
import time
import pylab as pl
from IPython import display
for i in range(10):
    pl.clf()
    pl.plot(pl.randn(100))
    display.display(pl.gcf())
    display.clear_output(wait=True)
    time.sleep(1.0)

推荐答案

这是一个循环更新绘图的示例.它更新图中的数据,并不会每次都重新绘制整个图形.它确实会阻止执行,但如果您有兴趣运行一组有限的模拟并将结果保存在某处,这对您来说可能不是问题.

Here is an example that updates a plot in a loop. It updates the data in the figure and does not redraw the whole figure every time. It does block execution, though if you're interested in running a finite set of simulations and saving the results somewhere, it may not be a problem for you.

%matplotlib notebook

import numpy as np
import matplotlib.pyplot as plt
import time

def 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 的最佳方式.

There is an IPython Widget version of nbagg that is currently a work in progress at the Matplotlib repository. When that is available, that will probably be the best way to use nbagg.

更新以显示多个图

这篇关于在 Jupyter/iPython 中动态更新绘图的当前正确方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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