Python 使用qt5自动更新jupyter笔记本中的图形
如何在jupyter笔记本中执行此操作:Python 使用qt5自动更新jupyter笔记本中的图形,python,matplotlib,qt5,jupyter,Python,Matplotlib,Qt5,Jupyter,如何在jupyter笔记本中执行此操作: %matplotlib notebook import numpy as np import matplotlib.pyplot as plt m = 100 n = 100 matrix = np.random.normal(0,1,m*n).reshape(m,n) fig = plt.figure() ax = fig.add_subplot(111) plt.ion() fig.show() fig.canvas.draw() for i
%matplotlib notebook
import numpy as np
import matplotlib.pyplot as plt
m = 100
n = 100
matrix = np.random.normal(0,1,m*n).reshape(m,n)
fig = plt.figure()
ax = fig.add_subplot(111)
plt.ion()
fig.show()
fig.canvas.draw()
for i in range(0,100):
#ax.clear()
plt.plot(matrix[i,:])
fig.canvas.draw()
但是使用“%matplotlib qt5”而不是“笔记本”
当我尝试它时,它仅在循环结束后才显示图形。我希望看到它更新每个绘图。原则上,您可以在交互模式下执行以下操作:
%matplotlib qt4
import numpy as np
import matplotlib.pyplot as plt
m = 100
n = 100
matrix = np.random.normal(0,1,m*n).reshape(m,n)
fig = plt.figure()
ax = fig.add_subplot(111)
plt.ion()
plt.draw()
for i in range(0,30):
#ax.clear()
plt.plot(matrix[i,:])
plt.draw()
plt.pause(0.1)
plt.ioff()
plt.show()
但是,在完成循环后,由于使用了Qt,它可能会在jupyter中崩溃。
但是,当使用tk后端时,%matplotlib tk
,它应该可以工作
您可能想考虑使用<代码>函数> <代码>,而不是交互模式。虽然这与Jupyter中的Qt有相同的限制,但我发现使用函数更新绘图更直观,并且不需要手动重新绘制画布
%matplotlib tk
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.animation
m = 100
n = 100
matrix = np.random.normal(0,1,m*n).reshape(m,n)
fig = plt.figure()
ax = fig.add_subplot(111)
def update(i):
#ax.clear()
plt.plot(matrix[i,:])
ani = matplotlib.animation.FuncAnimation(fig, update, frames=30, repeat=False)
plt.show()
我问了一个问题,为什么这对qt后端不起作用