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.

What is the most convenient way to change options such as VertexLabels in existing Graph objects? (Version 7 users note: Graph is new in Mathematica 8.)


With graphics, we can use Show for this, not matter what function was originally used to produce the graphic:

g = Plot[Sin[x], {x, 0, 10}]
Show[g, Axes -> False, Frame -> True]

Is there an analogous function for Graphs? Suppose we already have a graph, and now we need to show vertex labels in a different way.

Here's a workaround using HighlightGraph:

g = Graph[{1 -> 2}]
HighlightGraph[g, {}, VertexLabels -> "Name"]

While it works, this is not really what HighlightGraph is meant for.


Please note that Graph objects are atomic before trying to take them apart. Also, I'm looking for the safest and most robust solution. I'm hoping there's a function I overlooked.

share|improve this question

migrated from stackoverflow.com Apr 20 '12 at 13:05

This question came from our site for professional and enthusiast programmers.

add comment

3 Answers 3

up vote 13 down vote accepted

You could use SetProperty. For example

g = Graph[{1 -> 2, 2 -> 4, 3 -> 4}]

SetProperty[g, VertexLabels -> {"Name", 2 -> "Two"}]

Mathematica graphics Mathematica graphics

share|improve this answer
    
@Szabolcs, Heike Unfortunately (and unlike Show), SetProperty does not work if there is no second argument, which is a PITA if ypou want to make a wrapper function and call it with no options: Options[plotGraph] = Options@Graph; plotGraph[g_Graph, opts:OptionsPattern[]] := SetProperty[g, opts]; g = Graph[{1 -> 2, 2 -> 3}];plotGraph[g]. So one has to check whether opts is empty or not... –  István Zachar Nov 25 '13 at 11:06
add comment

You can use Graph itself much like Show. However, you pass the essential data from the graph as the first argument, rather than the graph itself. (Graph[g,....] will not work.)

g = Graph[{1 -> 8, 1 -> 11, 1 -> 18, 1 -> 19, 1 -> 21, 1 -> 25, 1 -> 26}, 
    VertexLabels -> "Name", 
    ImagePadding -> 10]

Graph[EdgeList[g], VertexShapeFunction -> "Diamond", VertexSize -> Medium]

graphs

Special types of graphs, e.g. TreeGraph, AdjacencyGraph, IncidenceGraph, etc. should work in a similar fashion.


Edit

Heike's solution has the advantage of maintaining the original options of the graph:

SetProperty[g, VertexShapeFunction -> "Diamond"]

graph2

It's nice that the VertexNames are displayed as they were initially in g (in contrast to the second graph shown above).

share|improve this answer
    
Strange, on my version of Mathematica setting VertexSize works fine, e.g. SetProperty[g, {VertexShapeFunction -> "Diamond", VertexSize -> Large}]. –  Heike Jan 15 '12 at 21:19
    
When I cut and paste your code, SetProperty[g, {VertexShapeFunction -> "Diamond", VertexSize -> Large}], into Mathematica, it works fine. However, when I type it into my notebook, it does not. Seems like there is some sort of corruption in my notebook. –  David Carraher Jan 15 '12 at 21:47
    
@David, is it possible that you forgot to put the options into a list when typing SetProperty[g, {VertexShapeFunction -> "Diamond", VertexSize -> Large}] manually? I made that mistake when I first typed it. –  Szabolcs Jan 16 '12 at 7:39
    
@Szabolcs Yes. I forgot the braces. (Actually, I didn't know that SetProperty required a list for multiple properties. –  David Carraher Jan 16 '12 at 12:58
add comment

I have encountered a few cases where SetProperty is useless, for example one cannot set GraphLayout (as discovered by Szabolcs here) or VertexLabels, and possibly more:

SetProperty[Graph[{1 -> 2}, VertexLabels -> {1 -> "A"}], VertexLabels -> None]

Mathematica graphics

Solution

I offer a more robust solution that explicitly replaces options. It keeps properties, keeps and updates existing options and adds new options. It might not be the fastest when it comes to rebuilding huge graphs, as it has to extract vertices and edges.

Options[showGraph] = Options@Graph;
showGraph[g_] := g;
showGraph[g_Graph, {new___}] := showGraph[g, new];
showGraph[g_Graph, new : OptionsPattern[]] := Module[{old = Options@g, opts},
   opts = DeleteDuplicates[First /@ Join[old, {new}]];
   Graph[VertexList@g, EdgeList@g, Thread[opts -> (opts /. {new} /. old)]]];

Testing

{g = Graph[{1 -> 2, 2 -> 3, 3 -> 1}, ImageSize -> 120, ImagePadding -> 15, 
   VertexSize -> {2 -> Large}, VertexLabels -> {1 -> "A", 2 -> "B", 3 -> "C"},
    GraphStyle -> "DiagramGold", EdgeStyle -> {(1 -> 2) -> Red}, 
   GraphLayout -> "SpiralEmbedding"],
 showGraph[g, EdgeStyle -> {(1 -> 2) -> Green}, VertexLabels -> None, 
  GraphLayout -> "CircularEmbedding"],
 showGraph[g],
 showGraph[g, VertexLabels -> "Name"]
 }

Mathematica graphics

Unfortunately, something like showGraph[Graph[v_, e_, old_]]:=Graph[v, e, ...] won't work, as:

MatchQ[Graph[{1, 2}, {1 -> 2}], _[_, _, ___]]   (* ==> False *)
MatchQ[graph[{1, 2}, {1 -> 2}], _[_, _, ___]]   (* ==> True  *)

One of the million annoying features of the atomistic representation of Graph-s.

share|improve this answer
add comment

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.