On Wed, Sep 21, 2011 at 9:28 PM, Benjamin Root <ben.root@ou.edu> wrote:
On Wed, Sep 21, 2011 at 2:00 PM, Armando Serrano Lombillo <arserlom@gmail.com> wrote:
Hello I have a dat set like this one
a=[[x1, y1, cat1], [x2, y2, cat1], ..., [x8, y8, cat1], [x9, y9, cat2], ..., [x34, y34, cat2], [x35, y35, cat3],...]
and I don't know beforehand how many diffferent categories there will be or how long they will be.

I would like to make a plot like this:
ax.plot(a[0:i1, 0], a[0:i1, 1], label=cat1)
ax.plot(a[i1:i2, 0], a[i1:i2, 1], label=cat2)
ax.plot(a[i2:i3, 0], a[i2:i3, 1], label=cat3)

where i1, i2... are the indices where the data set changes fomt cat1 to cat2, cat2 to cat3...

Does anybody see an easy way of coding this?


Maybe something like this? (Warning: untested!)

from collections import OrderedDict
b = OrderedDict()
for index, coords in enumerate(a) :
    b[coords[2]] = index + 1

a = np.array([[c[0], c[1]] for c in a])

prevIndex = 0
for cat, curIndex in b.iteritems() :
    ax.plot(a[prevIndex:curIndex, 0], a[prevIndex:curIndex, 1], label=cat)
    prevIndex = curIndex

The ordered dictionary may or may not be available in whatever version of python you use, but it guarantees the order.

I hope that helps!
Ben Root

Ok, thanks Ben, I was thinking that maybe with some numpy array manipulation there might have been a straightforward way I wasn't seeing.