5
$\begingroup$

I would like to combine a MatrixPlot and a GraphPlot, but I can't find a way to align them.

The code is

M = RandomChoice[{0, 1}, {4, 4}];
G = GridGraph[{5, 5}];

SetOptions[MatrixPlot, DataReversed -> True];

Show[MatrixPlot[M], GraphPlot[G]]

And the output displays the graph G with its origin in {1,1}, whereas the origin of the matrix is in {0,0}.

Two graphs combined, but displaced

How can I align the origin of both graphics?

$\endgroup$
3
$\begingroup$

This works, but note that the graph is re-made (with translated vertices.)

SeedRandom[123];
M = RandomChoice[{0, 1}, {4, 4}];
G = GridGraph[{5, 5}];
vcs = Map[# - {1, 1} &, 
   VertexCoordinates /. AbsoluteOptions[G, VertexCoordinates]];
G = GridGraph[{5, 5}, VertexCoordinates -> vcs];

SetOptions[MatrixPlot, DataReversed -> True]; 
Show[MatrixPlot[M], GraphPlot[G]]

enter image description here

$\endgroup$
2
$\begingroup$

Use the VertexCoordinates option:

mM = RandomChoice[{0, 1}, {4, 4}]
mG = GridGraph[{5, 5}, VertexCoordinates -> Tuples[Range[5] - 1, 2]]
Show[MatrixPlot[mM, DataReversed -> True], GraphPlot[mG]]
$\endgroup$
0
$\begingroup$

Consider DataRange?

SeedRandom[7]

M = RandomChoice[{0, 1}, {4, 4}];
G = GridGraph[{5, 5}];

SetOptions[MatrixPlot
  , DataReversed -> True
  , DataRange -> {{1.5, 4.5}, {1.5, 4.5}}
  , FrameTicks -> Range@{5, 5, 5, 5}
];

Show[MatrixPlot[M], GraphPlot[G]]

enter image description here

$\endgroup$
0
$\begingroup$

VertexCoordinateRules set to TranslationTransform of GraphEmbedding[G]:

mp1 = MatrixPlot[M, DataReversed -> True, ImageSize -> 300,  Epilog -> 
 GraphPlot[G, VertexCoordinateRules -> 
     TranslationTransform[{-1, -1}] @ GraphEmbedding[G]][[1]]];

TranslationTransform on Show[G]:

mp2 = MatrixPlot[M, DataReversed -> True,  ImageSize -> 300, Epilog -> 
  MapAt[GeometricTransformation[#, TranslationTransform[{-1, -1}]] &, Show[G], {1}][[1]]];

VertexCoordinates set to TranslationTransform of GraphEmbedding[G]:

G2 = Show[SetProperty[G, { VertexStyle -> RGBColor[0, 0, 0.7], VertexSize -> .05, 
     EdgeStyle -> Directive[Opacity[1], RGBColor[0.5, 0., 0.]], 
     VertexCoordinates -> TranslationTransform[{-1, -1}] @ GraphEmbedding[G]}]][[1]];
mp3 = MatrixPlot[M, DataReversed -> True, ImageSize -> 300,  Epilog -> G2];

Row[{mp1, mp2, mp3}, Spacer[10]]

enter image description here

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.