Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Transparent ContourPlot

712 views
Skip to first unread message

dg

unread,
Dec 16, 2011, 6:05:54 AM12/16/11
to
I have been trying to create a contour plot where the contour shading is transparent, so that I can draw gridlines and see them through the contours. I can't seem to figure this one out....

This produces a mess:

ContourPlot[x y^2 + 2, {x, -2, 2}, {y, -2, 2}, PlotRange -> {-10, 10},
ContourShading -> {Orange, Opacity[0.5]}]

Bob Hanlon

unread,
Dec 17, 2011, 2:41:56 AM12/17/11
to
Perhaps you want Mesh

ContourPlot[x y^2 + 2, {x, -2, 2}, {y, -2, 2},
PlotRange -> {-10, 10},
Mesh -> 7,
MeshFunctions -> {#1 &, #2 &},
Axes -> False,
ColorFunction -> (Lighter[Orange, #] &)]


Bob Hanlon
--
Bob Hanlon

Armand Tamzarian

unread,
Dec 17, 2011, 2:50:39 AM12/17/11
to
I'm guessing this might be what you want:

ContourPlot[x y^2 + 2, {x, -2, 2}, {y, -2, 2}, PlotRange -> {-10, 10},
ContourShading -> {{Orange, Opacity[0.5]}}]

{Orange, Opacity[0.5]} are in a list to indicate they apply together.

Mike

Heike Gramberg

unread,
Dec 18, 2011, 4:34:53 AM12/18/11
to
To draw grid lines on top of the plot, you could use the (undocumented) option
Method->{"GridLinesInFront"->True}, so you could do something like

ContourPlot[x y^2 + 2, {x, -2, 2}, {y, -2, 2},
PlotRange -> {-10, 10},
Method -> {"GridLinesInFront" -> True},
GridLines -> Automatic]

If you still want to have a transparent shading, you probably want to set
Method -> {"TransparentPolygonMesh" -> True} as well which will get rid of the edges of
the individual polygons. You can combine these two options as follows

ContourPlot[x y^2 + 2, {x, -2, 2}, {y, -2, 2},
PlotRange -> {-10, 10},
Method -> {"GridLinesInFront" -> True, "TransparentPolygonMesh" -> True},
ColorFunction -> (Directive[Opacity[.5], ColorData["Rainbow"][#]] &),
ContourShading -> Automatic,
GridLines -> Automatic]

Heike

On 16 Dec 2011, at 11:52, dg wrote:

> I have been trying to create a contour plot where the contour shading is transparent, so that I can draw gridlines and see them through the contours. I can't seem to figure this one out....

JUN

unread,
Dec 19, 2011, 7:17:52 AM12/19/11
to
Hi,
at first I thought Heike's solution with "TransparentPolygonMesh" ->
True was going to make my day. But unfortunately, when I export the
plot as PDF on Mac, it still shows the ugly mesh lines. So instead of
this solution, I modified another approach that I had settled on:

rasterContourPlot[f_, rx_, ry_, opts : OptionsPattern[]] :=
Module[{img, cont, plotRangeRule, contourOptions, frameOptions},
contourOptions =
Join[FilterRules[{opts},
FilterRules[Options[ContourPlot],
Except[{Background, PlotRangePadding, Frame,
Axes}]]], {PlotRangePadding -> None, Frame -> None,
Axes -> None}];
cont = ContourPlot[f, rx, ry,
Evaluate@Apply[Sequence, contourOptions]];
img = Rasterize[Graphics[
GraphicsComplex[cont[[1, 1]], cont[[1, 2, 1]]],
PlotRangePadding -> None, ImagePadding -> None],
ImageSize -> 2 ((ImageSize /. {opts}) /. {ImageSize ->
CurrentValue[ImageSize]})];
plotRangeRule = FilterRules[AbsoluteOptions[cont], PlotRange];
frameOptions = Join[FilterRules[{opts},
FilterRules[Options[Graphics],
Except[{PlotRangeClipping, PlotRange}]]],
{plotRangeRule, Frame -> True, PlotRangeClipping -> True}];
Show[Graphics[{{FaceForm[], EdgeForm[],
Apply[Rectangle, Transpose[PlotRange /. plotRangeRule]]},
Inset[Show[SetAlphaChannel[img, "ShadingOpacity" /. {opts}
/. {"ShadingOpacity" -> 1}], AspectRatio -> Full],
Scaled[{0, 0}], Scaled[{0, 0}], Scaled[{1, 1}]]},
PlotRangePadding -> None],
Graphics[GraphicsComplex[cont[[1, 1]], cont[[1, 2, 2]]],
PlotRangePadding -> None, ImagePadding -> None],
Evaluate@Apply[Sequence, frameOptions]]
]

Now I'm using this modified contour plot function with grid lines and
a prolog that display a blue disk behind the plot:

rasterContourPlot[x y^2 + 2, {x, -2, 2}, {y, -2, 2},
PlotRange -> {-10, 10}, GridLines -> Automatic, Contours -> 15,
"ShadingOpacity" -> .5, ImageSize -> 600,
Prolog -> {Blue, Disk[{1, 1.5}, 1]}]

The function has one new option that the standard contour plot doesn't
have: "ShadingOpacity" - that's what determines the opacity of the
contour shading. To implement this opacity, I convert the shading to
an image and then apply an alpha channel. The image is also immune to
the mesh lines artifacts that plagued the original exported PDF.

Jens


Syd Geraghty

unread,
Dec 19, 2011, 7:26:03 AM12/19/11
to
Heike,

I noticed that the mesh function chosen automatically led to a mis-colouring of a small triangle at the origin which can be corrected by using a MaxRecursion of 7.


ContourPlot[x y^2 + 2, {x, -2, 2}, {y, -2, 2}, PlotRange -> {-10, 10},
ImageSize -> 600, MaxRecursion -> 7,
Method -> {"GridLinesInFront" -> True,
"TransparentPolygonMesh" -> True},
ColorFunction -> (Directive[Opacity[.5], ColorData["Rainbow"][#]] &),
ContourShading -> Automatic, GridLines -> Automatic]

Cheers .... Syd

Syd Geraghty B.Sc, M.Sc.

sydge...@me.com

Mathematica 8.0 for Mac OS X x86 (64-bit) (February 23, 2011)
ReleaseID: 8.0.1.0 (2063982, 2063639)
MacOS X V 10.7.1 Lion
MacBook Pro 2.33 GHz Intel Core 2 Duo 3GB RAM
0 new messages