RGBA in imshow

Hi all:

It looks like one can no longer plot an array of RGBA values in imshow - I suspect this is a consequence of the recent changes to the way colors are handled. For example

from pylab import *
# make a random rgba matrix
rgba = ones((64,64,4),'f')
for k in range(3):
   rgba[:,:,k] = rand(64,64).astype('f')
imshow(rgba)
show()

works in 0.87.2, but fails with

Traceback (most recent call last):
  File "/Users/jsw/lib/python/matplotlib/backends/backend_gtk.py", line 283, in expose_event
    self._render_figure(self._pixmap, w, h)
  File "/Users/jsw/lib/python/matplotlib/backends/backend_gtkagg.py", line 72, in _render_figure
    FigureCanvasAgg.draw(self)
  File "/Users/jsw/lib/python/matplotlib/backends/backend_agg.py", line 389, in draw
    self.figure.draw(renderer)
  File "/Users/jsw/lib/python/matplotlib/figure.py", line 531, in draw
    for a in self.axes: a.draw(renderer)
  File "/Users/jsw/lib/python/matplotlib/axes.py", line 991, in draw
    im.draw(renderer)
  File "/Users/jsw/lib/python/matplotlib/image.py", line 184, in draw
    im = self.make_image()
  File "/Users/jsw/lib/python/matplotlib/image.py", line 130, in make_image
    im.is_grayscale = (self.cmap.is_gray() and
  File "/Users/jsw/lib/python/matplotlib/colors.py", line 634, in is_gray
    return (alltrue(self._lut[:,0] == self._lut[:,1])

with the latest svn.

This patch to image.py seems to fix it, but I'm not sure it's the right solution

--- /Users/jsw/lib/python/matplotlib/image.py.orig 2006-06-02 17:04:52.000000000 -0600
+++ /Users/jsw/lib/python/matplotlib/image.py 2006-06-02 17:05:24.000000000 -0600
@@ -127,8 +127,10 @@
             im.flipud_in()
          im.set_bg( *bg)
- im.is_grayscale = (self.cmap.is_gray() and
- len(self._A.shape) == 2)
+ if len(self._A.shape) == 2:
+ im.is_grayscale = self.cmap.is_gray()
+ else:
+ im.is_grayscale = False
          im.set_interpolation(self._interpd[self._interpolation])

-Jeff

···

--
Jeffrey S. Whitaker Phone : (303)497-6313
Meteorologist FAX : (303)497-6449
NOAA/OAR/PSD R/PSD1 Email : Jeffrey.S.Whitaker@...236...
325 Broadway Office : Skaggs Research Cntr 1D-124
Boulder, CO, USA 80303-3328 Web : http://tinyurl.com/5telg

Jeff,

Thanks for the report. I have committed a bugfix combined with a speedup: the check for gray will not occur if the image is cached.

Eric

···

----- Original Message -----
From: Jeff Whitaker <jswhit@...196...>
Date: Friday, June 2, 2006 1:07 pm
Subject: [matplotlib-devel] RGBA in imshow
To: matplotlib development list <matplotlib-devel@lists.sourceforge.net>

Hi all:

It looks like one can no longer plot an array of RGBA values in
imshow -
I suspect this is a consequence of the recent changes to the way
colors
are handled. For example

from pylab import *
# make a random rgba matrix
rgba = ones((64,64,4),'f')
for k in range(3):
  rgba[:,:,k] = rand(64,64).astype('f')
imshow(rgba)
show()

works in 0.87.2, but fails with

Traceback (most recent call last):
File "/Users/jsw/lib/python/matplotlib/backends/backend_gtk.py",
line
283, in expose_event
   self._render_figure(self._pixmap, w, h)
File
"/Users/jsw/lib/python/matplotlib/backends/backend_gtkagg.py",
line 72, in _render_figure
   FigureCanvasAgg.draw(self)
File "/Users/jsw/lib/python/matplotlib/backends/backend_agg.py",
line
389, in draw
   self.figure.draw(renderer)
File "/Users/jsw/lib/python/matplotlib/figure.py", line 531, in draw
   for a in self.axes: a.draw(renderer)
File "/Users/jsw/lib/python/matplotlib/axes.py", line 991, in draw
   im.draw(renderer)
File "/Users/jsw/lib/python/matplotlib/image.py", line 184, in draw
   im = self.make_image()
File "/Users/jsw/lib/python/matplotlib/image.py", line 130, in
make_image im.is_grayscale = (self.cmap.is_gray() and
File "/Users/jsw/lib/python/matplotlib/colors.py", line 634, in
is_gray return (alltrue(self._lut[:,0] == self._lut[:,1])

with the latest svn.

This patch to image.py seems to fix it, but I'm not sure it's the
right
solution

--- /Users/jsw/lib/python/matplotlib/image.py.orig 2006-06-02
17:04:52.000000000 -0600
+++ /Users/jsw/lib/python/matplotlib/image.py 2006-06-02
17:05:24.000000000 -0600
@@ -127,8 +127,10 @@
            im.flipud_in()

        im.set_bg( *bg)
- im.is_grayscale = (self.cmap.is_gray() and
- len(self._A.shape) == 2)
+ if len(self._A.shape) == 2:
+ im.is_grayscale = self.cmap.is_gray()
+ else:
+ im.is_grayscale = False

        im.set_interpolation(self._interpd[self._interpolation])

-Jeff

--
Jeffrey S. Whitaker Phone : (303)497-6313
Meteorologist FAX : (303)497-6449
NOAA/OAR/PSD R/PSD1 Email : Jeffrey.S.Whitaker@...236...
325 Broadway Office : Skaggs Research Cntr 1D-124
Boulder, CO, USA 80303-3328 Web : http://tinyurl.com/5telg

_______________________________________________
Matplotlib-devel mailing list
Matplotlib-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/matplotlib-devel

Eric Firing wrote:

Jeff,

Thanks for the report. I have committed a bugfix combined with a speedup: the check for gray will not occur if the image is cached.

Eric

Thanks Eric - that works fine.

-Jeff

···

--
Jeffrey S. Whitaker Phone : (303)497-6313
NOAA/OAR/CDC R/PSD1 FAX : (303)497-6449
325 Broadway Boulder, CO, USA 80305-3328