3
$\begingroup$

When I try to plot a 2D interpolation, coloring areas with RegionPlot and adding contours with ContourPlot, the boundary given by RegionPlot and the contours from ContourPlot don't always agree. Why is this and how do I fix it?

dataList = Uncompress @ Import["http://pastebin.com/raw.php?i=zUrJkXR4"];
area =  ConvexHullMesh[dataList[[All, 1 ;; 2]]];
dataList[[All, 1 ;; 2]] +=  
  RandomReal[10^-5*{-1, 1}, Dimensions[ dataList[[All, 1 ;; 2]]]];
  (* Recommended by Mathematica to avert zero-size triangulations *)
funct = Interpolation[dataList];
contour = ContourPlot[funct[a, b] == 124 , {a, b} ∈ area];
region = RegionPlot[funct[a, b] <= 124, {a, b} ∈ area];
Show[{region, contour}]

This produces

Contours do not line up!

$\endgroup$
5
  • 2
    $\begingroup$ It is not possible to answer this question without more information, preferably an example. $\endgroup$
    – bbgodfrey
    Nov 12, 2015 at 13:59
  • 1
    $\begingroup$ Perhaps increasing the number of PlotPoints in RegionPlot. $\endgroup$
    – Vaggelis_Z
    Nov 12, 2015 at 13:59
  • 3
    $\begingroup$ Here its considered helpful and polite show you own efforts and share your data and code attempts in a well formatted form, so we can quickly see the problem you are facing. Please help us to help you and edit your question accordingly. $\endgroup$
    – rhermans
    Nov 12, 2015 at 14:02
  • $\begingroup$ @rhermans Agreed. I now included a MWE. I do not know how to ideally display the data, though. The regular spoiler markup does not seem to work. $\endgroup$
    – Neuneck
    Nov 12, 2015 at 14:14
  • 1
    $\begingroup$ Pastebin is useful, look at this question. I have edited it for you. You do it next time. $\endgroup$
    – rhermans
    Nov 12, 2015 at 14:20

2 Answers 2

6
$\begingroup$

A direct way to obtain the desired region, here superimposed on the total range of dataList, is

ListContourPlot[dataList, Contours -> {124}]

enter image description here

The faint background can be eliminated, if desired, by

ListContourPlot[dataList, Contours -> {124}, ContourShading -> {Blue, White}]

As described here, the underlying cause for the poor quality of all these curves is that "currently no algorithm is implemented that allows to recover a higher-order interpolation on a list of unstructured points".

Display in 3D

The curve also can be displayed without difficulty on the three-dimensional surface representing dataList.

ListPlot3D[dataList, Mesh -> {{124}}, MeshFunctions -> {(#3) &},  BoundaryStyle -> None];
ListPointPlot3D[dataList, PlotStyle -> Red];
Show[%%, %]

enter image description here

Superimposed are the actual data points.

$\endgroup$
1
  • $\begingroup$ A very nice suggestion. However, I finally want to create plots where I use different constraints and display an overlay what constraints apply to what region. I used to do this with RegionPlot, which allows for a convenient combination of conditions. I do not see how to recreate that behavior with ListContourPlot. Still +1 :) $\endgroup$
    – Neuneck
    Nov 16, 2015 at 13:44
3
$\begingroup$

The issue is obviously RegionPlot is not doing a good job of discretizing the function. I could not coax RegionPlot to do a better job. Here is one approach:

pts = Cases[Normal@contour, Line[list_] :> list, Infinity][[1]];
Show[{contour, 
  Graphics@{Red, 
    Polygon[Join[pts, {{1.38, 1}, {1.38, 1.38}, pts[[1]]}]]}}]

enter image description here

oh here's a better way..

ContourPlot[funct[a, b], {a, b} \[Element] area, Contours -> {124}, 
       ContourShading -> {Red, White}]

enter image description here

$\endgroup$
1
  • $\begingroup$ A very nice suggestion. However, I finally want to create plots where I use different constraints and display an overlay what constraints apply to what region. I used to do this with RegionPlot, which allows for a convenient combination of conditions. I do not see how to recreate that behavior with ContourPlot. Still +1 $\endgroup$
    – Neuneck
    Nov 16, 2015 at 13:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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