Main Content

标签图节点和边缘

This example shows how to add and customize labels on graph nodes and edges.

Create and Plot Graph

创建一个代表城市中网格街道和交叉点的图形。在边缘上增加重量,使主要途径和十字路口在情节中看起来有所不同。绘制与边缘宽度与边缘重量成比例的图形绘制图。

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];权重= [1 5 1 5 5 1 5 1 5 5 1 5 1 5 5 1 1];g =图(s,t,weights);p =图(g,'行宽',g.edges.peight);

图包含一个轴对象。轴对象包含类型图形图的对象。

添加节点标签

对于具有100个或更少节点的图形,MATLAB®会使用数字节点索引或节点名称自动标记节点(默认情况下,较大的图将省略这些标签)。但是,您可以通过调整来更改节点标签节点Labelproperty of the图形图目的p或使用标签表功能。因此,即使节点有名称,您也可以使用与名称不同的标签。

Remove the default numeric node labels. Label one of the intersections as还有另一个工作

标记(p,1:12,'')标签表(P,5,'Home')标签表(P,12,'工作'

图包含一个轴对象。轴对象包含类型图形图的对象。

添加边缘标签

The edges in a plotted graph are不是labeled automatically. You can add edge labels by changing the value of theEdgelabelproperty of the图形图目的p或使用labeledge功能。

Add edge labels for streets in New York City. The order of the edges is defined in theG.Edgestable of the graph, so the order of the labels you specify must respect that order. It is convenient to store edge labels directly in theG.Edges表格,以便边缘名称就在其他边缘信息旁边。

G.Edges
ans =17×2桌末日的重量________ ______ 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 8 1 7 10 5 8 9 1 8 11 5 9 5 9 12 5 10 5 1011 1⋮

This example has 17 edges but only 7 unique street names. Therefore, it makes sense to define the street names in a cell array and then index into the cell array to retrieve the desired street name for each edge. Add a variable to theG.Edges包含街道名称的桌子。

streets = {'8th Ave''7th 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 =街道(inds);G.Edges
ans =17×3桌endnodes重量街道名称________ ______ _____________ 1 2 1 {'8th Ave'} 1 4 5 {'W 20th st'} 2 3 1 {'8th Ave'} 2 5 5 5 5 5 {'W 21st ST'} 3 6 5 {'W 6 5 {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'}⋮

Update theEdgelabel属性以引用这些街道名称。

P.Edgelabel = G.Edges.StreetName;

图包含一个轴对象。轴对象包含类型图形图的对象。

调整字体属性

The node and edge labels in a graph plot have their own properties that control the appearance and style of the labels. Since the properties are decoupled, you can use different styles for the node labels and the edge labels.

为了节点标签,您可以调整:

  • 节点Label

  • Nodelabelcolor

  • NodeFontName

  • 节点FontSize

  • 节点量级

  • 节点FontAngle

为了Edge标签,您可以调整:

  • Edgelabel

  • Edgelabelcolor

  • EdgeFontName

  • EdgeFontSize

  • EdgeFont量级

  • EdgeFontAngle

Use these properties to adjust the fonts in this example with New York City Streets:

  • Change节点FontSizeandNodelabelcolor使得相交标签为12分。字体和红色。

  • ChangeEdgeFont量级,,,,EdgeFontAngle,,,,andEdgeFontSize使用较大的大胆字体来沿一个方向街道和较小的斜体字体,用于另一个方向的街道。

  • ChangeEdgeFontName将新罗马的时间用于边缘标签。

您可以使用highlight改变t的函数he graph properties of a subset of the graph edges. Create a logical indexIsavenue那是true对于包含单词的边缘标签'ave'。使用此逻辑向量作为输入highlight,,,,label all of the Avenues in one way, and all of the non-Avenues another way.

p。节点FontSize = 12; P.NodeLabelColor ='r';isavenue =包含(P.Edgelabel,'ave');突出显示(P,“边缘”,,,,Isavenue,,,,'EdgeFontAngle',,,,“斜体”,,,,“ edgefontsize',7);突出显示(P,“边缘”,,,,~isAvenue,“边缘轻量级”,,,,'大胆的',,,,“ edgefontsize',10);P.EdgeFontName ='英语字体格式一种';

图包含一个轴对象。轴对象包含类型图形图的对象。

突出显示边缘

找到房屋和工作节点之间的最短路径,并检查哪些街道在路径上。突出显示红色路径上的节点和边缘,并删除所有不在路径上的边缘的边缘标签。

[路径,d,路径] =短路(g,5,12)
path =1×45 6 9 12
d = 11
pathEdges =1×38 10 15
g.edges.streetname(pathedges,:)
ans =3x1 cell{'7th Ave' } {'W 22nd St'} {'W 22nd St'}
突出显示(P,“边缘”,路径,'edgeColor',,,,'r')突出显示(P,path,'nodeColor',,,,'r')labeledge(p,setdiff(1:numedges(g),路径),''

图包含一个轴对象。轴对象包含类型图形图的对象。

也可以看看

Related Topics