Just to add to this issue, I have been profiling my own code which
embeds matplotlib inside a Cocoa app, and uses Agg to draw to an
NSView. (I posted a howto on this recently to the user list). I am
drawing a grayscale image (an array with values 0-255), and then
drawing lines over the top.
I have noticed that most of the time is spend in the ma.asarray(x)
method (specifically the ma.__init__ method) and discounted this as
too hard for me to figure on. But these posts point out that this is
a slow point in the code. Now it is faster for RGB images. I'm
wondering if there is a way to optimise this method for the (fairly
common, I imagine) case of using imshow on grayscale, non-masked
numpy arrays. Can this be done?
Your help is appreciated.
Eric Firing wrote:
> I really blew it on that one--it looked so simple! OK, I think this
> version is correct; it will land in CVS in a few minutes.
> def to_rgba(self, x, alpha=1.0):
> '''Return a normalized rgba array corresponding to x.
> If x is already an rgb or rgba array, return it unchanged.
> if hasattr(x, 'shape') and len(x.shape)>2: return x
> x = ma.asarray(x)
> x = self.norm(x)
> x = self.cmap(x, alpha)
> return x
> Jeff Whitaker wrote:
>> Eric Firing wrote:
>>> You tripped over a bug in cm.py: when I added masked array
>>> support, I
>>> put the "x = ma.asarray(x)" too early in the to_rgba() method of
>>> ScalarMappable. It is fixed now in CVS. Thanks for finding the
>>> If you want to try the fix in your version, in place of your
>>> workaround, here is the revised method:
>>> def to_rgba(self, x, alpha=1.0):
>>> # assume normalized rgb, rgba
>>> if len(x.shape)>2: return x
>>> x = ma.asarray(x)
>>> x = self.norm(x)
>>> x = self.cmap(x, alpha)
>>> return x
>> Hi Eric: This breaks some of the basemap demos which do this
>> CS = m.contour(x,y,hgt,15,linewidths=0.5,colors='k')
>> to draw solid black contour lines. I now get
>> File "/sw/lib/python2.4/site-packages/matplotlib/cm.py", line 52, in
>> if len(x.shape)>2: return x
>> AttributeError: 'list' object has no attribute 'shape'
>> It appears that colors now has to be an array?