6

I have a graph with multiple data sets on it. I need to continually redraw these lines, each separately, as the data is updated. How can I delete and reestablish it repeatedly, preferably without having to delete the entire graph and redraw all of the lines on it every time?

Elliot
  • 5,211
  • 10
  • 42
  • 70
  • The plotting is going on inside a function being passed into the scipy fmin function, which means that I cannot permanently assign persistent names at each plot command due to scoping. – Elliot Jul 09 '12 at 17:00

2 Answers2

5
#!/usr/bin/env python

import time
from pylab import *

ion() # turn interactive mode on

# initial data
x = arange(-8, 8, 0.1);
y1 = sin(x)
y2 = cos(x)

# initial plot
line1, line2, = plot(x, y1, 'r', x, y2, 'b')
line1.axes.set_xlim(-10, 10)
line1.axes.set_ylim(-2, 2)
line1.set_label("line1")
line2.set_label("line2")
legend()
grid()
draw()

# update line 1
for i in xrange(50):
    time.sleep(0.1)

    # update data
    y1 = sin(x + float(i) / 10)

    # update plot
    line1.set_ydata(y1)
    draw()

# update line 2
for i in xrange(50):
    time.sleep(0.1)

    # update data
    y2 = cos(x + float(i) / 10)

    # update plot
    line2.set_ydata(y2)
    draw()
none
  • 11,793
  • 9
  • 51
  • 87
  • When I try to make line objects like this, python complains that it is a 'NoneType' and thus not iterable. – Elliot Jul 09 '12 at 20:39
  • is it related to [this](http://stackoverflow.com/questions/3887381/python-typeerror-nonetype-object-is-not-iterable) by any chance? – none Jul 09 '12 at 21:25
  • Actually it turns out that this does work normally. I can't use it in my case because the inter-threading wrapper being used doesn't allow the assignment of the plot lines, but it is a valid answer. – Elliot Jul 11 '12 at 14:11
1

Look for animation API of Matplotlib. There are some examples too...

Charles Brunet
  • 21,797
  • 24
  • 83
  • 124