【发布时间】:2015-11-10 03:36:29
【问题描述】:
如this question 中所述,我正在尝试在 iPython 笔记本中(在一个单元格中)动态更新绘图。 不同之处在于我不想绘制新线,但我的 x_data 和 y_data 在某个循环的每次迭代中都在增长。
我想做的是:
import numpy as np
import time
plt.axis([0, 10, 0, 100]) # supoose I know what the limits are going to be
plt.ion()
plt.show()
x = []
y = []
for i in range(10):
x = np.append(x, i)
y = np.append(y, i**2)
# update the plot so that it shows y as a function of x
time.sleep(0.5)
但我希望情节有一个传奇,如果我这样做了
from IPython import display
import time
import numpy as np
plt.axis([0, 10, 0, 100]) # supoose I know what the limits are going to be
plt.ion()
plt.show()
x = []
y = []
for i in range(10):
x = np.append(x, i)
y = np.append(y, i**2)
plt.plot(x, y, label="test")
display.clear_output(wait=True)
display.display(plt.gcf())
time.sleep(0.3)
plt.legend()
我最终得到一个包含 10 个项目的图例。如果我将plt.legend() 放入循环中,则图例会在每次迭代中增长……有什么解决方案吗?
【问题讨论】:
标签: python matplotlib ipython-notebook