Matplotlib in Python - drawing shapes and animating them

So, I present the Token Ring network (doing the simulation in SimPy), I am completely new to matplotlib, but I was told that it would be really good to represent the visualizations of my simulation.

So, I searched googled and found out how to draw shapes and lines - using add_patch and add_line respectively for the axes (I think). So now I have this conclusion, which is absolutely beautiful:

(still unable to send images!) http://img137.imageshack.us/img137/7822/screenshot20100121at120.png

But I get this using the pylab.show () function, and I think I want to achieve this using the pylab.plot () function so that I can then update it, since my simulation is progressing using pylab. draw () after.

My code is as follows:

plab.ion()

plab.axes()

for circ in self.circleList:
    plab.gca().add_patch(circ)

for line in self.lineList:
    plab.gca().add_line(line)

plab.axis('scaled')
plab.show()

Where circleList and lineList are lists containing circles and lines in a diagram

I probably misunderstand something simple here, but in fact I cannot find examples that are not explicitly based on the graph using the plot () function.


Clarification:

How can I get the same result using pylab.plot () instead of pylab.show ()?

+5
source share
3 answers

Image replication using the graphing method:

from pylab import *

points = []
points.append((-0.25, -1.0))
points.append((0.7, -0.7))
points.append((1,0))
points.append((0.7,1))
points.append((-0.25,1.2))
points.append((-1,0.5))
points.append((-1,-0.5))
points.append((-0.25, -1.0))

a_line = plot(*zip(*points))[0]
a_line.set_color('g')
a_line.set_marker('o')
a_line.set_markerfacecolor('b')
a_line.set_markersize(30)
axis([-1.5,1.5,-1.5,1.5])

show()

EDIT ON COMMENTS

python matplotlib . , .

# general imports
import random, time
from multiprocessing import Process, Queue

# for matplotlib
import random
import numpy as np
import matplotlib
matplotlib.use('GTKAgg') # do this before importing pylab

import matplotlib.pyplot as plt
from matplotlib.patches import Circle


def matplotLibAnimate(q,points):

    # set up initial plot
    fig = plt.figure()
    ax = fig.add_subplot(111)


    circles = []
    for point in points:
        ax.add_patch(Circle(point,0.1))

    a_line, = ax.plot(*zip(*points))
    a_line.set_color('g')
    a_line.set_lw(2)

    currentNode = None  
    def animate(currentNode = currentNode):
        while 1:
            newNode = q.get()
            if currentNode: currentNode.remove()
            circle = Circle(newNode,0.1)
            currentNode = ax.add_patch(circle)
            circle.set_fc('r')
            fig.canvas.draw()

    # start the animation
    import gobject
    gobject.idle_add(animate)
    plt.show()

#initial points
points = ((-0.25, -1.0),(0.7, -0.7),(1,0),(0.7,1),(-0.25,1.2),(-1,0.5),(-1,-0.5),(-0.25, -1.0))
q = Queue()
p = Process(target = matplotLibAnimate, args=(q,points,))
p.start()

# feed animation data
while 1:
    time.sleep(random.randrange(4))
    q.put(random.sample(points,1)[0])

, , Whatnick Image. matplotlibs, . "" , PNG .

+4

, set_data. draw(). , set_data. pyvtk. - png , .

+1

It seems that Mark has the answer you were looking for, but if you decide to go with some approximation and create an animation from separate png, here is the code for implementing the Amit sentence for using mencoder (from http://en.wikibooks.org/ wiki / Mplayer ):

mencoder mf://*.png -mf w=400:h=400 -ovc lavc -lavcopts vcodec=xvid -of avi -o output.avi
+1
source

All Articles