python - update figure inside loop -


i'm new python matplotlib , want make figure updated in loop. simple test code. want draw 3 graphs, each 2 seconds, different slopes each time. of course, know should read more matplotlib need quick. wrong below? thanks!

#!/usr/bin/env python  import matplotlib.pyplot plt import numpy np import time  def demo(a):     y = [xt*a+1 xt in x]     ax.plot(x,y)  if __name__ == '__main__':     plt.ion()     fig, ax = plt.subplots()     ax.set_ylim([0,15])     x = range(0,5)     in range(1,4):         demo(a)         time.sleep(2) 

i thought plt.ion() makes interactive , ax.plot(x,y) take effect instantly (but guess not). tried adding ax.draw() after ax.plot(x,y) requires arguments artist or don't know yet :). also, have error message coming (ubuntu 14.04.lts).

xlib:  extension "xinputextension" missing on display ":1.0". x error: baddrawable (invalid pixmap or window parameter) 9   major opcode: 62 (x_copyarea)   resource id:  0x0 

edit : add correct code below. (according estilus's solution modified little correct graph display.

#!/usr/bin/env python  import matplotlib.pyplot plt import numpy np import time  def demo(a):     plt.cla()     y = [xt*a+1 xt in x]     ax.set_ylim([0,15])     ax.plot(x,y)  if __name__ == '__main__':     plt.ion()     fig, ax = plt.subplots()     x = range(5)     in range(1,4):         demo(a)         plt.pause(3)         plt.draw() 

so plt.ion() needs paused short period of time interactive. otherwise, you'll run frozen white screen. secondly want use draw() update figure. therefore, code this:

import matplotlib.pyplot plt import numpy np import time  def demo(a):     y = [xt*a+1 xt in x]     ax.plot(x,y)  if __name__ == '__main__':     plt.ion()     fig, ax = plt.subplots()     ax.set_ylim([0,15])     x = range(0,5)     in range(1,4):         demo(a)         plt.pause(3)         plt.draw() 

i set pause time 3 seconds, pretty small number if you'd (like 0.000001). hope helped!


Comments

Popular posts from this blog

java - Suppress Jboss version details from HTTP error response -

gridview - Yii2 DataPorivider $totalSum for a column -

Sass watch command compiles .scss files before full sftp upload -