图绘图和自定义
This example shows how to plot graphs, and then customize the display to add labels or highlighting to the graph nodes and edges.
图绘制对象
使用阴谋
function to plot图形
andDigraph
objects. By default,阴谋
检查图的大小和类型以确定要使用的布局。最终的图形窗口没有轴勾号标记。但是,如果您指定(x,y)节点的坐标XDATA
,,,,YDATA
, 或者ZDATA
name-value pairs, then the figure includes axes ticks.
节点标签自动包含在具有100个或更少节点的图形图中。节点标签如果可用,请使用节点名称;否则,标签是数字节点索引。
例如,使用Buckyball邻接矩阵创建图形,然后使用所有默认选项绘制图形。如果您打电话阴谋
and specify an output argument, then the function returns a handle to a图形图
object. Subsequently, you can use this object to adjust properties of the plot. For example, you can change the color or style of the edges, the size and color of the nodes, and so on.
g =图(Bucky);p =图(g)
p =带有属性的图形图:nodeColor:[0 0.4470 0.7410]标记:4标记:'o'EdgeColor:[0 0.4470 0.7410] linewidth:0.5000 linestyle:' - 'nodelabel:nodelabel:{1x60 cell} edgelabel:{1x60 cell} edgelabel:{} xdata。1.3374 2.2460 1.3509 0.0019 -1.0591 -2.2901 ...] YDATA:[-1.8039 -1.2709 -2.0484 -3.0776 -2.9916 -0.96420 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 ...]显示所有属性
After you have a handle to the图形图
对象,使用点索引访问或更改属性值。有关可以调整的属性的完整列表,请参阅图形图属性。
改变NodeColor
到'红色的'
。
p.nodecolor ='红色的';
Determine the line width of the edges.
p.LineWidth
ANS = 0.5000
Create and Plot Graph
Create and plot a graph representing an L-shaped membrane constructed from a square grid with a side of 12 nodes. Specify an output argument with阴谋
将手柄返回到图形图
object.
n = 12;a = delsq(numgrid('L',,,,n)); G = graph(A,“省略自助”)
g =带有属性的图形:边缘:[130x2表]节点:[75x0表]
p =图(g)
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: {1x75 cell} EdgeLabel: {} XData: [3.5040 3.5417 3.5684 3.5799 3.5791 3.0286 3.0574 3.0811 ... ] YData: [2.5225 2.1251 1.6498 1.1759 0.7827 2.5017 2.0929 1.6027 ... ] 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
更改图形节点布局
使用布局
function to change the layout of the graph nodes in the plot. The different layout options automatically compute node coordinates for the plot. Alternatively, you can specify your own node coordinates with theXDATA
,,,,YDATA
,,,,andZDATA
属性图形图
object.
而不是使用默认的2-D布局方法,请使用布局
到specify the'force3'
布局,,,,which is a 3-D force directed layout.
布局(P,'force3')查看(3)
Proportional Node Coloring
根据其度量为图节点涂色。在此图中,所有内部节点具有相同的最大程度为4,沿图的边界的节点为3度,角节点的最小度为2。将此节点着色数据存储为变量。NodeColors
inG.Nodes
。
g.nodes.nodecolors =度(g);p.nodecdata = g.nodes.nodecolors;配色栏
边缘宽度按重量
Add some random integer weights to the graph edges, and then plot the edges such that their line width is proportional to their weight. Since an edge line width approximately greater than 7 starts to become cumbersome, scale the line widths such that the edge with the greatest weight has a line width of 7. Store this edge width data as the variableLWIDTHS
inG。Edges
。
G.Edges.Weight = Randi([10 250],130,1);g.edges.lwidths = 7*g.edges.peage/max(g.edges.awert);p.linewidth = g.edges.lwidths;
提取子图
提取和绘制右上角G
as a subgraph, to make it easier to read the details on the graph. The new graph,H
,继承NodeColors
andLWIDTHS
来自G
,使得重新创建先前的图自定义很简单。但是,节点在H
重新编号以说明图中的新节点数量。
h =子图(G,[1:31 36:41]);p1 =图(h,'nodecdata',h.nodes.nodecolors,'行宽',,,,H。Edges.LWidths); colorbar
Label Nodes and Edges
Uselabeledge
标记宽度大于6
with the label,“大”
。这labelnode
功能以类似的方式用于标记节点。
labeledge(p1,find(h.edges.lwidths> 6),“大”)
Highlight Shortest Path
Find the shortest path between node 11 and node 37 in the subgraph,H
。突出显示沿着红色路径的边缘,并增加路径上的末端节点的大小。
路径=短路(H,11,37)
path =1×1011 12 17 18 19 24 25 30 36 37
highlight(p1,[11 37]) highlight(p1,path,'edgeColor',,,,'r')
卸下节点标签和配色栏,并使所有节点黑色。
p1.NodeLabel = {}; colorbaroffp1.nodecolor ='黑色的';
Find a different shortest path that ignores the edge weights. Highlight this path in green.
path2 =短路(h,11,37,'方法',,,,“减重”)
path2 =1×1011 12 13 14 15 20 25 30 31 37
突出显示(P1,Path2,'edgeColor',,,,'G')
Plotting Large Graphs
It is common to create graphs that have hundreds of thousands, or even millions, of nodes and/or edges. For this reason,阴谋
略有不同的图表以保持可读性和性能。这阴谋
function makes these adjustments when working with graphs that have more than 100 nodes:
这default graph layout method is always
'subspace'
。节点不再自动标记。
这
标记
property is set to2
。(较小的图的标记大小为4
)。这
ArrowSize
property of directed graphs is set to4
。(较小的定向图使用箭头大小7
)。