主要内容

Label Graph Nodes and Edges

此示例显示了如何在图节点和边缘上添加和自定义标签。

创建和绘图图

Create a graph representing the gridded streets and intersections in a city. Add weights to the edges so that the main avenues and cross streets appear differently in the plot. Plot the graph with the edge line widths proportional to the weight of the edge.

s = [1 1 2 2 3 4 4 5 5 6 7 7 8 8 9 10 11]; t = [2 4 3 5 6 5 7 6 8 9 8 10 9 11 12 11 12]; weights = [1 5 1 5 5 1 5 1 5 5 1 5 1 5 5 1 1]; G = graph(s,t,weights); P = plot(G,'LineWidth',G.Edges.Weight);

Figure contains an axes object. The axes object contains an object of type graphplot.

Add Node Labels

For graphs with 100 or fewer nodes, MATLAB® automatically labels the nodes using the numeric node indices or node names (larger graphs omit these labels by default). However, you can change the node labels by adjusting theNodelabel属性GraphPlotobjectPor by using thelabelnodefunction. Therefore, even if the nodes have names, you can use labels that are different from the names.

删除默认数字节点标签。将其中一个交叉点标记为Home和another asWork.

labelnode(P,1:12,'')标记(p,5,'家')标记(p,12,'Work')

Figure contains an axes object. The axes object contains an object of type graphplot.

Add Edge Labels

绘制图中的边缘是not自动标记。您可以通过更改的值来添加边缘标签边缘Label属性GraphPlotobjectPor by using the标签function.

在纽约市添加边缘标签。边缘的顺序在G.Edges图表的表,因此您指定的标签的顺序必须尊重该顺序。很方便将边缘标签直接存储在G.Edgestable, so that the edge name lives right next to the other edge information.

G.Edges
ans=17×2 tableEndNodes Weight ________ ______ 1 2 1 1 4 5 2 3 1 2 5 5 3 6 5 4 5 1 4 7 5 5 6 1 5 8 5 6 9 5 7 8 1 7 10 5 8 9 1 8 11 5 9 12 5 10 11 1 ⋮

这个示例有17个边缘,但只有7个独特的街道名称。因此,在单元格数组中定义街道名称,然后索引到单元格数组以检索每个边缘所需的街道名称是有意义的。在G.Edgestable containing the street names.

街道= {'8th Ave''第七大道''6th Ave''5th Ave'...'W 20th st''W 21st St''W 22nd St'}'; inds = [1 5 1 6 7 2 5 2 6 7 3 5 3 6 7 4 4]; G.Edges.StreetName = streets(inds); G.Edges
ans=17×3 tableEndNodes Weight StreetName ________ ______ _____________ 1 2 1 {'8th Ave' } 1 4 5 {'W 20th St'} 2 3 1 {'8th Ave' } 2 5 5 {'W 21st St'} 3 6 5 {'W 22nd St'} 4 5 1 {'7th Ave' } 4 7 5 {'W 20th St'} 5 6 1 {'7th Ave' } 5 8 5 {'W 21st St'} 6 9 5 {'W 22nd St'} 7 8 1 {'6th Ave' } 7 10 5 {'W 20th St'} 8 9 1 {'6th Ave' } 8 11 5 {'W 21st St'} 9 12 5 {'W 22nd St'} 10 11 1 {'5th Ave' } ⋮

更新边缘Labelproperty to reference these street names.

P.EdgeLabel = G.Edges.StreetName;

Figure contains an axes object. The axes object contains an object of type graphplot.

Adjust Font Properties

图图中的节点和边缘标签具有自己的属性,可以控制标签的外观和样式。由于属性是解耦的,因此您可以使用不同样式的节点标签和边缘标签。

For节点labels, you can adjust:

  • Nodelabel

  • NodelabelColor

  • 节点FontName

  • nodefontsize

  • 节点FontWeight

  • NodeFontangle

For边缘labels, you can adjust:

  • 边缘Label

  • 边缘LabelColor

  • 边缘FontName

  • EdgeFontSize

  • 边缘FontWeight

  • edgeFontangle

使用这些属性在纽约市街道的示例中调整字体:

  • 改变nodefontsizeNodelabelColorso that the intersection labels are 12 pt. font and red.

  • 改变边缘FontWeight,edgeFontangle, 和EdgeFontSizeto use a larger, bold font for streets in one direction and a smaller, italic font for streets in the other direction.

  • 改变边缘FontNameto use Times New Roman for the edge labels.

You can use the强调更改图表子集的图形属性的函数。创建逻辑索引isAvenuethat is真的for edge labels containing the word'Ave'. Using this logical vector as an input to强调,以一种方式标记所有途径,并以另一种方式标记所有非避难所。

p.nodefontsize = 12;p.nodelabelcolor ='r';isAvenue =包含(P。EdgeLabel,'Ave');突出显示(P,'Edges',Isavenue,“ edgefontangle”,'italic','EdgeFontSize', 7); highlight(P,'Edges',〜iSavenue,'EdgeFontWeight','bold','EdgeFontSize', 10); P.EdgeFontName ='Times New Roman';

Figure contains an axes object. The axes object contains an object of type graphplot.

Highlight Edges

Find the shortest path between the Home and Work nodes and examine which streets are on the path. Highlight the nodes and edges on the path in red and remove the edge labels for all edges that are not on the path.

[path,d,pathEdges] = shortestpath(G,5,12)
路径=1×45 6 9 12
d = 11
路径=1×38 10 15
G.Edges.StreetName(pathEdges,:)
ans =3x1单元{'7th Ave'} {'W 22nd ST'} {'W 22nd ST'}
突出显示(P,'Edges',pathEdges,'EdgeColor','r')突出显示(P,路径,'NodeColor','r') labeledge(P, setdiff(1:numedges(G), pathEdges),'')

Figure contains an axes object. The axes object contains an object of type graphplot.

See Also

相关话题