# Interpolation of a border between regions in ListDensityPlot

Let's say there is a scalar field dependent on two coordinates {x,y}, so a set {x, y, f}, and I use a ListDensityPlot to plot it:

ListDensityPlot[data,PlotRange->{0.5,1},InterpolationOrder->2]


Here we have two regions, a brown one and yellow one: Now I need a smooth curve which would separate these two regions: Ideally, I would like to extract also this curve to a separate plot. How this problem can be solved for ListDensityPlot? Thank you.

• Welcome to Mathematica.SE! 1) As you receive help, try to give it too, by answering questions in your area of expertise. 2) Take the tour and check the faqs! 3) When you see good questions and answers, vote them up by clicking the gray triangles, because the credibility of the system is based on the reputation gained by users sharing their knowledge. Also, please remember to accept the answer, if any, that solves your problem, by clicking the checkmark sign!
– user9660
Feb 27, 2016 at 15:47
• Since you not providing any data, see here Density plot with list data and preferable boundary
– user9660
Feb 27, 2016 at 15:50
• Possible duplicate of: (1), (2), (3). Feb 27, 2016 at 19:35
• They deal with ContourPlot and RegionPlot, but I need ListDensityPlot only for two explicit regions. Feb 28, 2016 at 16:43
• @ElectroWin What you want is a particular contour line for your data. That is why people suggested that you use e.g. ListContourPlot rather than ListDensityPlot, then extract the contour line from the Graphics object that is generated by that function. But again, if you don't share your data with us, we won't be able to help your with your specific problem... Feb 29, 2016 at 15:54

You could try something like the following using contour extraction from a ListContourPlot.

Generate some noisy data:

data = Flatten[#, 1] &@
Table[
{x, y, CDF[BinormalDistribution[0], {-x, -y}] + RandomReal[{-0.02, 0.02}]},
{x, -3, 3, 0.1}, {y, -3, 3, 0.1}
];


On that data set, we will do the following:

1. generate a contour plot with a specific desired value for the contour
2. extract the contour line from the plot output
3. aggressively smoothen the line using an exponential moving average
4. overlay it on the contour plot

All of it is wrapped in a Manipulate for convenience of finding the contour value you are actually interested in:

Manipulate[
(* generate contour plot *)
cp = ListContourPlot[
data,
Contours -> {contourvalue},
PlotRange -> All
];

(* extract contour data and smoothen *)
smoothcontour = ExponentialMovingAverage[
First@Cases[Normal@cp, Line[a__] -> a, Infinity],
.1 (* smoothing factor; adjust to taste *)
];

(* overlay the smoothed contour line on the original contour plot *)
Show[
cp,
Graphics[{Thickness[0.01], Red, Dashed, Line@smoothcontour}]
],

(* adjust the min / max values of this range to suit your data *)
{{contourvalue, 0.4}, 0.1, 1, 0.1, Appearance -> "Labeled"}
]


• MarcoB, thank you very much, that's exactly what I need! Mar 2, 2016 at 17:52
• @ElectroWin Glad it helped! Mar 2, 2016 at 18:42
{"extraction_info": {"found_math": true, "script_math_tex": 0, "script_math_asciimath": 0, "math_annotations": 0, "math_alttext": 0, "mathml": 0, "mathjax_tag": 0, "mathjax_inline_tex": 0, "mathjax_display_tex": 0, "mathjax_asciimath": 1, "img_math": 0, "codecogs_latex": 0, "wp_latex": 0, "mimetex.cgi": 0, "/images/math/codecogs": 0, "mathtex.cgi": 0, "katex": 0, "math-container": 0, "wp-katex-eq": 0, "align": 0, "equation": 0, "x-ck12": 0, "texerror": 0, "math_score": 0.21890497207641602, "perplexity": 2063.294791778797}, "config": {"markdown_headings": true, "markdown_code": true, "boilerplate_config": {"ratio_threshold": 0.3, "absolute_threshold": 10, "end_threshold": 15, "enable": true}, "remove_buttons": true, "remove_image_figures": true, "remove_link_clusters": true, "table_config": {"min_rows": 2, "min_cols": 3, "format": "plain"}, "remove_chinese": true, "remove_edit_buttons": true, "extract_latex": true}, "warc_path": "s3://commoncrawl/crawl-data/CC-MAIN-2022-21/segments/1652663006341.98/warc/CC-MAIN-20220527205437-20220527235437-00297.warc.gz"}