SetProperty[{obj,itemspec},namevalue]
sets the property namevalue for itemspec in obj.
SetProperty
SetProperty[{obj,itemspec},namevalue]
sets the property namevalue for itemspec in obj.
Details
- Properties are used to store and manipulate name->value pairs for specific named items within an object such as Graph.
- SetProperty will produce an object with a modified property value.
- The item specification itemspec typically has the form {collection,index}.
- The index can typically have the following forms:
-
ind a single index {ind1,ind2,…} a list of indexes All all possible indexes in collection patt indexes that match patt in collection - The collection can typically have the following forms:
-
col a single collection {col1,col2,…} a list of collections All all possible collections patt collections that match patt - The item specification itemspec of the form collection refers to the default value for all indexes in the collection.
- The items available for Graph objects include vertices and edges.
Examples
open all close allBasic Examples (2)
Specify a vertex or edge property:
g = CompleteGraph[4]{SetProperty[{g, 1}, VertexLabels -> "one"], SetProperty[{g, 12}, EdgeStyle -> Brown]}Use SetProperty to override the vertex style:
CompleteGraph[4, VertexSize -> Small]SetProperty[{%, 1}, VertexStyle -> Red]SetProperty[{%, 1}, VertexStyle -> Blue]Scope (6)
Basic Uses (2)
SetProperty[{[image], 1}, VertexSize -> Small]SetProperty[{[image], 12}, EdgeLabels -> "hello"]Set several properties for a vertex:
SetProperty[{[image], 1}, {VertexStyle -> Magenta, VertexLabels -> "hello"}]SetProperty[{[image], 12}, {EdgeStyle -> Magenta, EdgeLabels -> "hello"}]Vertex Properties (2)
Standard vertex properties include VertexStyle:
g = CompleteGraph[4, VertexSize -> Medium]SetProperty[{g, 4}, VertexStyle -> Red]SetProperty[{g, 1}, VertexLabels -> "one"]SetProperty[{g, 1}, VertexSize -> Tiny]SetProperty[{g, 1}, VertexShape -> [image]]SetProperty[{g, 2}, VertexShapeFunction -> "Square"]Non-visual properties include VertexWeight:
SetProperty[{g, 1}, VertexWeight -> 100]PropertyValue[{%, 1}, VertexWeight]SetProperty[{[image], 2}, "Material" -> "Wood"]PropertyValue[{%, 2}, "Material"]Edge Properties (2)
Standard edge properties include EdgeStyle:
g = CompleteGraph[4]SetProperty[{g, 12}, EdgeStyle -> Brown]SetProperty[{g, 12}, EdgeLabels -> 12]SetProperty[{g, 34}, EdgeShapeFunction -> "HalfFilledDoubleArrow"]Non-visual properties include EdgeWeight:
SetProperty[{g, 12}, EdgeWeight -> 42]PropertyValue[{%, 12}, EdgeWeight]SetProperty[{[image], 24}, "Resistance" -> 50.]PropertyValue[{%, 24}, "Resistance"]Applications (3)
Use Fold to apply SetProperty repeatedly:
g = CycleGraph[8, VertexSize -> Large]Fold[SetProperty[{#1, #2}, VertexStyle -> ColorData[45, #2]]&, g, Range[1, 5]]FoldList[SetProperty[{#1, #2}, VertexStyle -> ColorData[45, #2]]&, g, Range[1, 6]]g = CompleteGraph[4]Temporarily highlight one vertex:
SetProperty[{g, 1}, VertexSize -> Medium]SetProperty[{g, 2}, VertexSize -> Medium]Highlight the edges visited during a breadth-first scan:
g = CompleteKaryTree[4, 3, DirectedEdges -> True]ListAnimate[Reap[BreadthFirstScan[g, {"FrontierEdge" -> (Sow[SetProperty[{g, #}, EdgeStyle -> Red]]& )}]][[2, 1]], DefaultDuration -> Length[EdgeList[g]] / 2, SaveDefinitions -> True]Properties & Relations (6)
Use Property as a wrapper to specify properties in functions such as Graph:
Graph[{Property[1, {VertexSize -> Medium, VertexStyle -> Red}], 2, 3}, {12, 23, 31}]Use PropertyValue to extract values:
g = CompleteGraph[4, VertexStyle -> {1 -> Red}, VertexSize -> Small]PropertyValue[{g, 1}, VertexStyle]For undefined properties, the value $Failed is returned:
PropertyValue[{g, 1}, "foo"]MemberQ[PropertyList[{g, 1}], "foo"]Use PropertyValue to set properties in a graph:
g = CompleteGraph[4]PropertyValue[{g, 1}, VertexSize] = Small;The original graph has changed:
gUse PropertyValue to unset properties in a graph:
g = CompleteGraph[4, VertexSize -> {1 -> Small}]PropertyValue[{g, 1}, VertexSize]=.;The original graph has changed:
gUse PropertyList to test for the presence of a property:
g = CompleteGraph[4, VertexLabels -> {1 -> one, 4 -> four}]MemberQ[PropertyList[{g, 1}], VertexLabels]Generate a list of vertex label rules:
Cases[VertexList[g], v_ /; MemberQ[PropertyList[{g, v}], VertexLabels] :> v -> PropertyValue[{g, v}, VertexLabels]]Use RemoveProperty to remove a property without changing the original:
g = CompleteGraph[4, VertexStyle -> {1 -> Red}, VertexSize -> Small]RemoveProperty[{g, 1}, VertexStyle]The original graph is unchanged:
gPossible Issues (1)
PropertyValue is typically faster than SetProperty:
g = CycleGraph[1000];Timing@Do[PropertyValue[{g, v}, VertexWeight] = RandomReal[], {v, VertexList[g]}]g = CycleGraph[1000];Timing@Do[g = SetProperty[{g, v}, VertexWeight -> RandomReal[]], {v, VertexList[g]}]See Also
Related Guides
History
Text
Wolfram Research (2010), SetProperty, Wolfram Language function, https://reference.wolfram.com/language/ref/SetProperty.html.
CMS
Wolfram Language. 2010. "SetProperty." Wolfram Language & System Documentation Center. Wolfram Research. https://reference.wolfram.com/language/ref/SetProperty.html.
APA
Wolfram Language. (2010). SetProperty. Wolfram Language & System Documentation Center. Retrieved from https://reference.wolfram.com/language/ref/SetProperty.html
BibTeX
@misc{reference.wolfram_2026_setproperty, author="Wolfram Research", title="{SetProperty}", year="2010", howpublished="\url{https://reference.wolfram.com/language/ref/SetProperty.html}", note=[Accessed: 15-June-2026]}
BibLaTeX
@online{reference.wolfram_2026_setproperty, organization={Wolfram Research}, title={SetProperty}, year={2010}, url={https://reference.wolfram.com/language/ref/SetProperty.html}, note=[Accessed: 15-June-2026]}