Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

Consider the following line of code:

DensityPlot[4 Sin[2 Pi x] Cos[1.5 Pi y] (1 - x^2) (1 - y) y, 
                  {x, -1, 1}, {y, 0, 1}, Mesh -> All, MeshStyle -> Thick]

with the following output:

mesh

How can I convert the shown mesh into a Graph object such that:

  1. All the vertices are aligned at the intersection of the lines, withe correct VertexCoordinates.

  2. The edges (line segments in the plot) connect the corresponding vertices.

Note: If g is the result Graph, the the following code should give the same figure as the one above:

Show[DensityPlot[4 Sin[2 Pi x] Cos[1.5 Pi y] (1 - x^2) (1 - y) y, {x, -1, 1}, {y, 0, 1}], g]
share|improve this question
add comment

3 Answers

up vote 8 down vote accepted

This will do

densPlot = 
  DensityPlot[
   4 Sin[2 Pi x] Cos[1.5 Pi y] (1 - x^2) (1 - y) y, {x, -1, 1}, {y, 0,
     1}, MeshStyle -> Thick, Mesh -> All];

vertexCoordinates = densPlot[[1, 1]];

length = Length[vertexCoordinates];

graphReadyConnections =
  DeleteDuplicates@
   Flatten[
    Cases[#, 
       List[x_, y_, z_] :> {Sort[x \[UndirectedEdge] y], 
         Sort[x \[UndirectedEdge] z], Sort[y \[UndirectedEdge] z]}, 
       Infinity] &@
     densPlot[[1, 2, 1, 1, 3, 1, 1, 1]]
    ,
    1
    ];

Graph[Range[length], graphReadyConnections, 
 VertexCoordinates -> vertexCoordinates, 
 VertexShapeFunction -> {Disk[#, 0.005] &}, ImageSize -> 800]
share|improve this answer
add comment

If you don't mind using undocumented functions, you can do it like this:

Graphics`Mesh`MeshInit[];

mesh = DensityPlot[4 Sin[2 Pi x] Cos[1.5 Pi y] (1 - x^2) (1 - y) y, {x, -1, 1}, {y, 0, 1},
Method -> {"ReturnMeshObject" -> True}];

Graph[mesh["Edges"], VertexCoordinates -> mesh["Coordinates"], 
 VertexShapeFunction -> (Point[#] &)]

enter image description here

share|improve this answer
2  
The Graphics`Mesh` context sure is the gift that keeps on giving... –  J. M. Apr 23 '13 at 16:08
add comment

My modest attempt:

dp = DensityPlot[4 Sin[2 Pi x] Cos[3 Pi y/2] (1 - x^2) (1 - y) y,
                 {x, -1, 1}, {y, 0, 1}, Mesh -> All]

{verts, edgs} = List @@ MapAt[Composition[Union, Flatten],
        (Most[MapAt[Flatten[Cases[#, _Polygon, ∞]] &,
                    First[Cases[dp, _GraphicsComplex, ∞]], {2}]] /.
                    Polygon[p : {__?VectorQ}] :> Map[Polygon, p]) /.
         Polygon[p_] :> Map[Composition[Line, Sort], Partition[p, 2, 1, 1]], {2}]

Graph[Range[Length[verts]], edgs /. Line[l_] :> UndirectedEdge @@ l,
      VertexCoordinates -> verts, VertexShapeFunction -> {Point[#] &}]

graph from mesh

Compare:

Graphics[GraphicsComplex[verts, edgs]]

mesh lines from density plot

share|improve this answer
add comment

Your Answer

 
discard

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.