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:
All the vertices are aligned at the intersection of the lines, with correct
VertexCoordinates
.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]
Answer
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]
Comments
Post a Comment