Reputation: 179
I am trying to get 'realtime' ploting in Jupyter notebook. 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. BTW, the progressbar is working just fine...
Thanks Radek
Upvotes: 6
Views: 7050
Reputation: 4219
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.
Upvotes: 1