3

I have this piece of code:

fig,ax=subplots(figsize=(20,10))

#ax=plot(matriz[0],matriz[1],color='black',lw=0,marker='+',markersize=10)
#ax=plot(matriz[2],matriz[3],color='blue',lw=0,marker='o',markersize=10)
#show ()
def animate(i):
    ax=plot((matt[i][0],matt[i][2]),(matt[i][1],matt[i][3]),lw=0,color='r-',marker='o',markersize=8)
    return ax

anim=animation.FuncAnimation(fig,animate,frames=numlin, interval=1000,blit=True,repeat=0)
show()

I really don't have experience with matplotlib, but my boss asked me to paint (in each iteration) each point with a different color (i.e. point 1 in red, point 2 in blue and so on). I want to paint each point with a different color, but it should keep the same color in the next iteration.

How can I do this in matplotlib?

J Richard Snape
  • 20,116
  • 5
  • 51
  • 79
shinjidev
  • 383
  • 1
  • 6
  • 21
  • 4
    Totally, check out http://matplotlib.org/examples/pylab_examples/scatter_demo2.html. Every point effectively has a (x,y,size,color) tuple, although the actual function arguments are a vector of x-values, a vector of ... etc. – cphlewis Mar 02 '15 at 23:44
  • @cphlewis I can see the example, but I don't understand which line adds the color for each point. Is it possible to do with animate? – shinjidev Mar 02 '15 at 23:47
  • 2
    In `ax.scatter(delta1[:-1], delta1[1:], c=close, s=volume, alpha=0.5)`, the arguments are (in order) vectors of x, y, color, size, transparency. Experiment with passing a `range` of the right size as the color argument. Once you have scatter plots you like, FuncAnimation will do the same with them as with any plots. – cphlewis Mar 02 '15 at 23:53
  • 2
    @cphlewis thanks, I'm going play with this piece of code to understand how to do it. Really thank you :) – shinjidev Mar 02 '15 at 23:59
  • Also take a look at http://stackoverflow.com/questions/12965075/matplotlib-scatter-plot-colour-as-function-of-third-variable/12965761#12965761 – Warren Weckesser Mar 03 '15 at 01:17
  • @WarrenWeckesser I've tried to play with that code and I got this code https://gist.github.com/anonymous/f65d9144f66d22001fb2 . But It's painting in each iteration both points in the same color. I want to paint each point with a different color, but It should keep the same color in the next iteration. – shinjidev Mar 03 '15 at 13:50

1 Answers1

3

I think I see what you want to do and, yes, I think it is possible. First, I have set up some random data to simulate what I think you have in matt

from random import random as r

numlin=50

matt = []
for j in xrange(numlin):
    matt.append([r()*20, r()*10,r()*20,r()*10])

Now, using your code as closely as possible, I think you want to do this (I've added an init() function, which just returns an empty list, otherwise your first set of points stays on the axis throughout):

from matplotlib.pyplot import plot, show, subplots
import matplotlib.animation as animation

fig,ax=subplots(figsize=(20,10))
ax.set_xlim([0,20])
ax.set_ylim([0,10])


def animate(i):
    animlist = plot(matt[i][0],matt[i][1],'r',matt[i][2],matt[i][3],'b',marker='o',markersize=8)
    return animlist

def init():
    return []

anim=animation.FuncAnimation(fig,animate,frames=numlin,interval=1000,init_func=init,blit=True,repeat=0)
show()

How it works

Passing in sets of (x0,y0,c0, x1,y1,c1, x2,y2,c2 ... ) into plot() is valid where cx is a valid matplotlib colour format. They go before any named **kwargs like marker etc. It's described in the docs here.

An arbitrary number of x, y, fmt groups can be specified, as in:

a.plot(x1, y1, 'g^', x2, y2, 'g-')

Edit in response to OP comment

OP wanted to make this extensible to more sets of points, without simply appending them all as arguments to the plot function. Here is one way (altering the animate() function - the rest stays the same)

def animate(i):
    #Make a tuple or list of (x0,y0,c0,x1,y1,c1,x2....)
    newpoints = (matt[i][0],matt[i][1],'r',
                 matt[i][0],matt[i][3],'b',
                 matt[i][2],matt[i][3],'g',
                 matt[i][2],matt[i][1],'y')
    # Use the * operator to expand the tuple / list
    # of (x,y,c) triplets into arguments to pass to the
    # plot function
    animlist = plot(*newpoints,marker='o',markersize=8)
    return animlist
J Richard Snape
  • 20,116
  • 5
  • 51
  • 79
  • Awesome J Richard Snape. That's what i looking for. Thanks for that. Just one more little question. Is there any way to add more "x,y,c" but without extending the line of code where plot method is called? – shinjidev Mar 04 '15 at 21:04
  • Sure - I'll edit a bit more into my answer to show you how to do what I think you want. – J Richard Snape Mar 05 '15 at 10:16
  • Really really thanks J Richard Snape, that was really helpful :) – shinjidev Mar 06 '15 at 15:18