Warm tip: This article is reproduced from stackoverflow.com, please click
jupyter-notebook matplotlib python jupyter

How do I plot in real time in Jupyter

发布于 2020-03-30 21:13:30

When i populate an array in jupyter in a sequential loop and print the array as it grows with a plt.plot statement, I can get a print out of the arrays individually but only one plot.

import numpy as np
import matplotlib.pyplot as plt
import time
muarr = np.linspace(0,10,10)
print('muarray')
print(muarr)

z = np.linspace(0.0,1.0,10)  # create an array
print('array z')
print(z)

def fillit(mu):
    x = 10  # initial x value
    for i in range(0,10):   # fill n2-n1 iterations
        z[i] = i * x * mu
    return z  # returning the array

for i in range(0,10):  
        mu = muarr[i]          #for a specific horizontal axis location
        print()
        print('iteration '+ str(i))
        print('muarray '+str(i))
        print('mu = '+str(mu))
        y=fillit(mu)  # an array of 10 elements from 0 to 100*mu
        print('array y is an array of 10 elements from 0 to 100*mu')
        print (y)
        x=y*0.0 + mu   # dummy x value is all mu 
        print('array x is just all mu so that each x,y pt can be plotted')
        print (x)
        plt.plot(x,y,'ko',markersize=1)   # k=black, plot small points

I have no trouble plotting in real time from the console as illustrated here but that doesn't work in jupyter either. When I run that code as a python script from terminal, the arrays print out but no plot at all.

I would like the plot to update in real time as the data is generated. Is this possible in jupyter?

Questioner
aquagremlin
Viewed
59
Noki 2020-02-03 15:42

EDIT: Added another solution. Op in the comments..

thank you for your reply. However, putting plot.show() where you placed it only generates 10 individual graphs, not the data on successive iterations appearing on the same graph

Here it is a proper solution for jupyter notebooks.

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


muarr = np.linspace(0,10,10)
print('muarray')
print(muarr)

z = np.linspace(0.0,1.0,10)  # create an array
print('array z')
print(z)

def fillit(mu):
    x = 10  # initial x value
    for i in range(0,10):   # fill n2-n1 iterations
        z[i] = i * x * mu
    return z  # returning the array

fig = plt.figure()
ax = fig.add_subplot(111)
plt.ion()

fig.show()
fig.canvas.draw()

for i in range(0,10):  
        mu = muarr[i]          #for a specific horizontal axis location
        print()
        print('iteration '+ str(i))
        print('muarray '+str(i))
        print('mu = '+str(mu))
        y=fillit(mu)  # an array of 10 elements from 0 to 100*mu
        print('array y is an array of 10 elements from 0 to 100*mu')
        print (y)
        x=y*0.0 + mu   # dummy x value is all mu 
        print('array x is just all mu so that each x,y pt can be plotted')
        print (x)
        ax.plot(x,y,'ko',markersize=1)
        fig.canvas.draw()
        time.sleep(1)

If you need a plot for each iteration, you must add plt.show() at the end of the for loop, after the plt.plot:

for i in range(0,10):  
        mu = muarr[i]          #for a specific horizontal axis location
        print()
        print('iteration '+ str(i))
        print('muarray '+str(i))
        print('mu = '+str(mu))
        y=fillit(mu)  # an array of 10 elements from 0 to 100*mu
        print('array y is an array of 10 elements from 0 to 100*mu')
        print (y)
        x=y*0.0 + mu   # dummy x value is all mu 
        print('array x is just all mu so that each x,y pt can be plotted')
        print (x)
        plt.plot(x,y,'ko',markersize=1)   # k=black, plot small points
        plt.show()

The answer you are linking adds plt.show() after the loop, so it will only show the last plt.plot() created. In fact, the question linked is what you may need, because jupyter and terminal work slightly different.