在Jupyter/iPython中动态更新图的当前正确方法是什么? [英] What is the currently correct way to dynamically update plots in Jupyter/iPython?
问题描述
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笔记本中的现有图?由于在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)
nbagg
的 IPython Widget版本目前正在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屋!