温馨提示:本文翻译自stackoverflow.com,查看原文请点击:python - How do I plot in real time in Jupyter
jupyter-notebook matplotlib python jupyter

python - 如何在Jupyter中实时绘制

发布于 2020-03-30 21:31:45

当我在顺序循环中以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

我毫不费力地从控制台实时绘制,如此处所示 但这在jupyter中也不起作用。当我从终端将这些代码作为python脚本运行时,阵列会打印出来,但根本没有显示。

我希望在生成数据时实时更新情节。在jupyter中有可能吗?

查看更多

提问者
aquagremlin
被浏览
91
Noki 2020-02-03 15:42

编辑:添加了另一个解决方案。在评论中操作。

谢谢您的回复。但是,将plot.show()放置在其上的位置只会生成10个单独的图形,而不是同一图形上出现的连续迭代的数据

这是适用于Jupyter笔记本电脑的解决方案。

%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)

如果每次迭代都需要一个图,则必须在plt.plot之后的for循环末尾添加plt.show():

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()

您链接的答案将在循环后添加plt.show(),因此它将仅显示最后创建的plt.plot()。实际上,链接的问题是您可能需要的,因为jupyter和终端的工作方式略有不同。