Main Content

GraphPlot

有向图和无向图的图论图

说明

图论图是以可视化形式呈现使用 graphdigraph 函数创建的图和网络的主要方法。创建 GraphPlot 对象后,可以通过更改其属性值修改该绘图的各个方面。这对于修改图节点或边的显示特别有用。

创建对象

要创建 GraphPlot 对象,请指定一个输出参数和 plot 函数。例如:

G = graph([1 1 1 1 5 5 5 5],[2 3 4 5 6 7 8 9]);
h = plot(G)

属性

GraphPlot 属性图论图的外观和行为

对象函数

layout更改图论图布局
highlight突出显示绘制的图中的节点和边
labelnode为图节点添加标签
labeledge为图边添加标签

示例

全部折叠

创建一个 GraphPlot 对象,然后说明如何调整该对象的属性来影响输出显示。

创建并绘制一个图。

s = [1 1 1 1 1 1 1 9 9 9 9 9 9 9];
t = [2 3 4 5 6 7 8 2 3 4 5 6 7 8];
G = graph(s,t);
h = plot(G)

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

h = 
  GraphPlot with properties:

     NodeColor: [0 0.4470 0.7410]
    MarkerSize: 4
        Marker: 'o'
     EdgeColor: [0 0.4470 0.7410]
     LineWidth: 0.5000
     LineStyle: '-'
     NodeLabel: {'1'  '2'  '3'  '4'  '5'  '6'  '7'  '8'  '9'}
     EdgeLabel: {}
         XData: [-0.0552 -0.5371 1.4267 -0.4707 -2.0048 -1.9560 2.1807 1.3586 0.0577]
         YData: [-0.3011 -2.1306 1.6662 2.1447 -0.8743 0.9689 -0.0560 -1.7169 0.2991]
         ZData: [0 0 0 0 0 0 0 0 0]

  Use GET to show all properties

对图节点使用自定义节点坐标。

h.XData = [0 -3 -2 -1 0 1 2 3 0];
h.YData = [2 0 0 0 0 0 0 0 -2];

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

将图节点设置为红色。

h.NodeColor = 'r';

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

对图边使用虚线。

h.LineStyle = '--';

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

增加节点的大小。

h.MarkerSize = 8;

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

使用 savefig 函数保存图的图论图图窗。

s = [1 1 1 2 2 3 3 4 5 5 6 7];
t = [2 4 5 3 6 4 7 8 6 8 7 8];
G = graph(s,t);
plot(G);
savefig('cubegraph.fig');
clear s t G
close gcf

使用 openfig 重新将图的图论图图窗加载到 MATLAB® 中。openfig 还返回图窗的句柄 y

y = openfig('cubegraph.fig');

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

使用 findobj 函数利用一个属性值找到正确的对象句柄。通过使用 findobj,您可以继续操作用于生成图窗的原始 GraphPlot 对象。

h = findobj('Marker','o')
h = 
  GraphPlot with properties:

     NodeColor: [0 0.4470 0.7410]
    MarkerSize: 4
        Marker: 'o'
     EdgeColor: [0 0.4470 0.7410]
     LineWidth: 0.5000
     LineStyle: '-'
     NodeLabel: {'1'  '2'  '3'  '4'  '5'  '6'  '7'  '8'}
     EdgeLabel: {}
         XData: [-0.0495 -0.0119 -1.5285 -1.5694 1.5285 1.5694 0.0495 0.0119]
         YData: [-2.0789 -0.5215 0.8917 -0.8184 -0.8917 0.8184 2.0789 0.5215]
         ZData: [0 0 0 0 0 0 0 0]

  Use GET to show all properties

版本历史记录

在 R2015b 中推出

全部展开