12

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, with correct VertexCoordinates.

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

Note: If g is the resulting Graph, then 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]
kglr
  • 394,356
  • 18
  • 477
  • 896
Helium
  • 4,059
  • 24
  • 41

4 Answers4

13

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

Simon Woods
  • 84,945
  • 8
  • 175
  • 324
11

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]
Jacob Akkerboom
  • 12,215
  • 45
  • 79
7

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

J. M.'s missing motivation
  • 124,525
  • 11
  • 401
  • 574
2
dp = 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, Frame -> False, Axes -> False, ImageSize -> 400]

dg = DiscretizeGraphics[dp];
edges = MeshCells[dg, 1]/. Line[{a_,b_}]:> UndirectedEdge[a,b];
vcoords =  MeshCoordinates[dg];
vertices = Range @ Length @ vcoords;

g = Graph[vertices, edges, VertexCoordinates -> vcoords, 
  VertexStyle -> Directive[Black, PointSize[0]], 
  VertexShapeFunction -> "Point", 
  EdgeStyle -> Directive[Thick, Opacity[1], Black], AspectRatio->1]

enter image description here

dp2 = Show[DensityPlot[4 Sin[2 Pi x] Cos[1.5 Pi y] (1 - x^2) (1 - y) y, 
  {x, -1, 1}, {y, 0, 1}, Mesh -> None, Frame -> False, Axes -> False], g, ImageSize->400];

 Show[{dp, dp2}]

enter image description here

kglr
  • 394,356
  • 18
  • 477
  • 896