On 3/2/07, Simon Wood <[EMAIL PROTECTED]> wrote: python > Out of the box matplotlib works great with Numeric and numarray data types. > However, I have my own custom class which contains data members, methods and > an array of data (underlying C array). Is there a way to expose the C array > data to the plot() routines? For example I would like to be able to use > plot(X) where X is an instantiated object of type MyClass.
My initial response was if your object supports iteration, it will work with matplotlib But I just tested this and found it to be false. class C: def __init__(self): self._data = (1,2,3,4,5) def __iter__(self): for i in self._data: yield i return def __len__(self): return len(self._data) c = C() for i in c: print i But numpy.asarray, which is what mpl uses to convert inputs to arrays, fails with this object. In [16]: c = C() In [17]: numpy.asarray(c) Out[17]: array(<__main__.C instance at 0x8d04f6c>, dtype=object) because it treats it as an object array (but list(c) works as expected). What is the minimum interface for an object to be converted to a numpy sequence via as array? Setting up your object to work with the numpy asarray conversion facility is probably your best bet pending answers to the question above. But, if your object doesn't expose the proper interface, and for some reason you cannot modify or wrap your objects to expose it, we have a branch of svn to handle plotting of types with unit information, but this branch will also support plotting of arbitrary user types. One can register their type with a set of converter functions, and then do >>> plot(myobj) myobj does not need to expose any particular interface, as long as you have registered a converter with matplotlib -- the converter maps object type to classes that know how to convert your object to. This enable you to use custom objects (usually coming from 3rd part extension code that you do not want to wrap or modify) with matplotlib. Some basic examples are in examples/units and the documentation is in lib/matplotlib/units.py Michael, can you point Simon to a minimal example in the unit's branch that would show him how to use the new units registry for a custom object? Just to be clear, he doesn't need the unit handling, so would just need the default locator and formatter, but he would need the to_array conversion and object type registry. I think this will be an important use case for this code that was not part of the original plan. I think the basic idea is that an important use case will be one where the user doesn't care about units or tagged values or locators and formatters, and we should provide a base converter than encapsulates all this for them, and then all the need to do is derive from this class and register with the figure. It also suggests we may want to rename register_default_unit_conversion to register_default_conversion. The svn branch lives in > svn checkout > https://svn.sourceforge.net/svnroot/matplotlib/branches/unit_support/matplotlib > mplunits JDH ------------------------------------------------------------------------- Take Surveys. Earn Cash. Influence the Future of IT Join SourceForge.net's Techsay panel and you'll get the chance to share your opinions on IT & business topics through brief surveys-and earn cash http://www.techsay.com/default.php?page=join.php&p=sourceforge&CID=DEVDEV _______________________________________________ Matplotlib-users mailing list Matplotlib-users@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/matplotlib-users