12
$\begingroup$

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]
$\endgroup$

4 Answers 4

11
$\begingroup$

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]
$\endgroup$
13
$\begingroup$

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

$\endgroup$
2
  • 2
    $\begingroup$ The Graphics`Mesh`​ context sure is the gift that keeps on giving... $\endgroup$ Apr 23, 2013 at 16:08
  • 1
    $\begingroup$ Method -> {"ReturnMeshObject" -> True} appears to be broken in V10.1. Perhaps it has been superseded by DiscretizeGraphics and other mesh region functions. $\endgroup$
    – Michael E2
    Jul 9, 2015 at 18:08
7
$\begingroup$

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

$\endgroup$
2
$\begingroup$
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

$\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.