【发布时间】:2021-06-13 17:21:53
【问题描述】:
我想动态更新单元格的绘图。 IE。该图在单元格的开头初始化,并在(计算量大的)for循环中更新,显示计算的进展情况。在jupyter notebook 中,这可以使用pneumatics solution 中的What is the currently correct way to dynamically update plots in Jupyter/iPython? 来完成
%matplotlib notebook
import numpy as np
import matplotlib.pyplot as plt
import time
def pltsin(ax, colors=['b']):
x = np.linspace(0,1,100)
if ax.lines:
for line in ax.lines:
line.set_xdata(x)
y = np.random.random(size=(100,1))
line.set_ydata(y)
else:
for color in colors:
y = np.random.random(size=(100,1))
ax.plot(x, y, color)
fig.canvas.draw()
fig,ax = plt.subplots(1,1)
ax.set_xlabel('X')
ax.set_ylabel('Y')
ax.set_xlim(0,1)
ax.set_ylim(0,1)
for f in range(5):
pltsin(ax, ['b', 'r'])
time.sleep(1)
我正在jupyter lab 中寻找等效的方法。我尝试使用ipympl 库将%matplotlib notebook 替换为%matplotlib widget,但这不起作用:该图仅在循环完成后显示。
我不想要像Ziofil in 或Paidoo in jupyterlab interactive plot 那样的解决方案,它们会清除整个输出,因为我可能会打印其他内容,例如一个tqdm进度条
【问题讨论】:
-
也许这个使用
IPython.display.display.update的答案会有所帮助:stackoverflow.com/a/65400882/14396787
标签: matplotlib jupyter-lab ipywidgets