简体   繁体   English

我如何在 Jupyter 中实时绘图

[英]How do I plot in real time in Jupyter

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.当我在 jupyter 中以顺序循环填充数组并随着数组的增长使用 plt.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.我从控制台实时绘图没有问题,如图所示但这在 jupyter 中也不起作用。 When I run that code as a python script from terminal, the arrays print out but no plot at all.当我从终端将该代码作为 python 脚本运行时,数组打印出来但根本没有绘图。

I would like the plot to update in real time as the data is generated.我希望绘图在生成数据时实时更新。 Is this possible in jupyter?这在jupyter中可能吗?

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但是,将 plot.show() 放在放置它的位置只会生成 10 个单独的图形,而不是出现在同一图形上的连续迭代数据

Here it is a proper solution for jupyter notebooks.这是 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 循环的末尾添加 plt.show(),在 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.您链接的答案在循环后添加了 plt.show(),因此它只会显示最后创建的 plt.plot()。 In fact, the question linked is what you may need, because jupyter and terminal work slightly different.事实上,链接的问题是您可能需要的,因为 jupyter 和终端的工作方式略有不同。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM