update figure inside loop
So plt.ion() needs to be paused for a short period of time for it to be interactive. Otherwise, you'll just run into a frozen white screen. Secondly you want to use draw() to update the figure. Therefore, your code would look something like this:
import matplotlib.pyplot as plt
import numpy as np
import time
def demo(a):
y = [xt*a+1 for 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)
for a in range(1,4):
demo(a)
plt.pause(3)
plt.draw()
I set the pause time to be 3 seconds, but it could be a pretty small number if you'd like (like 0.000001). Hope this helped!
Chan Kim
Hi folks, I used to be a hardware engineer, but I am working on S/W area these years. Mostly I'll get help from this site, but I hope someday I can give helps to others. Thank you! Chan Kim visit my home page : http://chankim.dothome.co.kr
Updated on June 04, 2022Comments
-
Chan Kim almost 2 years
I'm new to Python
matplotlib
and want to make a figure updated in a loop. This is some simple test code. I want to draw 3 graphs, each for 2 seconds, with different slopes each time. Of course, I know I should read some more aboutmatplotlib
but I need to do something quick. What is wrong below? Thanks!#!/usr/bin/env python import matplotlib.pyplot as plt import numpy as np import time def demo(a): y = [xt*a+1 for 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) for a in range(1,4): demo(a) time.sleep(2)
I thought
plt.ion()
makes it interactive andax.plot(x,y)
take effect instantly (but guess not). I tried addingax.draw()
afterax.plot(x,y)
but it requires some arguments like artist or something which I don't know yet :). Also, I have this 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 : I add the correct code below. (according to Estilus's solution to which I modified a little for correct graph display.
#!/usr/bin/env python import matplotlib.pyplot as plt import numpy as np import time def demo(a): plt.cla() y = [xt*a+1 for xt in x] ax.set_ylim([0,15]) ax.plot(x,y) if __name__ == '__main__': plt.ion() fig, ax = plt.subplots() x = range(5) for a in range(1,4): demo(a) plt.pause(3) plt.draw()
-
Chan Kim almost 8 yearsI did exactly as you showed me but it gives me this error. (maybe not python related). and I see no figures excepts a short flashing figure window after a couple of seconds. 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
-
Frodon almost 8 years@ChanKim this is not Python related. It's a problem with your graphic environment (virtual machine ?)
-
Chan Kim almost 8 yearsI know, so I copied your code to my CentOS6.7 machine, but no picture window shows up. The script just ends after some seconds. What could be wrong? Are you sure your modified code runs ok in your machine?
-
Chan Kim almost 8 yearsHi, this works. except I moved ax.set_ylim([0,15]) to inside demo(). otherwise the ylim is not correctly set. Thanks!