[英]Interactive plot in Jupyter notebook
I am trying to get 'realtime' ploting in Jupyter notebook. 我试图在Jupyter笔记本中进行“实时”绘图。 The example can be found here . 这个例子可以在这里找到。 Here is the code: 这是代码:
%matplotlib notebook
import numpy as np
import matplotlib.pyplot as pl
from random import randint
from time import sleep
from ipywidgets import FloatProgress
from IPython import display
siz = 10
dat = np.zeros((siz, siz))
fig = pl.figure()
axe = fig.add_subplot(111)
img = axe.imshow(dat)
num = 1000
prgBar = FloatProgress(min=0, max=num-1)
display.display(prgBar)
for i in range(num):
prgBar.value = i
pos = (randint(0, siz-1), randint(0, siz-1))
dat[pos] += 1
img.set_data(dat)
img.autoscale()
#sleep(0.01)
What I am aiming for is to see how the plot is changing with each itteration. 我的目标是看每个迭代后情节如何变化。 I also tried set the interactive mod on by pl.ion(), changing backent to inline, calling pl.draw(), but non of it was working. 我也尝试通过pl.ion()设置交互式mod,更改后退到内联,调用pl.draw(),但是没有它正在工作。 BTW, the progressbar is working just fine... 顺便说一句,进度条工作得很好......
Thanks Radek 谢谢拉德克
The following code should do the trick: 以下代码应该可以解决问题:
import numpy as np
import matplotlib.pyplot as plt
from random import randint
from time import sleep
from ipywidgets import FloatProgress
from IPython.display import display, clear_output
siz = 10
dat = np.zeros((siz, siz))
fig = plt.figure()
axe = fig.add_subplot(111)
img = axe.imshow(dat)
num = 1000
prgBar = FloatProgress(min=0, max=num-1)
display(prgBar)
for i in range(num):
clear_output(wait = True)
prgBar.value = i
pos = (randint(0, siz-1), randint(0, siz-1))
dat[pos] += 1
img.set_data(dat)
img.autoscale()
display(fig)
I changed the for loop creating the image on each step and also imported clear_output
to clean the cell output on each step. 我更改了for循环,在每一步创建了图像,并导入了clear_output
来清理每一步的单元格输出。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.