Next problem: pixel-to-pixel alpha variation

Hi, folks. OK, I'm trying to set the alpha channel, pixel by pixel, using figimage w/ the data being of the "luminance" type (i.e., an MxN array). The Users Guide indicates that figimage takes an alpha= keyword argument, and it doesn't crash when I pass an array for this value, but subsequently when I try to draw it using fig.draw(canvas.get_renderer()), I get:

fig.draw(canvas.get_renderer())

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\python25\lib\site-packages\matplotlib\figure.py", line 607, in draw
    im.draw(renderer)
  File "C:\python25\lib\site-packages\matplotlib\image.py", line 597, in draw
    im = self.make_image()
  File "C:\python25\lib\site-packages\matplotlib\image.py", line 583, in make_im
age
    x = self.to_rgba(self._A, self._alpha)
  File "C:\python25\lib\site-packages\matplotlib\cm.py", line 76, in to_rgba
    x = self.cmap(x, alpha=alpha, bytes=bytes)
  File "C:\python25\lib\site-packages\matplotlib\colors.py", line 423, in __call

···

__
    alpha = min(alpha, 1.0) # alpha must be between 0 and 1
ValueError: The truth value of an array with more than one element is ambiguous.
Use a.any() or a.all()

which to me "smells" as if the array-valued alpha is the problem.

Clearly (?) I can do what I'm after if I use MxNx4 data, but is that the only way to have a varying alpha?

Thanks!

DG

Yep, currently that is the only way. If you start with luminance, you
can use the colormapping code to generate RGB, and then attach an
alpha mask for RGBA and pass that to figimage or imshow.

JDH

···

On Tue, Sep 9, 2008 at 12:40 AM, David Goldsmith <d_l_goldsmith@...9...> wrote:

Clearly (?) I can do what I'm after if I use MxNx4 data, but is that the only way to have a varying alpha?

David Goldsmith wrote:

Hi, folks. OK, I'm trying to set the alpha channel, pixel by pixel, using figimage w/ the data being of the "luminance" type (i.e., an MxN array). The Users Guide indicates that figimage takes an alpha= keyword argument, and it doesn't crash when I pass an array for this value, but subsequently when I try to draw it using fig.draw(canvas.get_renderer()), I get:

fig.draw(canvas.get_renderer())
        

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\python25\lib\site-packages\matplotlib\figure.py", line 607, in draw
    im.draw(renderer)
  File "C:\python25\lib\site-packages\matplotlib\image.py", line 597, in draw
    im = self.make_image()
  File "C:\python25\lib\site-packages\matplotlib\image.py", line 583, in make_im
age
    x = self.to_rgba(self._A, self._alpha)
  File "C:\python25\lib\site-packages\matplotlib\cm.py", line 76, in to_rgba
    x = self.cmap(x, alpha=alpha, bytes=bytes)
  File "C:\python25\lib\site-packages\matplotlib\colors.py", line 423, in __call
__
    alpha = min(alpha, 1.0) # alpha must be between 0 and 1
ValueError: The truth value of an array with more than one element is ambiguous.
Use a.any() or a.all()

which to me "smells" as if the array-valued alpha is the problem.

Clearly (?) I can do what I'm after if I use MxNx4 data, but is that the only way to have a varying alpha?
  

The alpha parameter always takes only a single (global) value, and the only way to do pixel-by-pixel alpha is an MxNx4 array. It should be fairly straightforward to create this array by concatenating together three copies of the luminance and one copy of your alpha, though.

Something like:

  # lum is MxN, alpha is MxN
  lum = lum.reshape((M, N, 1))
  alpha = alpha.reshape((M, N, 1))
  rgba = numpy.concatenate((lum, lum, lum, alpha))

(There might be an even more straightforward way --- I'm not much of a numpy expert...)

Cheers
Mike

···

--
Michael Droettboom
Science Software Branch
Operations and Engineering Division
Space Telescope Science Institute
Operated by AURA for NASA

Thanks, John and Michael.

Yeah, Michael, that's what I'm doing now, but eventually (like, now) I'd like to do what John implies is possible, i.e., "invert" a cm back to its RGB table - John (or anyone) can you short-cut the learning process for me w/ a code example of how to do this? :slight_smile: Thanks!

DG

···

--- On Tue, 9/9/08, Michael Droettboom <mdroe@...86...> wrote:

From: Michael Droettboom <mdroe@...86...>
Subject: Re: [Matplotlib-users] Next problem: pixel-to-pixel alpha variation
To: d_l_goldsmith@...9...
Cc: matplotlib-users@lists.sourceforge.net
Date: Tuesday, September 9, 2008, 7:22 AM
David Goldsmith wrote:
> Hi, folks. OK, I'm trying to set the alpha
channel, pixel by pixel, using figimage w/ the data being of
the "luminance" type (i.e., an MxN array). The
Users Guide indicates that figimage takes an alpha= keyword
argument, and it doesn't crash when I pass an array for
this value, but subsequently when I try to draw it using
fig.draw(canvas.get_renderer()), I get:
>
>
>>>> fig.draw(canvas.get_renderer())
>>>>
> Traceback (most recent call last):
> File "<stdin>", line 1, in
<module>
> File
"C:\python25\lib\site-packages\matplotlib\figure.py",
line 607, in draw
> im.draw(renderer)
> File
"C:\python25\lib\site-packages\matplotlib\image.py",
line 597, in draw
> im = self.make_image()
> File
"C:\python25\lib\site-packages\matplotlib\image.py",
line 583, in make_im
> age
> x = self.to_rgba(self._A, self._alpha)
> File
"C:\python25\lib\site-packages\matplotlib\cm.py",
line 76, in to_rgba
> x = self.cmap(x, alpha=alpha, bytes=bytes)
> File
"C:\python25\lib\site-packages\matplotlib\colors.py",
line 423, in __call
> __
> alpha = min(alpha, 1.0) # alpha must be between 0
and 1
> ValueError: The truth value of an array with more than
one element is ambiguous.
> Use a.any() or a.all()
>
> which to me "smells" as if the array-valued
alpha is the problem.
>
> Clearly (?) I can do what I'm after if I use MxNx4
data, but is that the only way to have a varying alpha?
>
The alpha parameter always takes only a single (global)
value, and the
only way to do pixel-by-pixel alpha is an MxNx4 array. It
should be
fairly straightforward to create this array by
concatenating together
three copies of the luminance and one copy of your alpha,
though.

Something like:

  # lum is MxN, alpha is MxN
  lum = lum.reshape((M, N, 1))
  alpha = alpha.reshape((M, N, 1))
  rgba = numpy.concatenate((lum, lum, lum, alpha))

(There might be an even more straightforward way ---
I'm not much of a
numpy expert...)

Cheers
Mike

--
Michael Droettboom
Science Software Branch
Operations and Engineering Division
Space Telescope Science Institute
Operated by AURA for NASA

Well, I've figured out half of it: the RGB data appears to be in <cmap>._segmentdata, an RGB-keyed dictionary of tuples of triples, but, looking at this for the jet cmap, e.g., I'm confused, 'cause the red and and blue tuples have five such triples, but the green tuple has six triples, so how exactly are these remapped to an RGB array? Thanks!

DG

···

--- On Tue, 9/9/08, David Goldsmith <d_l_goldsmith@...9...> wrote:

From: David Goldsmith <d_l_goldsmith@...9...>
Subject: Re: [Matplotlib-users] Next problem: pixel-to-pixel alpha variation
To: matplotlib-users@lists.sourceforge.net
Date: Tuesday, September 9, 2008, 9:15 AM
Thanks, John and Michael.

Yeah, Michael, that's what I'm doing now, but
eventually (like, now) I'd like to do what John implies
is possible, i.e., "invert" a cm back to its RGB
table - John (or anyone) can you short-cut the learning
process for me w/ a code example of how to do this? :slight_smile:
Thanks!

DG
--- On Tue, 9/9/08, Michael Droettboom > <mdroe@...86...> wrote:

> From: Michael Droettboom <mdroe@...86...>
> Subject: Re: [Matplotlib-users] Next problem:
pixel-to-pixel alpha variation
> To: d_l_goldsmith@...9...
> Cc: matplotlib-users@lists.sourceforge.net
> Date: Tuesday, September 9, 2008, 7:22 AM
> David Goldsmith wrote:
> > Hi, folks. OK, I'm trying to set the alpha
> channel, pixel by pixel, using figimage w/ the data
being of
> the "luminance" type (i.e., an MxN array).
The
> Users Guide indicates that figimage takes an alpha=
keyword
> argument, and it doesn't crash when I pass an
array for
> this value, but subsequently when I try to draw it
using
> fig.draw(canvas.get_renderer()), I get:
> >
> >
> >>>> fig.draw(canvas.get_renderer())
> >>>>
> > Traceback (most recent call last):
> > File "<stdin>", line 1, in
> <module>
> > File
>
"C:\python25\lib\site-packages\matplotlib\figure.py",
> line 607, in draw
> > im.draw(renderer)
> > File
>
"C:\python25\lib\site-packages\matplotlib\image.py",
> line 597, in draw
> > im = self.make_image()
> > File
>
"C:\python25\lib\site-packages\matplotlib\image.py",
> line 583, in make_im
> > age
> > x = self.to_rgba(self._A, self._alpha)
> > File
>
"C:\python25\lib\site-packages\matplotlib\cm.py",
> line 76, in to_rgba
> > x = self.cmap(x, alpha=alpha, bytes=bytes)
> > File
>
"C:\python25\lib\site-packages\matplotlib\colors.py",
> line 423, in __call
> > __
> > alpha = min(alpha, 1.0) # alpha must be
between 0
> and 1
> > ValueError: The truth value of an array with more
than
> one element is ambiguous.
> > Use a.any() or a.all()
> >
> > which to me "smells" as if the
array-valued
> alpha is the problem.
> >
> > Clearly (?) I can do what I'm after if I use
MxNx4
> data, but is that the only way to have a varying
alpha?
> >
> The alpha parameter always takes only a single
(global)
> value, and the
> only way to do pixel-by-pixel alpha is an MxNx4 array.
It
> should be
> fairly straightforward to create this array by
> concatenating together
> three copies of the luminance and one copy of your
alpha,
> though.
>
> Something like:
>
> # lum is MxN, alpha is MxN
> lum = lum.reshape((M, N, 1))
> alpha = alpha.reshape((M, N, 1))
> rgba = numpy.concatenate((lum, lum, lum, alpha))
>
> (There might be an even more straightforward way ---
> I'm not much of a
> numpy expert...)
>
> Cheers
> Mike
>
> --
> Michael Droettboom
> Science Software Branch
> Operations and Engineering Division
> Space Telescope Science Institute
> Operated by AURA for NASA

-------------------------------------------------------------------------
This SF.Net email is sponsored by the Moblin Your Move
Developer's challenge
Build the coolest Linux based applications with Moblin SDK
& win great prizes
Grand prize is a trip for two to an Open Source event
anywhere in the world
http://moblin-contest.org/redirect.php?banner_id=100&url=/
_______________________________________________
Matplotlib-users mailing list
Matplotlib-users@lists.sourceforge.net
matplotlib-users List Signup and Options