Artifacts In Transparent Contour Plots?

I am making contourf plots with some transparency as I want to
eventually overlay such plots. When I plot such plots I see artifacts
or lines that shouldn't be there as seen here:
http://josephsmidt.googlepages.com/ex.png

    Is this a bug or am I plotting incorrectly? My script is here:
http://josephsmidt.googlepages.com/plotex.py and the data needed for
the plot is here: http://josephsmidt.googlepages.com/exCHI.txt

However, the relevant part of the code for plotting I'll just cut and
paste here:

levels = [x0, x1, x2, x3]
contourf(Y,X,transpose(CHI),levels,alpha=0.7)
show()

      Thanks.

                       Joseph Smidt

···

--
------------------------------------------------------------------------
Joseph Smidt <josephsmidt@...287...>

Physics and Astronomy
4129 Frederick Reines Hall
Irvine, CA 92697-4575
Office: 949-824-3269

This is a long-standing known issue with the contour code. The contouring library we are using (in cntr.c) was written for a graphics library that did not support compound paths, so to draw a donut-shaped objects it creates "cut paths" from the inner to outer edge. A number of the matplotlib developers have taken stabs at changing this behavior over the years, but it is a very opaque chunk of code, and none of us have managed it without introducing new more serious errors. A fresh set of eyes may be able to sort it out, and we do have compound path support in all backends (save the mostly obsolete Gdk) now. Replacing the contouring code with something else that is license-compatible is also an option, but I wasn't able to find anything suitable last time I looked (which was a long time ago now).

Cheers,
Mike

Joseph Smidt wrote:

···

I am making contourf plots with some transparency as I want to
eventually overlay such plots. When I plot such plots I see artifacts
or lines that shouldn't be there as seen here:
http://josephsmidt.googlepages.com/ex.png

    Is this a bug or am I plotting incorrectly? My script is here:
http://josephsmidt.googlepages.com/plotex.py and the data needed for
the plot is here: http://josephsmidt.googlepages.com/exCHI.txt

However, the relevant part of the code for plotting I'll just cut and
paste here:

levels = [x0, x1, x2, x3]
contourf(Y,X,transpose(CHI),levels,alpha=0.7)
show()

      Thanks.

                       Joseph Smidt

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

Joseph Smidt wrote:

I am making contourf plots with some transparency as I want to
eventually overlay such plots. When I plot such plots I see artifacts
or lines that shouldn't be there as seen here:
http://josephsmidt.googlepages.com/ex.png

    Is this a bug or am I plotting incorrectly? My script is here:
http://josephsmidt.googlepages.com/plotex.py and the data needed for
the plot is here: http://josephsmidt.googlepages.com/exCHI.txt

However, the relevant part of the code for plotting I'll just cut and
paste here:

levels = [x0, x1, x2, x3]
contourf(Y,X,transpose(CHI),levels,alpha=0.7)
show()

      Thanks.

                       Joseph Smidt

This is a problem with two parts: first, the filled-contour algorithm creates singly-connected patches, so cuts are required (the horizontal lines); second, depending on whether edges are stroked and on the antialias setting, these cuts, and the boundaries between regions, can show up as artifacts when rendered. The rendering problem can be backend-dependent.

When I make a contourf plot using the agg backend (which is presumably what you are using to make the png file--either directly or indirectly via an interactive backend), I am not seeing the artifacts, so I think the default settings in svn are good for agg rendering with alpha. My guess is that you are using a version of mpl with different settings. We have struggled with this problem, trying different combinations of settings, for a long time. There doesn't seem to be a formula that works for all backends and all alpha values.

I suspect that in the version of mpl you are using, the patch edges are being stroked. As a workaround, try this:

cs = contourf(Y,X,transpose(CHI),levels,alpha=0.7)
for c in cs.collections:
     c.set_edgecolors(['none'])

Eric

Thanks a lot. The code you mentioned:

cs = contourf(Y,X,transpose(CHI),levels,alpha=0.7)
for c in cs.collections:
   c.set_edgecolors('none')

    worked great and the extra lines are gone.

                 Joseph Smidt

PS. Good luck with this issue. I'd help but I'm not that good.

···

--
------------------------------------------------------------------------
Joseph Smidt <josephsmidt@...287...>

Physics and Astronomy
4129 Frederick Reines Hall
Irvine, CA 92697-4575
Office: 949-824-3269

Michael Droettboom wrote:

This is a long-standing known issue with the contour code. The contouring library we are using (in cntr.c) was written for a graphics library that did not support compound paths, so to draw a donut-shaped objects it creates "cut paths" from the inner to outer edge. A number of the matplotlib developers have taken stabs at changing this behavior over the years, but it is a very opaque chunk of code, and none of us have managed it without introducing new more serious errors. A fresh set of eyes may be able to sort it out, and we do have compound path support in all backends (save the mostly obsolete Gdk) now. Replacing the contouring code with something else that is license-compatible is also an option, but I wasn't able to find anything suitable last time I looked (which was a long time ago now).

I have been looking about every 6 months for years, with absolutely no success. There are remarkably few filled-contouring codes (or detailed algorithm descriptions) out there, under any license. Note also that the code Matlab uses (or at least used a long time ago when I last looked) handles the problem by layering the contour patches, not by leaving holes, so it is an example of a class of algorithms that would not work with non-unit alpha. I have also thought a bit about manipulating the paths generated by the present code to remove the cuts, or manipulating the non-filled contour output to generate patch boundaries, but I haven't gone into it deeply. I suspect it would quickly become very complicated. And, on top of all that, even if we solved the cut problem, there would still be backend-dependent artifacts involved in rendering the boundaries.

Eric

···

Cheers,
Mike

Joseph Smidt wrote:

I am making contourf plots with some transparency as I want to
eventually overlay such plots. When I plot such plots I see artifacts
or lines that shouldn't be there as seen here:
http://josephsmidt.googlepages.com/ex.png

    Is this a bug or am I plotting incorrectly? My script is here:
http://josephsmidt.googlepages.com/plotex.py and the data needed for
the plot is here: http://josephsmidt.googlepages.com/exCHI.txt

However, the relevant part of the code for plotting I'll just cut and
paste here:

levels = [x0, x1, x2, x3]
contourf(Y,X,transpose(CHI),levels,alpha=0.7)
show()

      Thanks.

                       Joseph Smidt