[Matplotlib-users] plotting an array of enumerated values

John Hunter wrote:

"Eric" == Eric Firing <efiring@...229...> writes:

    > John Hunter wrote:

    >>
    > they will not be transparent. If you need transparent
    > masked regions, then try pcolor instead of imshow. Pcolor
    > plots nothing at all in masked cells. Pcolormesh, on the
    > other hand, is like imshow in plotting the assigned bad
    > color and in using a single alpha for everything.
    >> I'm confused about the comments about alpha not working on
    >> imshow -- can you elaborate. On the agg backend at least, the
    >> alpha channel is respected in imshow, eg
    >> examples/layer_images.py. Is there a reason it does not (or
    >> should not) work in the masked example?

    > John,

    > I don't know why it doesn't work; I know only that in my
    > example, it doesn't work as I perhaps naively think it
    > should. My interpretation of alpha is that if alpha is zero
    > in any colored region, and if nothing else is drawn on top,
    > then the background should show through; that is, the r,g,b
    > values in the r,g,b,a tuple for a region should have no
    > effect if a is zero. If you uncomment
    > #cmap.set_bad((1,1,1,0) in my example, you will find that
    > the masked region is white; and if you change the rgb part
    > of that tuple, it takes on that color, regardless of alpha.

My confusion here was that cmap.set_bad() ignores the alpha part of the color spec; you have to set alpha explicitly with a kwarg. Maybe I will fix this some time. But, read on...

I'm not sure what is going on in your example, but this test case
shows that the alpha channel is respected. I made a red RGBA array
and set the alpha channel for the center to be transparent and it
behaves as expected: you can see the line through the transparent
region of the rectangle and the axes bgcolor shows through. I had to
make a small change to svn to make this work because the image wasn't
respecting the zorder (revision 2495). So the bug you are
experiencing is likely to be in the front-end code.

You are partly right; alpha works fine with imshow. But there is a problem in a backend, specifically the agg draw_quad_mesh and/or DrawQaudMesh methods. This is where I originally ran into the problem, and the attached script illustrates it. The figure on-screen has erratic and incorrect behavior in the central rectangle of the second subplot, for which alpha is zero. Note what happens when you resize the window. The ps backend, using the backend_bases code for the quad mesh, works correctly, now that I fixed it--two of the vertices in each quad were originally reversed.

It seems that DrawQuadMesh is trying to go straight to a low-level agg function, and in the process something is being lost when alpha is zero, or any value less than 1; in the example, you can change the alpha to 0.5, for example, and you will still see the behavior when you resize the window.

I'm stumped.

Eric

alpha_test.py (1.01 KB)