7
$\begingroup$

I would like to label a graph with matrices. Suppose the graph is simple:


GraphPlot
And I have an array mats that contains the labels for vertices 1,2,3,4 (these are just random 0-1 matrices; ignore the entries):
mats
What is the appropriate syntax to employ VertexRenderingFunction to achieve having each vertex i replaced by (an image of) the MatrixForm of mats[[i]]? Or perhaps I am looking in the wrong direction entirely...?

I know this is elementary; thanks for your patience & advice!

$\endgroup$

1 Answer 1

13
$\begingroup$

It looks to me like you're using DirectedEdges, which are designed to work with Graph as opposed to GraphPlot. If you'd like to use GraphPlot and VertexRenderingFunction, you can do like so:

matrix[1] = {
   {0, 1, 0},
   {0, 0, 1},
   {0, 0, 1}
   };
matrix[2] = {
   {1, 0, 0},
   {1, 0, 1},
   {1, 0, 0}
   };
matrix[3] = {
   {1, 0, 1},
   {0, 0, 0},
   {0, 1, 1}
   };
matrix[4] = {
   {1, 1, 0},
   {0, 1, 1},
   {0, 0, 0}
   };
vertexRenderer[position_, v_] := 
  {Text[Framed[matrix[v], Background -> RGBColor[1, 1, 0.8],
     FrameStyle -> RGBColor[0.94, 0.85, 0.36]], position]};
GraphPlot[{1 -> 2, 1 -> 3, 3 -> 4},
 DirectedEdges -> True,
 VertexRenderingFunction -> vertexRenderer]

enter image description here

You might seriously consider using the newer (and generally better) Graph based functionality and leverage the fact that virtually anything can be a vertex - including your matrices. Then, labeling the vertices is as simple as using VertexLabels.

Graph[{matrix[1] -> matrix[2], matrix[1] -> matrix[3], matrix[3] -> matrix[4]},
  VertexLabels -> "Name", PlotRangePadding -> {{0.01, 0.2}, {0.01, 0.2}}]

enter image description here

If desired, you can always use VertexShapeFunction to get more control over the appearance.

$\endgroup$
1
  • $\begingroup$ Thank you so much! The second method is especially easy. $\endgroup$ Commented Aug 30, 2014 at 11:40

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.