Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have a data file with triplets. The first two numbers indicate the x and y position, while the third one is the log10 value of a particular indicator. I want to create a 2D plot in which the positions are plotted and assigned colors according to the value of the third number. I think the best solution is to use ListContourPlot or ListDensityPlot. I tried the first one and I managed to create the plot I wanted. However, there is problem which I cannot solve. I provide the corresponding Mathematica code. Unfortunately, I don't know how to upload the data file. The black dots correspond to the points. As you can see, there is a region with "bad" colors in which there are no points at all in the data file, but the algorithm is trying to spread the colors in that region. How could I fix this? I want to restrict colors to the data.

   ContourShading -> Automatic,
   ContourStyle -> None,
   InterpolationOrder -> 5, 
   ColorFunction -> "Rainbow", 
   PlotLegends -> 
          LegendMargins -> {{0, 0}, {10, 5}}, 
          LegendLabel -> "\!\(\*SubscriptBox[\(log\), \(10\)]\)(MLCN)", 
          LabelStyle -> {FontSize -> 15, FontFamily -> "Helvetica"}], 
   PerformanceGoal -> "Quality", 
   FrameLabel -> {"R", OverDot["R"]}, 
   RotateLabel -> False, 
   LabelStyle -> Directive[FontSize -> 17, FontFamily -> "Helvetica"], 
   Contours -> 200,
   ImageSize -> 500]

Code and data files
Apparently it's not possible at this moment to easily upload files to the StackMMA attachment repository (see this Meta question). Therfore I share the above mentioned files in the following way.

The files will be automatically opened in a file system browser (at least on Windows) by executing the following single line of code in Mathematica (this method is describe here):

SystemOpen[Export[FileNameJoin[{$TemporaryDirectory, ""}], Uncompress@FromCharacterCode@Flatten@ImageData[Import[""], "Byte"], "Byte"]]


share|improve this question
The data files and the complete Mathematica notebook can be downloaded from Many thanks in advance and I look forward for your reply. – user5051 Dec 16 '12 at 14:31
If I am correct, the only solution is through the RegionFuction. However, still I cannot make it work. If anyone has an idea please leave a comment. The data files and the Math code are available using the above link. – user5051 Dec 16 '12 at 17:13

For some reason the ListContourPlot doesn't seem to be accurately using the given RegionFunction. But you could just plot the complement region separately in white, and then overlay them:

complement = RegionPlot[Rp^2/2 + \[CapitalPhi][R, 0] > en, {R, 0, 11}, {Rp, 0, 70}, 
  PlotStyle -> White, BoundaryStyle -> None]

Show[S1, complement]


share|improve this answer

If the region, where you have no data (or the region where you have data) is easy to express, you could use RegionFunction to restrict the plotting to only the region in which you have data. Compare the two plots produced by the followin code:

data = Flatten[Table[{x, y, Sin[x + y^2]}, {x, 0, 3, 0.1}, {y, 0, 3, 0.1}], 1];
region = Function[{x, y}, 1 < x^2 + y^2 < 3];
GraphicsRow[{ListContourPlot[data], ListContourPlot[data, RegionFunction -> region]}]

Here the RegionFunction is used to include only the data in the range 1 < x^2 + y^2 < 3 in the plot.

enter image description here

share|improve this answer
I can define the region in which there are all the data through a contour plot. In particular: Lz = 10; Mn = 500; cn = 0.25; en = -670; G = 1; a = 3; Md = 7000; h = 0.175; Φ[R_, z_] := -((G Md)/Sqrt[R^2 + (a + Sqrt[z^2 + h^2])^2]) + Lz^2/( 2 R^2) - (G Mn)/Sqrt[R^2 + z^2 + cn^2]; cv0 = ContourPlot[Rp^2/2 + Φ[R, 0] == en, {R, 0, 11}, {Rp, 0, 70}, ContourStyle -> {Black, Thickness[0.004]}, PlotPoints -> 300, ImageSize -> 500] However, if I use RegionFunction -> cv0 inside ListContourPlot nothing changes. Any more ideas? – user5051 Dec 16 '12 at 12:08

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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