1
$\begingroup$

I have the following density plot:

DensityPlot[x^2 + y^2, {x, -1, 1}, {y, -1, 1} , MeshFunctions -> {#3 &}, Mesh -> 1]

I want to collect the points on the level curve z=1 shown by the mesh on the graph.

thanks for your help

enter image description here

$\endgroup$
4
  • 1
    $\begingroup$ If g is your plot you could just use the output as in ListPlot[Select[g[[1, 1]], 0.99 <= [[1]]^2 + #[[2]]^2 <= 1.01 &]]. $\endgroup$ Commented Mar 6, 2016 at 9:39
  • 4
    $\begingroup$ Try Cases[Normal[(* plot *)], _Line, Infinity] and report back… $\endgroup$ Commented Mar 6, 2016 at 9:39
  • $\begingroup$ @b.gatessucks Thank you very much, it works. $\endgroup$
    – Ahmad A
    Commented Mar 6, 2016 at 9:58
  • $\begingroup$ @J.M. This method works too. Thank you very much. $\endgroup$
    – Ahmad A
    Commented Mar 6, 2016 at 9:59

1 Answer 1

1
$\begingroup$

The following is just for the sake of entering an answer to this question that has been otherwise answered in comments.

Let's store your plot:

g = DensityPlot[x^2 + y^2, {x, -1, 1}, {y, -1, 1} , MeshFunctions -> {#3 &}, Mesh -> 1]

@J.M. suggested extracting the Line object representing the contour line directly from the plot, which is the more straightforward approach in my opinion:

contour = First@Cases[Normal[g], _Line, Infinity]

{Line[{{0.0714286, -0.997354}, {-2.22045*10^-16, -1.}, [...], {0.0714286, -0.997354}}]}

This returns a Line object, which can be visualized directly using Graphics, or from which coordinates can be extracted.

Graphics[contour]
contour[[1]]

JM

{{0.0714286, -0.997354}, {-2.22045*10^-16, -1.}, [...], {0.0714286, -0.997354}}


Alternatively, @b.gatessucks suggested using a Select statement to extract points that belong to the circle.

Note that a # had gone missing in the comment; I would also suggest adding AspectRatio -> Automatic to the ListPlot to make sure that circles are plotted as such, rather than deformed (AspectRatio otherwise defaults to 1/GoldenRatio for the plotting functions):

ListPlot[
 Select[g[[1, 1]], 0.99 <= #[[1]]^2 + #[[2]]^2 <= 1.01 &],
 AspectRatio -> Automatic
]

b.gates

$\endgroup$

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.