3
$\begingroup$

How can I extract coordinates from a list density plot.

Example plot of interest:

1

I need to place two points on either side of the center white circle and calculate the distance between them. My idea was to use two locators that gives their dynamic coordinates and connect a straight line between them. And calculate the different between them and output it to the user.

How can I do this with?

So far the only thing I figured out is to use a locators as a line to give me dynamic position.

Module[{v1 = {0, 0}, v2 = {2, 0}}, 
 Graphics[{Locator[Dynamic[v1]], Line[{Dynamic[v1], Dynamic[v2]}], 
   Locator[Dynamic[v2]]}, PlotRange -> 3, Frame -> True]]

Screenshot of output:

2

$\endgroup$
1
  • 1
    $\begingroup$ related Q&A $\endgroup$
    – Kuba
    Aug 7, 2013 at 8:12

3 Answers 3

5
$\begingroup$

A simple DynamicModule combined with a LocatorPane should give you a first starting point. The line and the distance could be included directly in the ListDensityPlot as dynamic Epilog

DynamicModule[{pt1 = {10., 10.}, pt2 = {30., 30.}}, 
 LocatorPane[Dynamic[{pt1, pt2}], 
  ListDensityPlot[
   Table[x + Sin[3 x + y^2], {x, -3, 3, 0.1}, {y, -3, 3, 0.1}], 
   ColorFunction -> "SunsetColors", PlotRangePadding -> None, 
   Epilog :> 
    Dynamic@{White, Dashed, Line[{pt1, pt2}], 
      Text[Style[Norm[pt1 - pt2], 18], Mean[{pt1, pt2}] + {5, 5}]}]
  ]
]

Mathematica graphics

By the way. If you want to have the real coordinates in your density plot, you should provide that data as {{x1,y1,f1}, {x2,y2,f2}, ...}.

$\endgroup$
4
  • 2
    $\begingroup$ Congratulations on breaking 30K! $\endgroup$
    – Mr.Wizard
    Aug 7, 2013 at 10:30
  • $\begingroup$ Though your image says its 60x60 the extra white boundary adds it upto 62.5. Is there any way I can avoid that white extra boundary from the image so that my locators give me exact 60 from one vertex to the other ? $\endgroup$ Aug 7, 2013 at 18:21
  • $\begingroup$ @abhilashsukumari You can set PlotRangePadding->0 or PlotRangePadding->None to avoid the white space (see also how @belisarius plotted it). $\endgroup$
    – Jens
    Aug 7, 2013 at 18:59
  • $\begingroup$ @abhilashsukumari I edited my answer and added the PlotRangePadding option to make it as big as the graphics. $\endgroup$
    – halirutan
    Aug 8, 2013 at 0:54
3
$\begingroup$
dp = DensityPlot[PDF[BinormalDistribution[{35, 23}, {7, 6}, -.7],
                 {x, y}], {x, 0, 50}, {y, 0, 50}, Frame -> False, ImageMargins -> False, 
                 PlotRangePadding -> None, AspectRatio -> Automatic]

Mathematica graphics

Show[Rasterize@dp, 
 Graphics[{Red, Thick, Rotate[Circle[#[[1]], #[[2]]], #[[3]]]}] &@(1 /. 
    ComponentMeasurements[Binarize[dp, .99], {"Centroid", "SemiAxes", "Orientation"}])]

Mathematica graphics

2 (1 /. ComponentMeasurements[Binarize[dp, .999], {"SemiAxes"}]) ((PlotRange /. 
             AbsoluteOptions[dp, PlotRange])[[1, 2]])/(ImageDimensions[bdp][[1]])

(*
  {{28.0864, 12.8444}}
*)
$\endgroup$
3
$\begingroup$

Here's a way using Manipulate and two Locators (using the plot from belisarius answer)

dp = DensityPlot[PDF[BinormalDistribution[{35, 23}, {7, 6}, -.7], {x, y}], 
    {x, 0, 50}, {y, 0, 50}, Frame -> False, ImageMargins -> False, 
    PlotRangePadding -> None, AspectRatio -> Automatic];
Manipulate[Show[dp, Graphics[{Line[{p1, p2}],  Inset[Norm[p1 - p2]]}]], 
    {{p1, {10, 10}}, Locator}, {{p2, {20, 20}}, Locator}]

enter image description here

$\endgroup$
3
  • $\begingroup$ Are you measuring in the same scale of the Density plot? $\endgroup$ Aug 7, 2013 at 23:24
  • $\begingroup$ @belisarius - using your density plot map, it has the same scale... $\endgroup$
    – bill s
    Aug 7, 2013 at 23:57
  • $\begingroup$ OK, +1 now .. :) $\endgroup$ Aug 8, 2013 at 0:00

Your Answer

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

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