Matplotlib updating axis



我正在寻找一种方法,它可以让我在脚本运行时更新axis,但我找不到。例如:

import matplotlib.pyplot as plt
x = (1,2)
y = (1,2)
plt.plot(x,y, 'r-')
plt.show()
#here during run of program I want to clear axis with help of plt.cla() and update it 
#new one
# x = (2,4)
# y = (2,4)
#plt.plot(x,y, 'b-')

要获得所需的功能,您可能需要将matplotlib设置为交互模式:

plt.ion()

然后调用draw进行更新。例如:

import numpy
import time
x = (1,2)
y = (1,2)
plt.ion()
plt.plot(x,y, 'r-')
plt.draw()
for i in range(100):
   print i
   time.sleep(1)
   plt.cla()
   y = (numpy.random.normal(), numpy.random.normal())
   plt.plot(x,y, 'r-')
   plt.draw()

最新更新