Plot data from custom class

John asked:
> What is the minimum interface for an object to be
> converted to a numpy sequence via as array?

The class must inherit from object.
That will probably do it.
If all else fails, try fromiter.

Alan Isaac

I know it works with fromiter, but I am trying to find a way mpl users
can create objects that will work directly in mpl, which uses asarray.
Thanks for the object suggestion; here is the minimal interface that
appears to work

class C(object):
    def __init__(self):
        self._data = (1,2,3,4,5)

    def __getitem__(self, i):
        return self._data[i]

    def __len__(self):
        return len(self._data)

import numpy
c = C()
print numpy.asarray(c)

···

On 3/2/07, Alan Isaac <aisaac@...310...> wrote:

John asked:
> What is the minimum interface for an object to be
> converted to a numpy sequence via as array?

The class must inherit from object.
That will probably do it.
If all else fails, try fromiter.

To all,
Thanks for the help. I had the other member functions implemented and I simply needed to add the getitem member function.

BTW: I am actually using Boost.Python to expose my C++ library to Python. I was able to add the getitem member function to my class and voila…things worked nicely (I already had the len member function).

···

On 3/2/07, John Hunter <jdh2358@…287…> wrote:

John said:
…here is the minimal interface that
appears to work

class C(object):
def init(self):
self._data = (1,2,3,4,5)

def __getitem__(self, i):
    return self._data[i]


def __len__(self):
    return len(self._data)

import numpy
c = C()
print numpy.asarray(c)