Learn how easy it is to sync an existing GitHub or Google Code repo to a SourceForge project!

## Re: [Matplotlib-users] From list of objects to plotting

 Re: [Matplotlib-users] From list of objects to plotting From: John Hunter - 2007-05-07 14:21:39 ```On 5/6/07, Jouni K. Sepp=E4nen wrote: > Tommy Grav writes: > > > I would now like to plot a vs e for all the obj objects in nlist. > > how do I do that? I tried > > > > plot(nlist[:].a,nlist[:].e,'ko') > > You have a list of objects that have attributes named a and e; these > are not attributes of the list. Try > > plot([x.a for x in nlist], [x.e for x in nlist], 'ko') Another idiom which I use a lot a,e =3D zip(*[(o.a, o.e) for o in nlist]) or if I have a lot of attributes I want dumped into arrays a,b,c,d =3D map(numpy.asarray, zip(*[(o.a, o.b, o.c, o.d) for o in data])= ) JDH ```

 [Matplotlib-users] From list of objects to plotting From: Tommy Grav - 2007-05-04 15:55:01 ```I have some code that simplified looks like this: class cKBO(object): def __init__(cls): cls.a = 0. cls.e = 0. lines = open("test.file","r").readlines() nlist = [] for line in lines: obj = cKBO() (a,e) = line.split obj.a = float(a) obj.e = float(e) nlist.append(obj) I would now like to plot a vs e for all the obj objects in nlist. how do I do that? I tried plot(nlist[:].a,nlist[:].e,'ko') show() but that resulted in raceback (most recent call last): File "/Users/tgrav/Work/MyCode/Python/MOPS/ckbos.py", line 108, in ? plot(nlist[:].a,nlist[:].e,'ko') AttributeError: 'list' object has no attribute 'a' Which is understandable. But how do I most easily convert a list of class objects into two lists of object variables? Cheers Tommy ```
 Re: [Matplotlib-users] From list of objects to plotting From: Matthias Michler - 2007-05-06 07:25:20 ```Hello Tommy, I would like to do such convertings, too, but I think there is no such fast way in python. I'm using the 'for statement' to iterate over a list of objects and save their properties into a new list. best regards, Matthias On Friday 04 May 2007 17:54, Tommy Grav wrote: > I have some code that simplified looks like this: > > class cKBO(object): > def __init__(cls): > cls.a = 0. > cls.e = 0. > > lines = open("test.file","r").readlines() > > nlist = [] > for line in lines: > obj = cKBO() > (a,e) = line.split > obj.a = float(a) > obj.e = float(e) > nlist.append(obj) > > I would now like to plot a vs e for all the obj objects in nlist. > how do I do that? I tried > > plot(nlist[:].a,nlist[:].e,'ko') > show() > > but that resulted in > raceback (most recent call last): > File "/Users/tgrav/Work/MyCode/Python/MOPS/ckbos.py", line 108, in ? > plot(nlist[:].a,nlist[:].e,'ko') > AttributeError: 'list' object has no attribute 'a' > > Which is understandable. But how do I most easily convert a list of > class > objects into two lists of object variables? > > Cheers > Tommy > > > > ------------------------------------------------------------------------- > This SF.net email is sponsored by DB2 Express > Download DB2 Express C - the FREE version of DB2 express and take > control of your XML. No limits. Just data. Click to get it now. > http://sourceforge.net/powerbar/db2/ > _______________________________________________ > Matplotlib-users mailing list > Matplotlib-users@... > https://lists.sourceforge.net/lists/listinfo/matplotlib-users ```
 Re: [Matplotlib-users] From list of objects to plotting From: - 2007-05-06 12:39:20 ```Tommy Grav writes: > I would now like to plot a vs e for all the obj objects in nlist. > how do I do that? I tried > > plot(nlist[:].a,nlist[:].e,'ko') You have a list of objects that have attributes named a and e; these are not attributes of the list. Try plot([x.a for x in nlist], [x.e for x in nlist], 'ko') -- Jouni K. Seppänen http://www.iki.fi/jks ```
 Re: [Matplotlib-users] From list of objects to plotting From: John Hunter - 2007-05-07 14:21:39 ```On 5/6/07, Jouni K. Sepp=E4nen wrote: > Tommy Grav writes: > > > I would now like to plot a vs e for all the obj objects in nlist. > > how do I do that? I tried > > > > plot(nlist[:].a,nlist[:].e,'ko') > > You have a list of objects that have attributes named a and e; these > are not attributes of the list. Try > > plot([x.a for x in nlist], [x.e for x in nlist], 'ko') Another idiom which I use a lot a,e =3D zip(*[(o.a, o.e) for o in nlist]) or if I have a lot of attributes I want dumped into arrays a,b,c,d =3D map(numpy.asarray, zip(*[(o.a, o.b, o.c, o.d) for o in data])= ) JDH ```