Re: Transparent ContourPlot
- To: mathgroup at smc.vnet.net
- Subject: [mg123781] Re: Transparent ContourPlot
- From: JUN <noeckel at gmail.com>
- Date: Mon, 19 Dec 2011 07:16:36 -0500 (EST)
- Delivered-to: l-mathgroup@mail-archive0.wolfram.com
- References: <201112161052.FAA06915@smc.vnet.net> <jckc3t$1hu$1@smc.vnet.net>
On Dec 18, 1:34 am, Heike Gramberg <heike.gramb... at gmail.com> wrote: > 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.... > > > This produces a mess: > > > ContourPlot[x y^2 + 2, {x, -2, 2}, {y, -2, 2}, PlotRange -> {-10, 10}, > > ContourShading -> {Orange, Opacity[0.5]}] 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
- References:
- Transparent ContourPlot
- From: dg <davide.guarisco@gmail.com>
- Transparent ContourPlot