如这个问题所述,我正在尝试在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()
放在循环内,那么在每次迭代中图例都会增加...有什么解决办法吗?
display.clear_output
和display.display
行的顺序。这在你链接到的问题的答案中有更详细的讨论。 - tmdavison%matplotlib notebook
,您可以做得更好,因为它提供了一个实时的图形界面,您可以对其进行更新和重新绘制(以及平移/缩放和鼠标+键盘事件)。 - tacaswell%matplotlib notebook
。正在更新答案... - tmdavison%matplotlib inline
作为一种方法来消除第二个图形的出现(当单元格结束时)。交换display.clear_output
和display.display
的顺序对我没有起作用。我还没有找到正确使用%matplotlib notebook
进行动态绘图的方法,因此在此期间这是一种临时解决方案。我在笔记本中的其他地方都使用%matplotlib inline
,因此重置它可能对我有意义 :-) - SherylHohman