# Converting Mesh in DensityPlot to a Graph

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: 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]


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];

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
];

VertexCoordinates -> vertexCoordinates,
VertexShapeFunction -> {Disk[#, 0.005] &}, ImageSize -> 800]


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

GraphicsMeshMeshInit[];

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[#] &)] • The GraphicsMesh​ context sure is the gift that keeps on giving... Apr 23 '13 at 16:08
• Method -> {"ReturnMeshObject" -> True} appears to be broken in V10.1. Perhaps it has been superseded by DiscretizeGraphics and other mesh region functions. Jul 9 '15 at 18:08

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[#] &}] Compare:

Graphics[GraphicsComplex[verts, edgs]] 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],
VertexShapeFunction -> "Point",
EdgeStyle -> Directive[Thick, Opacity, Black], AspectRatio->1] 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}] 