6
$\begingroup$

I am trying to use a ColorFunction with Opacity in ContourPlot. Here's my code:

data = RandomVariate[BinormalDistribution[.75], 10];
d = SmoothKernelDistribution[data];
cf[z_] := {Opacity[z], Red};
sc1 = ContourPlot[
   Evaluate@PDF[d, {x, y}], {x, -3, 3}, {y, -3, 3}, 
   PlotRange -> All, PlotPoints -> 50, ColorFunction -> cf, 
   ColorFunctionScaling -> True]

And here's what it produces:

enter image description here

How can the ugly mesh be avoided? I am using Mathematica 8.0.1 in OS X.

$\endgroup$
11
  • $\begingroup$ does Mesh -> None give what you need? Btw, I don't get the "ugly mesh" with Version 9 on Windows Vista 64 bit. $\endgroup$
    – kglr
    Feb 28, 2013 at 20:34
  • $\begingroup$ @Guillochon Here it works fine Mathematica 9.0.1 Win7 64b $\endgroup$
    – Lou
    Feb 28, 2013 at 20:36
  • $\begingroup$ Mesh -> None is not a legal option for ContourPlot for me. I am using Mathematica 8.0.1 for OS X. $\endgroup$
    – Guillochon
    Feb 28, 2013 at 20:37
  • $\begingroup$ You may use Mesh->False, but it doesn't do what you want $\endgroup$ Feb 28, 2013 at 20:41
  • 1
    $\begingroup$ As above, what platform are you on? Can you please include that information. $\endgroup$ Feb 28, 2013 at 21:05

3 Answers 3

10
$\begingroup$

If you are using OS X the use undocumented Method -> {"TransparentPolygonMesh" -> True}

enter image description here

$\endgroup$
10
  • $\begingroup$ Well, it doesn't solve the problem here $\endgroup$ Feb 28, 2013 at 20:57
  • $\begingroup$ How do you mean? Am I misreading the question? I thought he wanted the mesh to go (??) $\endgroup$ Feb 28, 2013 at 20:59
  • $\begingroup$ In my machine the mesh disappears with Style[sc1, Antialiasing -> False] but not with this solution. Probably version/platform dependent $\endgroup$ Feb 28, 2013 at 21:01
  • $\begingroup$ @belisarius Yes, the problem is Mac-specific. $\endgroup$
    – Szabolcs
    Feb 28, 2013 at 21:02
  • $\begingroup$ @Szabolcs I'm on Windows, and I have the same problem. The solution is Mac-specific :) $\endgroup$ Feb 28, 2013 at 21:03
3
$\begingroup$

Since you are looking at a PDF, I would use SmoothDensityHistogram to plot it, instead.

data = RandomVariate[BinormalDistribution[.75], 10];
d = SmoothKernelDistribution[data];
cf[z_] := {Opacity[z], Red};

sc1 = SmoothDensityHistogram[data , Automatic, "PDF", 
  PlotRange -> All, ColorFunction -> cf, Mesh -> 10, 
  MeshStyle -> Directive[Opacity[0.5], Black]]

enter image description here

By default, in this case, it was not displaying the contours, so I added them via Mesh and MeshStyle. Modify to taste.

$\endgroup$
3
  • $\begingroup$ I looked at this as an option, but the problem was not being able to get flat shading between the contours. $\endgroup$
    – Guillochon
    Feb 28, 2013 at 21:01
  • $\begingroup$ @Guillochon, why do you want the flat shading they don't truly represent the function? $\endgroup$
    – s0rce
    Feb 28, 2013 at 21:02
  • 2
    $\begingroup$ It's a stylistic choice, the smooth coloring is hard to see, and is harder to notice when printed to paper. $\endgroup$
    – Guillochon
    Feb 28, 2013 at 21:15
3
$\begingroup$

Just for behavior documentation. Mathematica 8.0 on WinXP.

GraphicsRow[Style[sc1, Antialiasing -> #] & /@ {True, False}]

Mathematica graphics

$\endgroup$
10
  • $\begingroup$ When I tested this on Windows 7, sc1 without any antialiasing option produced an antialiased graphic without a visible mesh. (I.e. when not specifying Antialiasing at all, the problem didn't appear.) $\endgroup$
    – Szabolcs
    Feb 28, 2013 at 21:51
  • $\begingroup$ I get the same with mma 9.0.0 win7 64 bit i.stack.imgur.com/f9um4.png $\endgroup$
    – Ajasja
    Feb 28, 2013 at 21:57
  • $\begingroup$ @Szabolcs So it's definitely due to a change in defaults. I think specifying the Antialiasing makes the results portable $\endgroup$ Feb 28, 2013 at 21:58
  • $\begingroup$ @Ajasja Try including Automatic too in the list of options, not just True and False. This is what I got with Automatic: i.stack.imgur.com/Vtpi1.png $\endgroup$
    – Szabolcs
    Mar 1, 2013 at 0:24
  • $\begingroup$ I have to comment on this again, because it's misleading: on all Windows systems I tried, there's no problem with the mesh if you don't use any anti-aliasing setting at all. A fair example would show Antialiasing -> Automatic as well. This example prevented the original question from being closed as a duplicate even though it really should be. It's a Mac OS X specific issue which is solved by the suggestion in the accepted answer. $\endgroup$
    – Szabolcs
    Apr 4, 2013 at 14:09

Not the answer you're looking for? Browse other questions tagged or ask your own question.