【问题标题】:How do I plot in real time in Jupyter如何在 Jupyter 中实时绘图
【发布时间】:2020-01-31 00:03:53
【问题描述】:

当我在 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

如图所示,我可以从控制台实时绘图 here 但这在 jupyter 中也不起作用。 当我从终端将该代码作为 python 脚本运行时,数组打印出来但根本没有绘图。

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

【问题讨论】:

标签: python matplotlib jupyter-notebook jupyter


【解决方案1】:

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

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

这是一个适合 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)

如果每次迭代都需要绘图,则必须在 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()

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

【讨论】:

  • 感谢您的回复。但是,将 plot.show() 放在您放置它的位置只会生成 10 个单独的图表,而不是出现在同一图表上的连续迭代的数据
  • @aquagremlin 更新了答案,现在它对你有用
猜你喜欢
  • 2020-02-22
  • 2016-10-10
  • 2021-10-25
  • 2021-06-13
  • 1970-01-01
  • 2021-07-27
  • 1970-01-01
  • 2021-02-15
  • 1970-01-01
相关资源
最近更新 更多