Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

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!

share|improve this question

1 Answer 1

up vote 12 down vote accepted

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.

share|improve this answer
    
Thank you so much! The second method is especially easy. –  Joseph O'Rourke Aug 30 at 11:40

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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