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 have the following problem: I used GraphPlot to plot my adjacency matrix. I divided my nodes in three groups: A = {...}, B={...} and C={...}. I would like to color the nodes conditioning on them belonging to group A (Blue), B (Red), C (Grey). How do I do that?

Example: I have $12$ nodes distributed in three groups as follows: $ A=\{1,4,5,8\}, B=\{2,3,6,7,9,10\},C=\{11,12\}$. I want to color the nodes in $A$ blue, the ones in $B$ red and the ones in $C$ grey. I have of course a $12$x$12$ adjacency matrix $M$ and I use GraphPlot[M] to plot the graph. Thank you.

share|improve this question
    
Please include an example –  rm -rf Oct 3 '13 at 20:39
    
@rm-rf I have just added an example, thank you! –  Friedrich Nietzsche Oct 3 '13 at 20:46
    
Please look up HighlightGraph and use Graph instead of GraphPlot. There's no built-in function named PlotGraph. If you meant GraphPlot, please make sure that you are precise and you type the correct names in this question. This makes the difference between a working and a non-working command. –  Szabolcs Oct 3 '13 at 20:55

2 Answers 2

up vote 2 down vote accepted

Using the notation Pinguin Dirk you could also use VertexStyle in AdjacencyGraph

aa = {1, 4, 5, 8}; bb = {2, 3, 6, 7, 9, 10}; cc = {11, 12};
vs = Join @@ 
   MapThread[Thread[#1 -> #2] &, {{aa, bb, cc}, {Blue, Red, Gray}}];
mm = RandomChoice[{0, 1}, {12, 12}];
AdjacencyGraph[mm, VertexSize -> 0.4, VertexStyle -> vs]

enter image description here

EDIT

If GraphPlot is what is desired then:

GraphPlot[mm, 
 VertexRenderingFunction -> ({#2 /. vs, Disk[#1, 0.1]} &)]
share|improve this answer

Let's say you have some random matrix, such as:

mm = RandomChoice[{0, 1}, {12, 12}];

and the lists

aa = {1, 4, 5, 8}; bb = {2, 3, 6, 7, 9, 10}; cc = {11, 12};

Now, GraphPlot can be used to plot an adjacency matrix, (docu: GraphPlot[m] generates a plot of the graph represented by the adjacency matrix m.)

Thus, using in particular, VertexRenderingFunction, we can do:

GraphPlot[mm, ImageSize -> Large, 
     VertexRenderingFunction -> (Text[
     Framed[#2, 
         Background -> 
             Which[
               MemberQ[aa, #2], LightRed, 
               MemberQ[bb, #2], LightBlue, 
               MemberQ[cc, #2], LightGray, 
               True, Pink]], #1] &)]

I chose LightRed etc for better readability. Obviously, lot's of formatting can be done from this point to make it look nice.

enter image description here

(a small aside: try not to use single letter upper-case names for variables. E.g. C is already taken by the system)

share|improve this answer

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.