主要内容

Graph Plotting and Customization

此示例显示了如何绘制图形,然后自定义显示以添加标签或突出显示到图节点和边缘。

Graph Plotting Objects

利用theplot绘制功能graphdigraph对象。默认,plotexamines the size and type of graph to determine which layout to use. The resulting figure window contains no axes tick marks. However, if you specify the(x,y)coordinates of the nodes with theXData,YData, orZData名称值对,然后图包括轴tick。

Node labels are included automatically in plots of graphs that have 100 or fewer nodes. The node labels use the node names if available; otherwise, the labels are numeric node indices.

For example, create a graph using the buckyball adjacency matrix, and then plot the graph using all of the default options. If you callplot并指定输出参数,然后该函数将句柄返回到一个GraphPlot目的。随后,您可以使用此对象调整图的属性。例如,您可以更改边缘的颜色或样式,节点的大小和颜色等等。

G = graph(bucky); p = plot(G)

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

p = GraphPlot with properties: NodeColor: [0 0.4470 0.7410] MarkerSize: 4 Marker: 'o' EdgeColor: [0 0.4470 0.7410] LineWidth: 0.5000 LineStyle: '-' NodeLabel: {1x60 cell} EdgeLabel: {} XData: [0.1033 1.3374 2.2460 1.3509 0.0019 -1.0591 -2.2901 ... ] YData: [-1.8039 -1.2709 -2.0484 -3.0776 -2.9916 -0.9642 -1.2170 ... ] ZData: [0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 ... ] Show all properties

在您有把握之后GraphPlotobject, use dot indexing to access or change the property values. For a complete list of the properties that you can adjust, seeGraphPlot Properties

Change the value ofNodeColorto“红色”

p.NodeColor =“红色”;

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

确定边缘的线宽度。

P.LineWidth
ans = 0.5000

创建和绘图图

创建和绘制一个代表由具有12个节点侧的正方形网格构建的L形膜的图。指定输出参数plotto return a handle to theGraphPlot目的。

n = 12; A = delsq(numgrid('L',n));g =图(a,“omitselfloops')
G = graph with properties: Edges: [130x2 table] Nodes: [75x0 table]
p = plot(G)

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

p =带有属性的图形图:nodeColor:[0 0.4470 0.7410]标记:4标记:'o'EdgeColor:[0 0.4470 0.7410] lineWidth:0.5000 linestyle:' - 'nodelabel:nodelabel:{1x75 cell} edgelabel} edgelabel:3.5417 3.5684 3.5799 3.5791 3.0286 3.0574 3.0574 3.0811 ...] YDATA:[2.52225 2.1251 1.6498 1.6498 1.1759 0.7827 2.5017 2.5017 2.0929 1.6027 1.6027 ...] 1.6027 ...0 0 0 0 0 0 0 0 ...]显示所有属性

Change Graph Node Layout

利用thelayout函数以更改图中图节点的布局。不同的布局选项会自动计算图的节点坐标。另外,您可以使用XData,YData, 和ZDataproperties of theGraphPlot目的。

Instead of using the default 2-D layout method, uselayout指定'force3'布局,这是3D力的定向布局。

layout(p,'force3') view(3)

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

比例节点着色

Color the graph nodes based on their degree. In this graph, all of the interior nodes have the same maximum degree of 4, nodes along the boundary of the graph have a degree of 3, and the corner nodes have the smallest degree of 2. Store this node coloring data as the variableNodeColorsG.Nodes

G.Nodes.NodeColors = degree(G); p.NodeCData = G.Nodes.NodeColors; colorbar

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

Edge Line Width by Weight

将一些随机的整数重量添加到图形边缘,然后绘制边缘,使它们的线宽度与其重量成正比。由于边缘线的宽度大约大于7开始变得笨重,因此扩展线宽度,以使重量最大的边缘的线宽度为7。将此边缘宽度数据作为变量存储LWidthsG.Edges

G.Edges。Weight = randi([10 250],130,1); G.Edges.LWidths = 7*G.Edges.Weight/max(G.Edges.Weight); p.LineWidth = G.Edges.LWidths;

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

Extract Subgraph

Extract and plot the top right corner ofG作为子图,使阅读图表上的详细信息变得更加容易。新图,H, inherits theNodeColorsLWidthsvariables fromG, so that recreating the previous plot customizations is straightforward. However, the nodes inHare renumbered to account for the new number of nodes in the graph.

H = subgraph(G,[1:31 36:41]); p1 = plot(H,'NodeCData',H.Nodes.NodeColors,'LineWidth',h.edges.lwidths);配色栏

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

标签节点和边缘

利用标签to label the edges whose width is larger than6带有标签,'大的'。这标签表function works in a similar manner for labeling nodes.

标签(p1,find(H.Edges.LWidths > 6),'大的')

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

突出显示最短路径

在子图中找到节点11和节点37之间的最短路径,H。Highlight the edges along this path in red, and increase the size of the end nodes on the path.

路径=shortestpath(H,11,37)
路径=1×1011 12 17 18 19 24 25 30 36 37
突出显示(P1,[11 37])亮点(P1,路径,'EdgeColor','r')

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

Remove the node labels and colorbar, and make all of the nodes black.

p1.nodelabel = {};配色栏离开p1.NodeColor ='black';

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

找到忽略边缘权重的不同最短路径。以绿色突出显示这条路。

路径2 =shortestpath(H,11,37,'Method',“未加权”)
路径2 =1×1011 12 13 14 15 20 25 30 31 37
highlight(p1,path2,'EdgeColor','g')

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

绘制大图

通常创建具有数十万甚至数百万节点和/或边缘的图形。为此原因,plottreats large graphs slightly differently to maintain readability and performance. Theplot当使用具有超过100个节点的图表时,功能会进行这些调整:

  1. 默认图形布局方法始终是“子空间”

  2. 这nodes are no longer labeled automatically.

  3. MarkerSize属性设置为2。(小图形标记的大小4)。

  4. 箭头大小定向图的属性设置为4。(Smaller directed graphs use an arrow size of7)。

See Also

|||

相关话题