gpt4 book ai didi

python - 如何在 Jupyter 中实时绘图

转载 作者:行者123 更新时间:2023-12-01 06:28:53 27 4
gpt4 key购买 nike

当我在顺序循环中填充 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 中可能吗?

最佳答案

编辑:添加了另一个解决方案。评论里哦..

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

这是适用于 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 和终端的工作方式略有不同。

关于python - 如何在 Jupyter 中实时绘图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59996283/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com