Interactive plot in a Jupyter laptop

I am trying to get real-time graphics in a Jupyter laptop. An 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'm going to do is see how the plot changes with each iteration. I also tried installing the interactive mod on pl.ion (), changing the backent to inline by calling pl.draw (), but it didn't work. BTW, progressbar works fine ...

Thanks Radek

+5
source share
1 answer

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 to create an image at each step, and also imported clear_output to clear the cell output at each step.

+1
source

Source: https://habr.com/ru/post/1245258/


All Articles