-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPlotting_Syntax.m
More file actions
102 lines (77 loc) · 2.49 KB
/
Copy pathPlotting_Syntax.m
File metadata and controls
102 lines (77 loc) · 2.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
%%语法
% 创建数据
x = 0 : 0.1 : 2 * pi;%起始值:步长:终止值
y = sin(x);
% 1.绘制基本图形
plot(x, y);
title('正弦曲线'); % 添加标题
xlabel('x轴'); % x轴标签
ylabel('y轴'); % y轴标签
grid on; % 显示网格
% 2.多条曲线绘制
x = 0 : 0.1 : 2 * pi;
y1 = sin(x);
y2 = cos(x);
% 绘制两条曲线,指定颜色和线型
plot(x, y1, 'r-', x, y2, 'b--');
legend('sin(x)', 'cos(x)'); % 添加图例
% 3.子图绘制
x = 0 : 0.1 : 2 * pi;
y1 = sin(x);
y2 = cos(x);
y3 = sin(x) + cos(x);
subplot(2, 2, 1); % 2行2列,第1个子图
plot(x, y1);
title('sin(x)');
subplot(2, 2, 2); % 2行2列,第2个子图
plot(x, y2);
title('cos(x)');
subplot(2, 2, 3); % 2行2列,第3个子图
plot(x, y3);
title('sin(x)+cos(x)');
% 4. 绘制数字标号的图
% 定义边和权重
S = [1, 1, 2, 3]; % 起点,下标从1开始
T = [2, 3, 3, 4]; % terminal
W = [5, 3, 1, 2]; % 边的权重
% 顶点名称
Names = {'A', 'B', 'C', 'D'};
% 创建带权重的无向图
G = graph(S, T, W, Names); %顶点名称
G2 = graph([1,2],[2,3], [10,20]);
% 可视化(显示权重)
plot(G, 'EdgeLabel', G.Edges.Weight);%标签名唯一固定
%图对象的主要属性
% 创建G = graph(...) 后,可通过以下属性访问图的信息:
% G.Nodes:顶点信息(包含 Name 等属性)
% G.Edges:边信息(包含 EndNodes 起点终点、Weight 权重等)
% G.NumNodes:顶点数量
% G.NumEdges:边数量
%常见标签
% 'EdgeLabel':边的标签内容(如权重、自定义文本)
plot(G, 'EdgeLabel', G.Edges.Weight)
% 'EdgeColor':边的颜色(支持颜色名或RGB值)
plot(G, 'EdgeColor', 'red')
% 'LineWidth':边的宽度(数值越大线越粗,默认0.5)
plot(G, 'LineWidth', 2) % 边宽设为 2
% 'LineStyle':边的线型('-'实线 '--' 虚线)
plot(G, 'LineStyle', '--')
title('带权重的无向图');
% 5.绘制字符串标号的图
s = {'北京','上海','广州','深圳','上海'};
t = {'上海','广州','深圳','北京','深圳'};
w = [10 65 3 90 60];
G = graph(s,t,w);
plot(G,'EdgeLabel',G.Edges.Weight,'Linewidth',2);
% 6.绘制邻接矩阵表示的图
c = [0 15 10 20 0 0 0 0;
0 0 0 0 7 10 0 0;
0 0 0 0 0 8 2 0;
0 0 0 0 0 0 18 0;
0 0 0 0 0 0 0 6;
0 0 0 0 0 0 0 16;
0 0 0 0 0 0 0 20;
0 0 0 0 0 0 0 0;];
view(biograph(c, [], 'ShowWeights', 'on'));
% biograph(邻接矩阵, 节点名称, 属性名, 属性值)
%必须是方阵,c(i,j) 的值表示 “从节点 i 到节点 j 的边权重”: