LineCollection and alpha

Hi all,

I found a small bug in LineCollection, which gives an exception when setting alpha. This is manifested in e.g. hlines and vlines:

In [1]: hlines((0,),(-1,),(1,),color=‘b’,alpha=0.1)


IndexError Traceback (most recent call last)

XXX/ in ()

XXX/lib/python/matplotlib/pyplot.py in hlines(*args, **kwargs)

1700 hold(h)

1701 try:

→ 1702 ret = gca().hlines(*args, **kwargs)

1703 draw_if_interactive()

1704 except:

XXX/lib/python/matplotlib/axes.py in hlines(self, y, xmin, xmax, colors, linestyles, label, **kwargs)

2609 linestyles=linestyles, label=label)

2610 self.add_collection(coll)

→ 2611 coll.update(kwargs)

2612

2613 minx = min(xmin.min(), xmax.min())

XXX/lib/python/matplotlib/artist.py in update(self, props)

438 if func is None or not callable(func):

439 raise AttributeError(‘Unknown property %s’%k)

→ 440 func(v)

441 changed = True

442 self.eventson = store

XXX/lib/python/matplotlib/collections.py in set_alpha(self, alpha)

306 else:

307 artist.Artist.set_alpha(self, alpha)

→ 308 self._facecolors[:, 3] = alpha

309 self._edgecolors[:, 3] = alpha

310

IndexError: invalid index

This appears to be because LineCollection.init() only uses Collection._edgecolors, and sets _facecolors to an empty array. I don’t know if it’s the proper solution, but I did this to get it to work:

Index: lib/matplotlib/collections.py

···

===================================================================

— lib/matplotlib/collections.py (revision 5000)

+++ lib/matplotlib/collections.py (working copy)

@@ -305,7 +305,8 @@

except TypeError: raise TypeError(‘alpha must be a float’)

else:

artist.Artist.set_alpha(self, alpha)

  •        self._facecolors[:, 3] = alpha
    
  •        if len(self._facecolors):
    
  •            self._facecolors[:, 3] = alpha
    

self._edgecolors[:, 3] = alpha

def get_linewidths(self):

Best,

Mike

That seems like a reasonable fix. Fixed in SVN r5003.

Cheers,
Mike

Michael Fitzgerald wrote:

···

Hi all,

I found a small bug in LineCollection, which gives an exception when setting alpha. This is manifested in e.g. hlines and vlines:

In [1]: hlines((0,),(-1,),(1,),color='b',alpha=0.1)
---------------------------------------------------------------------------
IndexError Traceback (most recent call last)

XXX/<ipython console> in <module>()

XXX/lib/python/matplotlib/pyplot.py in hlines(*args, **kwargs)
   1700 hold(h)
   1701 try:
-> 1702 ret = gca().hlines(*args, **kwargs)
   1703 draw_if_interactive()
   1704 except:

XXX/lib/python/matplotlib/axes.py in hlines(self, y, xmin, xmax, colors, linestyles, label, **kwargs)
   2609 linestyles=linestyles, label=label)
   2610 self.add_collection(coll)
-> 2611 coll.update(kwargs)
   2612
   2613 minx = min(xmin.min(), xmax.min())

XXX/lib/python/matplotlib/artist.py in update(self, props)
    438 if func is None or not callable(func):
    439 raise AttributeError('Unknown property %s'%k)
--> 440 func(v)
    441 changed = True
    442 self.eventson = store

XXX/lib/python/matplotlib/collections.py in set_alpha(self, alpha)
    306 else:
    307 artist.Artist.set_alpha(self, alpha)
--> 308 self._facecolors[:, 3] = alpha
    309 self._edgecolors[:, 3] = alpha
    310

IndexError: invalid index

This appears to be because LineCollection.__init__() only uses Collection._edgecolors, and sets _facecolors to an empty array. I don't know if it's the proper solution, but I did this to get it to work:

Index: lib/matplotlib/collections.py

--- lib/matplotlib/collections.py (revision 5000)
+++ lib/matplotlib/collections.py (working copy)
@@ -305,7 +305,8 @@
         except TypeError: raise TypeError('alpha must be a float')
         else:
             artist.Artist.set_alpha(self, alpha)
- self._facecolors[:, 3] = alpha
+ if len(self._facecolors):
+ self._facecolors[:, 3] = alpha
             self._edgecolors[:, 3] = alpha

     def get_linewidths(self):

Best,
Mike

------------------------------------------------------------------------

-------------------------------------------------------------------------
This SF.net email is sponsored by: Microsoft
Defy all challenges. Microsoft(R) Visual Studio 2008.
http://clk.atdmt.com/MRT/go/vse0120000070mrt/direct/01/
------------------------------------------------------------------------

_______________________________________________
Matplotlib-devel mailing list
Matplotlib-devel@lists.sourceforge.net
matplotlib-devel List Signup and Options