-
参见
- 函 数
- TreeGraph
- PathGraph
- AdjacencyGraph
- IncidenceGraph
- GraphData
- Graph3D
-
- 实体类型
- Graph
-
- 诠释器类型
- Graph
- ComputedGraph
-
- 格式
- Graphlet
- DOT
- GraphML
- 相关指南
Graph
更多信息和选项
- Graph[…] 在笔记本中显示为一个图.
- Graph[…] 总是转化为具有结构 Graph[vertices,edges,…] 的优化标准形式.
- Graph 被诸如 AtomQ 的函数,以及模式匹配的情况下,作为原始对象处理.
- u 和 v 之间的一条无向边可以由 uv、u<->v、UndirectedEdge[u,v] 或者 TwoWayRule[u,v] 给出. 字符 可以输入为
ue
. - 从 u 到 v 的标记边可以通过 u
v、u
v、UndirectedEdge[u,v,t] 或 DirectedEdge[u,v,t] 给出. - 从 u 到 v 的一条有向边可以以 uv、u->v、DirectedEdge[u,v] 或者 Rule[u,v] 给出. 字符 可以输入为
de
. - 一个无向图使用一组无向边指定.
- 一个有向图使用一组有向边指定.
- 混合图使用有向边和无向边的集合指定.
- 可以对顶点和边使用以下特殊封装:
-
Annotation[a,label] 提供一个注解 Button[a,action] 定义当元素被点击时执行的操作 EventHandler[a,…] 为元素定义通用事件处理程序 Hyperlink[a,uri] 将元素变成超链接 Labeled[a,…] 显示元素时同时显示标签 PopupWindow[a,cont] 为元素添加弹出窗口 StatusArea[a,label] 鼠标悬停时在状态栏中显示 Style[a,opts] 用指定样式显示元素 Tooltip[a,label] 对元素附加任意工具提示条 - 可能的标签位置分别在 VertexLabels 和 EdgeLabels 中给出.
- Annotation 可用于将顶点和边与注解相关联:
-
Annotation[v,name->value] 将顶点 v 与注解 name->value 相关联 Annotation[e,name->value] 将边 e 与注解 name->value 相关联 - data 可以是下列任何形式之一:
-

类型 "Graph" 的 Entity 
任意 Graph 对象 
Molecule 对象 
Tree 表达式 
DiscreteMarkovProcess 对象 - 对于顶点,支持如下标准属性:
-
VertexLabels 顶点的标签和标签位置 VertexCoordinates 顶点的中心坐标 VertexShape 顶点的形状 VertexSize 顶点的大小 VertexStyle 顶点的样式 VertexShapeFunction 顶点的形状渲染函数 VertexWeight 顶点的权值 - 对于边,支持如下标准属性:
-
EdgeLabels 边的标签和标签位置 EdgeStyle 边的样式 EdgeShapeFunction 边的形状渲染函数 EdgeWeight 边的权值 - Graph 具有和 Graphics 相同的选项,另外有如下添加和变动: [所有选项的列表]
-
AnnotationRules {} 图、边和顶点的注释 DirectedEdges Automatic 是否将 Rule 解释为 DirectedEdge EdgeLabels None 边的标签和标签放置 EdgeLabelStyle Automatic 边的标签所使用的样式 EdgeShapeFunction Automatic 如何生成边的图形表示 EdgeStyle Automatic 边的样式 EdgeWeight Automatic 边的权值 GraphHighlight {} 要突出显示的图的元素 GraphHighlightStyle Automatic 突出显示时使用的样式 GraphLayout Automatic 如何设计顶点和边的布局 PerformanceGoal Automatic 优化的目标 PlotTheme $PlotTheme 图的整体主题样式 VertexCoordinates Automatic 顶点的坐标 VertexLabels None 顶点的标签和位置 VertexLabelStyle Automatic 顶点的标签所使用的样式 VertexShape Automatic 顶点的形状 VertexShapeFunction Automatic 生成顶点的形状的函数 VertexSize Medium 顶点的大小 VertexStyle Automatic 顶点的样式 VertexWeight Automatic 顶点的权值 - PlotTheme 的可能设置包括常见基本主题、强调颜色特征的主题、强调字体特征的主题和强调大小特征的主题.
- 图形的特征主题会影响顶点和边的绘制. 特征主题包括:
-

"LargeGraph" 大图 
"ClassicLabeled" 经典图 
"IndexLabeled" 索引标记图 
"NameLabeled" 名称标记图 - AnnotationRules->{a->{name1->val1,…},…} 将诸如 name1->val1 的属性与作为一个顶点、边或者图的 a 相关联. a 的下列字符串具有特殊含义:
-
"DefaultEdgeProperties" 默认边属性 "DefaultVertexProperties" 默认顶点属性 "GraphProperties" 图自身的属性 - 在设置 VertexCoordinates->Automatic 下,顶点的位置和边的路径基于 GraphLayout 的设置自动计算.
- 边的样式和其它说明实际上以如下顺序应用: PlotTheme、EdgeStyle、GraphHighlightStyle、Style 和其它封装以及 EdgeShapeFunction,其中后面的说明覆盖前面的说明.
- 顶点的样式和其它说明实际上以如下顺序应用: PlotTheme、VertexStyle、 GraphHighlightStyle、Style 和其它封装以及 VertexShapeFunction,其中后面的说明覆盖前面的说明.
- 边标签的标签样式和其它规范以如下顺序应用: PlotTheme、EdgeLabelStyle、GraphHighlightStyle、 Labeled 和 EdgeLabels,其中后面的规范覆盖前面的规范.
- 顶点标签的标签样式和其它规范以如下顺序应用: PlotTheme、VertexLabelStyle、 GraphHighlightStyle、Labeled 和 VertexLabels,其中后面的规范覆盖前面的规范.
所有选项的列表
范例
打开所有单元 关闭所有单元基本范例 (5)
Graph[{12, 23, 31}]Graph[{12, 23, 31}]Graph[{12, 23, 31}, VertexStyle -> Directive[Orange, EdgeForm[Orange]], EdgeStyle -> Green]Graph[{1, 2, Style[3, Red]}, {12, 23, Style[31, Blue]}]Graph[{12, 23, Labeled[31, "hello"]}, VertexLabels -> "Name"]Graph[{12, 23, 31}, VertexShapeFunction -> "Diamond", VertexSize -> Medium]Graph[{12, 23, 31}, EdgeShapeFunction -> "CarvedArcArrow"]范围 (28)
连通度 (9)
Graph[{12, 23, 31}]Graph[{12, 23, 31}]Graph[{1 -> 2, 2 -> 3, 3 -> 1}]Graph[{1 -> 2, 2 -> 3, 3 -> 1}, DirectedEdges -> False]Graph[{1, 2, 3, 4}, {12, 23, 31}]Graph[{}]Through[{VertexCount, EdgeCount}[%]]利用 VertexList 和 EdgeList 获取顶点和边:
{g1, g2} = {Graph[{12, 23, 31}], Graph[{23, 12, 31}]}EdgeList /@ {g1, g2}VertexList /@ {g1, g2}利用显式的顶点列表控制 VertexList 中所用的排序:
{g1, g2} = {Graph[{1, 2, 3}, {23, 12, 31}], Graph[{3, 2, 1}, {23, 12, 31}]}VertexList /@ {g1, g2}{Graph[{12, 22}], Graph[{12, 22}]}{Graph[{ab, bc, ca}], Graph[{"foo""bar", "bar""gnu", "gnu""foo"}],
Graph[{[image][image], [image][image], [image][image]}]}VertexList /@ %封装 (5)
{Graph[{Style[1, Red], 2, 3}, {12, 23, 31}], Graph[{1, 2, 3}, {Style[12, Red], 23, 31}]}{Graph[{1, 2, Labeled[Style[3, Red], "hello"]}, {12, 23, 31}], Graph[{1, 2, 3}, {Style[Labeled[12, "hello"], Red], 23, 31}]}通过诸如 Tooltip 的封装添加交互式行为:
Graph[{12, 23, Tooltip[Style[31, Red], "hello"]}]Graph[{12, 23, Tooltip[Style[31, Red], Plot[Sin[x], {x, 0, 2Pi}]]}]当点击一条边或者一个顶点时,使用 Button 触发行为:
{Graph[{12, 23, Button[Style[31, Red], Speak["Edge from 3 to 1"]]}], Graph[{1, 2, Button[Style[3, Red], Speak["Vertex 3"]]}, {12, 23, 31}, VertexSize -> Medium]}使用 PopupWindow 提供下拉信息:
Graph[{12, 23, PopupWindow[Style[31, Red], DateListPlot[FinancialData["IBM", "Jan. 1, 2004"]]]}]样式 (8)
{Graph[{12, 23, 31}, VertexStyle -> Green], Graph[{12, 23, 31}, EdgeStyle -> Green]}{Graph[{12, 23, 31}, VertexStyle -> {1 -> Green, Orange}], Graph[{12, 23, 31}, EdgeStyle -> {12 -> Green, Orange}]}{Graph[{Style[1, Green], 2, 3}, {12, 23, 31}, VertexStyle -> Orange], Graph[{Style[12, Green], 23, 31}, EdgeStyle -> Orange]}Table[Graph[{12, 23, 31}, VertexSize -> s, PlotLabel -> s], {s, {Tiny, Small, Medium, Large}}]Table[Graph[{12, 23, 31}, VertexSize -> s, PlotLabel -> s], {s, {0.1, 0.2, 0.5, 1}}]使用 VertexShapeFunction 的内置设置集合:
Table[Graph[{12, 23, 31}, VertexShapeFunction -> s, VertexSize -> 0.2, PlotLabel -> s], {s, {"Triangle", "Square", "Rectangle", "Pentagon", "Hexagon", "Octagon"}}]Table[Graph[{12, 23, 31}, VertexShapeFunction -> s, VertexSize -> 0.2, PlotLabel -> Style[s, 9]], {s, ResourceData["VertexShapeFunction", "Rounded"]}]Table[Graph[{12, 23, 31}, VertexShapeFunction -> s, VertexSize -> 0.2, PlotLabel -> s], {s, ResourceData["VertexShapeFunction", "Concave"]}]Graph[{12, 23, 31}, VertexShapeFunction -> { 1 -> "Square"}, VertexSize -> 0.2]Graph[{12, 23, 31}, VertexShapeFunction -> { 1 -> "Square", "Triangle"}, VertexSize -> 0.2]将任意 Graphics、Image 或者 Graphics3D 作为顶点形状使用:
Table[Graph[{12, 23, 31}, VertexShape -> s, VertexSize -> Medium], {s, {[image], [image], [image]}}]使用 EdgeShapeFunction 的内置设置集合:
Table[Graph[{12, 23, 31}, EdgeShapeFunction -> {{ef, "ArrowSize" -> 0.1}}, PlotLabel -> ef], {ef, {"BoxLine", "DiamondLine", "DotLine"}}]Table[Graph[{12, 23, 31}, EdgeShapeFunction -> {{ef, "ArrowSize" -> 0.1}}, PlotLabel -> Style[ef, 9]], {ef, ResourceData["EdgeShapeFunction", "FilledArrow"]}]Table[Graph[{12, 23, 31}, EdgeShapeFunction -> {{ef, "ArrowSize" -> 0.1}}, PlotLabel -> Style[ef, 9]], {ef, ResourceData["EdgeShapeFunction", "UnfilledArrow"]}]Table[Graph[{12, 23, 31}, EdgeShapeFunction -> {{ef, "ArrowSize" -> 0.1}}, PlotLabel -> Style[ef, 9]], {ef, ResourceData["EdgeShapeFunction", "CarvedArrow"]}]Graph[{12, 23, 31}, EdgeShapeFunction -> {12 -> "FilledArrow"}]Graph[{12, 23, 31}, EdgeShapeFunction -> {12 -> "FilledArrow", "CarvedArrow"}]添加标签 (6)
{Graph[{12, 23, Labeled[31, "hello"]}], Graph[{1, 2, Labeled[3, "hello"]}, {12, 23, 31}]}Table[Graph[{1, 2, Labeled[3, l]}, {12, 23, 31}, ImagePadding -> 25], {l, {Sin[x], [image], [image]}}]Table[Graph[{12, 23, Labeled[31, l]}], {l, {Sin[x], [image], [image]}}]利用 Placed 控制顶点标签的位置,包括用符号表示的顶点内的位置:
Table[Graph[{1, 2, Labeled[3, Placed["■■", p]]}, {12, 23, 31}, PlotLabel -> p, VertexSize -> Large, VertexShapeFunction -> "Square"], {p, {Left, Right, Bottom, Top}}]Table[Graph[{1, 2, Labeled[3, Placed["■■", p]]}, {12, 23, 31}, PlotLabel -> p, VertexShapeFunction -> "Square", VertexSize -> Small], {p, {Before, After, Below, Above}}]Table[Graph[{1, 2, Labeled[3, Placed["■", p]]}, {12, 23, 31}, VertexSize -> 0.25, VertexShapeFunction -> "Square", PlotLabel -> p, BaselinePosition -> Axis], {p, {{0, 0}, {1 / 2, 1 / 2}, {1, 1}}}]在封装中使用 Placed 放置多个标签:
Graph[{1, 2, Labeled[3, Placed[{"lbl1", "lbl2"}, {Above, Below}]]}, {12, 23, 31}]Graph[{1, 2, Labeled[3, Placed[{"lbl1", "lbl2", "lbl3", "lbl4"}, {Above, After, Below, Before}]]}, {12, 23, 31}]用 VertexLabels 放置多个标签:
Graph[{12, 23, 31}, VertexLabels -> {3 -> Placed[{"lbl1", "lbl2"}, {Above, Below}]}]将 Placed 与符号式位置合用,控制标签在边上的位置:
Table[Graph[{Labeled[12, Placed["■■", p]], 23, 31}, PlotLabel -> p, VertexSize -> Small], {p, {"Start", "Middle", "End"}}]Table[Graph[{Labeled[12, Placed["■■", p]], 23, 31}, PlotLabel -> p, VertexSize -> Small, BaselinePosition -> Axis], {p, {0, 1 / 4, 1 / 3}}]在封装中使用 Placed 放置多个标签:
Graph[{12, 23, Labeled[31, Placed[{"lbl1", "lbl2"}, {"Start", "End"}]]}]Graph[{12, 23, Labeled[31, Placed[{"lbl1", "lbl2", "lbl3"}, {"Start", "Middle", "End"}]]}]用 EdgeLabels 放置多个标签:
Graph[{12, 23, 31}, EdgeLabels -> {31 -> Placed[{"lbl1", "lbl2"}, {"Start", "End"}]}]选项 (85)
AnnotationRules (2)
DirectedEdges (2)
Graph[{1 -> 2, 2 -> 3, 3 -> 1}]用 DirectedEdges->False 将规则解释为无向边:
Graph[{1 -> 2, 2 -> 3, 3 -> 1}, DirectedEdges -> False]用 DirectedEdge 或 UndirectedEdge 直接指定是有向图还是无向图:
{Graph[{12, 23, 31}], Graph[{12, 23, 31}]}EdgeLabels (7)
Graph[{12, 23, 31}, EdgeLabels -> {12 -> "Hello"}]el = {12, 23, 31};Graph[el, EdgeLabels -> Table[el[[i]] -> Subscript[e, i], {i, Length[el]}]]Graph[{12, 23, 31}, EdgeLabels -> {12 -> [image], 23 -> [image], 31 -> [image]}]将 Placed 与符号位置一起使用,以控制在边的什么位置放置标签:
Table[Graph[{12, 23, 31}, EdgeLabels -> {12 -> Placed["■■■", p]}, PlotLabel -> p], {p, {"Start", "Middle", "End"}}]Table[Graph[{12, 23, 31}, EdgeLabels -> {12 -> Placed["■■■", p]}, PlotLabel -> p, BaselinePosition -> Axis], {p, {0, 1 / 3, 1 / 4}}]Table[Graph[{12, 23, 31}, EdgeLabels -> {13 -> Placed["■■■", {1 / 2, p}]}, PlotLabel -> p, BaselinePosition -> Axis], {p, {{0, 0}, {1 / 2, 1 / 2}, {1, 1}}}]用封装中的 Placed 放置多个标签:
Graph[{12, 23, Labeled[31, Placed[{"lbl1", "lbl2"}, {"Start", "End"}]]}]Graph[{12, 23, Labeled[31, Placed[{"lbl1", "lbl2", "lbl3"}, {"Start", "Middle", "End"}]]}]用 EdgeLabels 放置多个标签:
Graph[{12, 23, 31}, EdgeLabels -> {31 -> Placed[{"lbl1", "lbl2"}, {"Start", "End"}]}]通过 Tooltip 和 StatusArea 用值进行自动标注:
Graph[{12, 23, 31}, EdgeLabels -> Placed["Name", Tooltip]]Graph[{12, 23, 31}, EdgeLabels -> Placed["Name", StatusArea]]EdgeShapeFunction (6)
获取 EdgeShapeFunction 的内置设置列表:
ResourceData["EdgeShapeFunction"]Graph[{12, 23, 31}, EdgeShapeFunction -> "Line"]Table[Graph[{12, 23, 31}, EdgeShapeFunction -> {{ef, "ArrowSize" -> 0.1}}, PlotLabel -> ef], {ef, {"BoxLine", "DiamondLine", "DotLine"}}]Table[Graph[{12, 23, 31}, EdgeShapeFunction -> {{ef, "ArrowSize" -> 0.1}}, PlotLabel -> Style[ef, 10]], {ef, ResourceData["EdgeShapeFunction", "FilledArrow"]}]Table[Graph[{12, 23, 31}, EdgeShapeFunction -> {{ef, "ArrowSize" -> 0.1}}, PlotLabel -> Style[ef, 9]], {ef, ResourceData["EdgeShapeFunction", "UnfilledArrow"]}]Table[Graph[{12, 23, 31}, EdgeShapeFunction -> {{ef, "ArrowSize" -> 0.1}}, PlotLabel -> Style[ef, 10]], {ef, ResourceData["EdgeShapeFunction", "CarvedArrow"]}]Graph[{12, 23, 31}, EdgeShapeFunction -> {12 -> "FilledArrow"}]Graph[{12, 23, 31}, EdgeShapeFunction -> {12 -> "FilledArrow", "CarvedArrow"}]ef[pts_List, e_] :=
Block[{s = 0.015, g = [image]}, {Arrowheads[{{s, 0.33, g}, {s, 0.67, g}}], Arrow[pts]}]Graph[{12, 23, 31}, EdgeShapeFunction -> ef]EdgeShapeFunction 可与 EdgeStyle 组合使用:
Graph[{12, 23, 31}, EdgeStyle -> Blue, EdgeShapeFunction -> (Line[#1]&)]EdgeShapeFunction 比 EdgeStyle 的优先级高:
Graph[{12, 23, 31}, EdgeStyle -> Blue, EdgeShapeFunction -> ({Red, Line[#1]}&)]EdgeStyle (4)
Table[Graph[{12, 23, 31}, EdgeStyle -> style, PlotLabel -> style], {style, {Gray, Dashed, Thick}}]Graph[{12, 23, 31}, EdgeStyle -> {12 -> Blue, 23 -> Dashed}]EdgeStyle 可与 EdgeShapeFunction 组合使用:
ef1[el_, ___] := Arrow[el, 0.1]Graph[{12, 23, 31}, EdgeStyle -> Blue, EdgeShapeFunction -> ef1]EdgeShapeFunction 比 EdgeStyle 的优先级高:
ef2[el_, ___] := {Red, Arrow[el, 0.1]}Graph[{12, 23, 31}, EdgeStyle -> Blue, EdgeShapeFunction -> ef2]Graph[{12, 23, 31}, BaseStyle -> Red, EdgeStyle -> Dashed]Graph[{12, 23, 31}, BaseStyle -> Red, EdgeStyle -> Blue]EdgeWeight (2)
GraphHighlight (3)
Graph[{12, 23, 31}, VertexSize -> Tiny, GraphHighlight -> {1}]Graph[{12, 23, 31}, VertexSize -> Tiny, GraphHighlight -> {23}]Graph[{12, 23, 31}, VertexSize -> Tiny, GraphHighlight -> {1, 2, 13, 23}]GraphHighlightStyle (2)
获取 GraphHighlightStyle 的内置设置的列表:
ResourceData["GraphHighlightStyle"]使用 GraphHighlightStyle 的内置设置:
Graph[{12, 23, 31}, GraphHighlight -> {1, 23}, VertexSize -> Small, GraphHighlightStyle -> #, PlotLabel -> #]& /@ Select[ResourceData["GraphHighlightStyle"], # =!= Automatic&]GraphLayout (5)
Graph[{12, 23, 31}, GraphLayout -> Automatic]Table[Graph[Table[ii + 1, {i, 20}], GraphLayout -> l, PlotLabel -> l], {l, {"CircularEmbedding", "SpiralEmbedding"}}]el = EdgeList@GridGraph[{10, 10}];Table[
Graph[el, GraphLayout -> l, PlotLabel -> Style[l, 10]], {l, {"SpringEmbedding", "SpringElectricalEmbedding", "HighDimensionalEmbedding"}}]VertexCoordinates 将覆盖 GraphLayout 的坐标:
{Graph[{12, 23, 31}, GraphLayout -> "SpringElectricalEmbedding"],
Graph[{12, 23, 31}, GraphLayout -> "SpringElectricalEmbedding", VertexCoordinates -> Table[{i, i}, {i, 0, 2}]]}用 AbsoluteOptions 提取用布局算法得出的 VertexCoordinates:
Graph[{12, 23, 31}]AbsoluteOptions[%, VertexCoordinates]PlotTheme (4)
基本主题 (2)
VertexCoordinates (3)
Graph[{12, 23, 31}]用 AbsoluteOptions 提取所得的顶点坐标:
AbsoluteOptions[%, VertexCoordinates]ellipseLayout[n_, {a_, b_}] := Table[{a Cos[2Pi / n u], b Sin[2Pi / n u]}, {u, 1, n}]Graphics[Point[ellipseLayout[20, {2, 1}]]]Graph[Table[iMod[i + 1, 20, 1], {i, 20}], VertexCoordinates -> ellipseLayout[20, {2, 1}]]VertexCoordinates 比 GraphLayout 的优先级高:
Graph[{12, 23, 31}, VertexCoordinates -> Table[{i, i}, {i, 3}], GraphLayout -> "CircularEmbedding"]VertexLabels (14)
Graph[{12, 23, 31}, VertexLabels -> "Name"]Graph[{12, 23, 31}, VertexLabels -> {2 -> "one"}]Graph[{12, 23, 31}, VertexLabels -> Table[i -> Subscript[v, i], {i, 3}]]Graph[{12, 23, 31}, VertexLabels -> {1 -> [image], 2 -> [image], 3 -> [image]}, ImagePadding -> 30]将 Placed 与符号式位置合用,控制标签的放置,包括顶点外的位置:
Table[Graph[{12, 23, 31}, VertexSize -> 0.1, VertexShapeFunction -> "Square", VertexLabels -> Table[i -> Placed["■", p], {i, 3}], PlotLabel -> p], {p, {Before, After, Below, Above}}]pl = {{Before, Below}, {After, Below}, {Before, Above}, {After, Above}};Table[Graph[{12, 23, 31}, VertexSize -> 0.1, VertexShapeFunction -> "Square", VertexLabels -> Table[i -> Placed["■", p], {i, 3}], PlotLabel -> p], {p, pl}]Table[Graph[{12, 23, 31}, VertexSize -> 0.25, VertexLabels -> Table[i -> Placed[[image], p], {i, 3}], VertexShapeFunction -> "Square", PlotLabel -> p], {p, {Left, Top, Right, Bottom}}]pl = {{Left, Bottom}, {Right, Bottom}, {Left, Top}, {Right, Top}};Table[Graph[{12, 23, 31}, VertexSize -> 0.25, VertexShapeFunction -> "Square", VertexLabels -> Table[i -> Placed[[image], p], {i, 3}], PlotLabel -> p], {p, pl}]Table[Graph[{12, 23, 31}, VertexSize -> 0.25, VertexShapeFunction -> "Square", VertexLabels -> Table[i -> Placed[[image], p], {i, 3}], PlotLabel -> p, BaselinePosition -> Axis], {p, {{0, 0}, {1 / 2, 1 / 2}, {1, 1}}}]Table[Graph[{12, 23, 31}, VertexSize -> 0.35, VertexShapeFunction -> "Square", VertexLabels -> Table[i -> Placed[[image], {{1, 1}, p}], {i, 3}], PlotLabel -> p, BaselinePosition -> Axis], {p, {{0, 0}, {1 / 2, 1 / 2}, {1, 1}}}]Graph[{12, 23, 31}, VertexSize -> 0.35, VertexLabels -> Placed[[image], Center], VertexShapeFunction -> "Square"]在封装中使用 Placed 放置多个标签:
Graph[{1, 2, Labeled[3, Placed[{"lbl1", "lbl2"}, {Above, Below}]]}, {12, 23, 31}]Graph[{1, 2, Labeled[3, Placed[{"lbl1", "lbl2", "lbl3", "lbl4"}, {Above, After, Below, Before}]]}, {12, 23, 31}]用 VertexLabels 放置多个标签:
Graph[{12, 23, 31}, VertexLabels -> {3 -> Placed[{"lbl1", "lbl2"}, {Above, Below}]}]Graph[{12, 23, 31}, VertexLabels -> Placed["Name", Tooltip]]或 StatusArea:
Graph[{12, 23, 31}, VertexLabels -> Placed["Name", StatusArea]]rotateLabel[lbl_] := Rotate[lbl, 45Degree]Graph[{12, 23, 31}, VertexLabels -> Table[i -> Placed["xxx", Below, rotateLabel], {i, 3}]]panelLabel[lbl_] := Panel[lbl, FrameMargins -> 0, Background -> StandardGray]Graph[{12, 23, 31}, VertexLabels -> Table[i -> Placed["xxx", Center, panelLabel], {i, 3}]]hyperlinkLabel[lbl_] := Hyperlink[lbl, "http://www.wolfram.com"]Graph[{12, 23, 31}, VertexLabels -> Table[i -> Placed["xxx", Center, hyperlinkLabel], {i, 3}]]VertexShape (5)
使用任意 Graphics、Image 或 Graphics3D 作为顶点的形状:
Table[Graph[{12, 23, 31}, VertexShape -> s, VertexSize -> Medium], {s, {[image], [image], [image]}}]Graph[{12, 23, 31}, VertexShape -> {2 -> [image]}, VertexSize -> Medium]VertexShape 可与 VertexSize 组合使用:
Table[Graph[{12, 23, 31}, VertexSize -> s, VertexShape -> [image], PlotLabel -> s], {s, {Small, Large}}]VertexShape 不受 VertexStyle 的影响:
Graph[{12, 23, 31}, VertexSize -> 0.2, VertexShape -> [image], VertexStyle -> Blue]VertexShapeFunction 比 VertexShape 的优先级高:
Graph[{12, 23, 31}, VertexSize -> 0.1, VertexShapeFunction -> "Square", VertexShape -> [image]]VertexShapeFunction (11)
获取 VertexShapeFunction 的内置集合列表:
ResourceData["VertexShapeFunction"]使用 "Basic" 集合中 VertexShapeFunction 的内置设置:
ResourceData["VertexShapeFunction", "Basic"]Table[Graph[{12, 23, 31}, VertexShapeFunction -> vf, VertexSize -> 0.2, PlotLabel -> vf], {vf, {"Triangle", "Square", "Rectangle", "Pentagon", "Hexagon", "Octagon"}}]Table[Graph[{12, 23, 31}, VertexShapeFunction -> vf, VertexSize -> 0.2, PlotLabel -> vf], {vf, {"DownTrapezoid", "UpTrapezoid", "Parallelogram", "FiveDown", "Circle", "Diamond", "Star", "Capsule"}}]使用 "Rounded" 集合中 VertexShapeFunction 的内置设置:
ResourceData["VertexShapeFunction", "Rounded"]Table[Graph[{12, 23, 31}, VertexShapeFunction -> vf, VertexSize -> 0.2, PlotLabel -> Style[vf, 9]], {vf, ResourceData["VertexShapeFunction", "Rounded"]}]使用 "Concave" 集合中 VertexShapeFunction 的内置设置:
ResourceData["VertexShapeFunction", "Concave"]Table[Graph[{12, 23, 31}, VertexShapeFunction -> vf, VertexSize -> 0.2, PlotLabel -> vf], {vf, ResourceData["VertexShapeFunction", "Concave"]}]Graph[{12, 23, 31}, VertexShapeFunction -> { 1 -> "Square"}, VertexSize -> 0.2]Graph[{12, 23, 31}, VertexShapeFunction -> { 1 -> "Square", "Triangle"}, VertexSize -> 0.2]Graph[{12, 23, 31}, VertexShapeFunction -> (Inset[[image], #]&)]vf[{xc_, yc_}, name_, {w_, h_}] :=
Block[{xmin = xc - w, xmax = xc + w, ymin = yc - h, ymax = yc + h},
Polygon[{{xmin, ymin}, {xmax, ymax}, {xmin, ymax}, {xmax, ymin}}]
];Graph[{12, 23, 31}, VertexShapeFunction -> vf, VertexSize -> 0.2]VertexShapeFunction 可与 VertexStyle 组合使用:
vf1[{xc_, yc_}, name_, {w_, h_}] := Rectangle[{xc - w, yc - h}, {xc + w, yc + h}]Graph[{12, 23, 31}, VertexSize -> 0.2, VertexStyle -> Blue, VertexShapeFunction -> vf1]VertexShapeFunction 比 VertexStyle 的优先级高:
vf2[{xc_, yc_}, name_, {w_, h_}] := {Red, Rectangle[{xc - w, yc - h}, {xc + w, yc + h}]}Graph[{12, 23, 31}, VertexSize -> 0.2, VertexStyle -> Blue, VertexShapeFunction -> vf2]VertexShapeFunction 可与 VertexSize 组合使用:
Graph[{12, 23, 31}, VertexShapeFunction -> "Star", VertexSize -> {1 -> Small, Medium}]VertexShapeFunction 比 VertexShape 的优先级高:
Graph[{12, 23, 31}, VertexSize -> 0.3, VertexShapeFunction -> "Star", VertexShape -> [image]]VertexSize (8)
Graph[{12, 23, 31}, VertexSize -> Automatic]Table[Graph[{12, 23, 31}, VertexSize -> s, PlotLabel -> s], {s, {Tiny, Small, Medium, Large}}]Table[Graph[{12, 23, 31}, VertexSize -> s, PlotLabel -> s], {s, 0.1, 1, 0.3}]Table[Graph[{12, 23, 31}, VertexSize -> {"Scaled", s}, PlotLabel -> {"Scaled", s}], {s, 0.1, 1, 0.3}]Table[Graph[{12, 23, 31}, VertexSize -> s, PlotLabel -> s], {s, {{0.1, 0.2}, {0.2, 0.1}}}]Graph[{12, 23, 31}, VertexSize -> {1 -> 0.2, 2 -> 0.3}]VertexSize 可与 VertexShapeFunction 组合使用:
Table[Graph[{12, 23, 31}, VertexSize -> s, VertexShapeFunction -> "Square", PlotLabel -> s], {s, {0.05, 0.1, 0.2}}]VertexSize 可与 VertexShape 组合使用:
Table[Graph[{12, 23, 31}, VertexSize -> s, VertexShape -> [image], PlotLabel -> s], {s, {0.1, 0.2, 0.4}}]VertexStyle (5)
Table[Graph[{12, 23, 31}, VertexStyle -> style, VertexSize -> 0.2], {style, {Yellow, EdgeForm[Dashed]}}]Graph[{12, 23, 31}, VertexStyle -> {1 -> Blue, 2 -> Red}, VertexSize -> 0.2]VertexShapeFunction 可与 VertexStyle 组合使用:
vf1[{xc_, yc_}, name_, {w_, h_}] := Rectangle[{xc - w, yc - h}, {xc + w, yc + h}]Graph[{12, 23, 31}, VertexSize -> 0.2, VertexStyle -> Blue, VertexShapeFunction -> vf1]VertexShapeFunction 比 VertexStyle 的优先级高:
vf2[{xc_, yc_}, name_, {w_, h_}] := {Red, Rectangle[{xc - w, yc - h}, {xc + w, yc + h}]}Graph[{12, 23, 31}, VertexSize -> 0.2, VertexStyle -> Blue, VertexShapeFunction -> vf2]VertexStyle 可与 BaseStyle 组合使用:
Graph[{12, 23, 31}, VertexStyle -> LightBlue, BaseStyle -> EdgeForm[Dotted], VertexSize -> 0.2]VertexStyle 比 BaseStyle 的优先级高:
Graph[{12, 23, 31}, VertexStyle -> LightBlue, BaseStyle -> Gray, VertexSize -> 0.2]VertexShape 不受 VertexStyle 的影响:
Graph[{12, 23, 31}, VertexSize -> 0.2, VertexShape -> [image], VertexStyle -> Blue]VertexWeight (2)
应用 (4)
Graph[Table[i -> Mod[i^2, 74], {i, 100}]]Graph[Thread[Range[20] -> RandomSample[Range[20]]]]利用 Table 对多个项设置相似的注释:
Graph[Table[Annotation[v, {VertexSize -> 0.2 + 0.2Mod[v, 5], VertexStyle -> Hue[(v/15), 1, 1]}], {v, 0, 14}], Table[vMod[v + 1, 15], {v, 0, 14}]]words = DictionaryLookup["wol*"];Flatten[Map[(Thread[#DeleteCases[Nearest[words, #, 3], #]])&, words]];Graph[%, VertexLabels -> "Name", ImageSize -> 450]属性和关系 (3)
用 VertexCount 和 EdgeCount 计算顶点数和边数:
g = Graph[Table[iMod[i + 1, 2], {i, 20}]]{VertexCount[g], EdgeCount[g]}用 VertexList 和 EdgeList 以标准顺序列出顶点和边:
g = Graph[{12, 23, 31}];{VertexList[g], EdgeList[g]}g1 = Graph[{23, 31, 12}];{VertexList[g1], EdgeList[g1]}g2 = Graph[{31, 23, 12}];{VertexList[g2], EdgeList[g2]}从一个图计算 AdjacencyMatrix:
g = Graph[{ab, bc, ca}];(m = AdjacencyMatrix[g]//Normal)//MatrixForm行-列排序由 VertexList 给出:
VertexList[g]Position[m, 1, {2}]可能存在的问题 (2)
Graph 对象是原子原生对象:
g = Graph[{1 -> 2, 2 -> 3, 3 -> 1}];AtomQ[g]使用 GraphQ 检验它是否为图:
MatchQ[g, _ ? GraphQ]平行的边在 Graph 中是无法区分的:
Graph[{Style[12, Red], Style[12, Blue], 23, 31}]使用 EdgeTaggedGraph 为每条边指定一个唯一的标签:
EdgeTaggedGraph[{Style[12, Red], Style[12, Blue], 23, 31}]EdgeList[%]参见
TreeGraph PathGraph AdjacencyGraph IncidenceGraph GraphData Graph3D
实体类型: Graph
诠释器类型: Graph ComputedGraph
相关指南
-
▪
- 图的构建与表示 ▪
- 图的可视化 ▪
- 图与网络 ▪
- 离散数学 ▪
- 社交网络分析 ▪
- 图布局 ▪
- WDF(Wolfram 数据框架) ▪
- 生命科学和医学:数据与计算 ▪
- 科学模型 ▪
- 分子结构与计算 ▪
- 科学数据分析 ▪
- 数据库连接 ▪
- Wolfram 数据知识库
相关链接
文本
Wolfram Research (2010),Graph,Wolfram 语言函数,https://reference.wolfram.com/language/ref/Graph.html (更新于 2022 年).
CMS
Wolfram 语言. 2010. "Graph." Wolfram 语言与系统参考资料中心. Wolfram Research. 最新版本 2022. https://reference.wolfram.com/language/ref/Graph.html.
APA
Wolfram 语言. (2010). Graph. Wolfram 语言与系统参考资料中心. 追溯自 https://reference.wolfram.com/language/ref/Graph.html 年
BibTeX
@misc{reference.wolfram_2026_graph, author="Wolfram Research", title="{Graph}", year="2022", howpublished="\url{https://reference.wolfram.com/language/ref/Graph.html}", note=[Accessed: 02-November-2026]}
BibLaTeX
@online{reference.wolfram_2026_graph, organization={Wolfram Research}, title={Graph}, year={2022}, url={https://reference.wolfram.com/language/ref/Graph.html}, note=[Accessed: 02-November-2026]}