Mathematica Stack Exchange is a question and answer site for users of Mathematica. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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 Apr 20 '12 at 13:05

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

up vote 15 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

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]


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


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

SetProperty[g, VertexShapeFunction -> "Diamond"]


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. – DavidC 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. – DavidC Jan 16 '12 at 12:58

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


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)]]];


{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, 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

Since version 10, the following works:

Graph[g, VertexLabels -> "Name"]

Here g is already a graph object.

This syntax is important because it also allows converting between Graph and Graph3D, both of which are graphs, but the latter is displayed in 3D by default.

share|improve this answer

Your Answer


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.