4
$\begingroup$

How to change color and size at the same time of some vertices of a graph?

I am trying to do it more than an hour but nothing seems to work.

I can do it either for color or for size but not both at the same time.

I want to have big blue disks.

HighlightGraph[GridGraph[{3, 3}], {1, 2}, VertexSize -> .2]
HighlightGraph[GridGraph[{3, 3}], Style[{1, 2}, Blue]]

enter image description here

enter image description here

This does not work:

HighlightGraph[GridGraph[{3, 3}], {1, 2}, VertexSize -> .2,Style[{1, 2}, Blue]]

Update:

Why the following code changes size of all vertices, I want to change size only of the green ones.

HighlightGraph[
 GridGraph[{3, 3}], {Style[PathGraph[{3, 2, 1, 4, 7, 8, 9, 6, 5}], 
   Red, Thickness[0.01]], Style[{3, 5}, Green]}, VertexSize -> 0.2]

enter image description here

$\endgroup$
1
  • 1
    $\begingroup$ With your last example, you are relying on the fact that HighlightGraph will interpret VertexSize -> x as wanting all highlighted vertices with size x. You want more control than that, so use the more explicit form of that option: VertexSize -> {3 -> 0.2, 5 -> 0.2} $\endgroup$ – Jason B. Sep 4 '20 at 20:42
5
$\begingroup$

This input

HighlightGraph[GridGraph[{3, 3}], {1, 2}, VertexSize -> .2,Style[{1, 2}, Blue]]

is not valid syntax for HighlightGraph. VertexSize is an Option and so it must come after all the other arguments

HighlightGraph[GridGraph[{3, 3}], Style[{1, 2}, Blue], 
 VertexSize -> .2]

enter image description here

Just for fun, here is another way you can modify properties of vertices and edges:

g = GridGraph[{3, 3}];
AnnotationValue[{g, {1, 2}}, VertexSize] = .2;
AnnotationValue[{g, {1, 2}}, VertexStyle] = Blue;
g
$\endgroup$
1
  • $\begingroup$ OK thanks, if you be so kind to answer additional question - see update. Why in your code the parameter changes only the two vertices and in my code all? $\endgroup$ – azerbajdzan Sep 4 '20 at 20:19
3
$\begingroup$

You can specify VertexStyle directives using Style and

  1. use the form {Alternatives@@vlist1 -> optionvalue1, ...} for setting the values of other Vertex* options:

HighlightGraph[GridGraph[{3, 3}], 
  {Style[PathGraph[{3, 2, 1, 4, 7, 8, 9, 6, 5}], Red, Thickness[0.01]], 
  Style[{3, 5}, Green, EdgeForm[{Purple, Opacity[1], Thickness[.02]}]]}, 
 VertexSize -> {3 | 5 -> .2, 1 -> .3},
 VertexShapeFunction -> {3 | 5 -> "Square", 1 -> "ConcaveHexagon"}, 
 VertexLabels -> {3 | 5 -> Placed["Name", Center]},
 VertexLabelStyle -> 16]

enter image description here

  1. or use Property[vlist, {options}] (Annotation[vlist, {options}] in version 12.1+) to specify Vertex* options for vlist:

HighlightGraph[GridGraph[{3, 3}], 
 {Style[PathGraph[{3, 2, 1, 4, 7, 8, 9, 6, 5}], Red, Thickness[0.01]], 
  Property[Style[{3, 5}, Green, EdgeForm[{Opacity[1], Blue, Thickness[.02]}]], 
   {VertexSize -> 0.2, VertexLabels -> Placed["Name", Center], 
    VertexShapeFunction -> "Hexagon"}]}]

enter image description here

$\endgroup$
1
  • $\begingroup$ Thanks for very useful informations. In the Mathematica help you can find how to set different properties to all items but if you want to combine several properties to different items you have no examples in the help. $\endgroup$ – azerbajdzan Sep 5 '20 at 20:22

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.