Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/296.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何在Jupyter中实时绘图_Python_Matplotlib_Jupyter Notebook_Jupyter - Fatal编程技术网

Python 如何在Jupyter中实时绘图

Python 如何在Jupyter中实时绘图,python,matplotlib,jupyter-notebook,jupyter,Python,Matplotlib,Jupyter Notebook,Jupyter,当我以顺序循环的方式在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('arra

当我以顺序循环的方式在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也不起作用。 当我从terminal以python脚本的形式运行代码时,数组会打印出来,但根本不会打印


我想在生成数据时实时更新绘图。这在jupyter中可能吗?

编辑:添加了另一个解决方案。评论中的Op


谢谢你的回复。但是,将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)
如果每次迭代都需要绘图,则必须在for循环末尾的plt.plot之后添加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和terminal的工作原理略有不同。

那么,您是否在为for循环中的每个
plt.plot
寻找多个绘图窗口?另外,我想知道为什么您的代码中没有
plt.show()
。几乎重复:--除了这个问题希望绘图按顺序显示而不是替换。谢谢您的回答。但是,将plot.show()放在您放置它的位置只会生成10个单独的图形,而不是出现在同一平面上的连续迭代的数据graph@aquagremlin更新了答案,现在对你有用了