-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsankeyDemo21.m
More file actions
74 lines (62 loc) · 2.63 KB
/
Copy pathsankeyDemo21.m
File metadata and controls
74 lines (62 loc) · 2.63 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
%% sankey demo21 : Stacked bar chart and zero-height nodes
% This demo is inspired by:
% Ma, L., Zhang, H., Lu, Q. (2026). Multi-perspective assessment of
% ecological civilization progress across cities in the Yangtze River
% Economic Belt. Environmental and Sustainability Indicators, 29, 101107.
% https://doi.org/10.1016/j.indic.2025.101107
rng(3)
% 随便构造一组数据 (Generate random multi-layer flow data)
RandMat = @() rand([7, 7]).*([0, rand([1, 5]), 0] < .85).*...
(rand([7, 7]) < .65).*((1:7).^2.'.*(1:7));
A = RandMat();
A = A./sum(sum(A));
ASet = {A};
for i = 2:7
A = RandMat();
A = A./sum(A, 2).*sum(ASet{i - 1}, 1).';
A = A./sum(sum(A));
ASet{i} = A;
end
% 设置层名和节点名 (Set layer and node names)
layerNames = {'2019','2020','2021','2022','2023','2024','2025','2026'};
nodeNames = {'AAAA','BBBB','CCCC','DDDD','EEEE','FFFF','GGGG'};
% 颜色表 (Color list)
CList = [.02,.18,.29; .04,.40,.56; .20,.58,.73; .55,.71,.46;
.25,.53,.47; .08,.38,.32; .69,.39,.16; .90,.68,.55];
LN = length(layerNames);
NN = length(nodeNames);
CList = CList(1:NN, :);
fig = figure('Name','sankey demo21', 'Units','normalized', 'Position',[.025,.2,.95,.6]);
axL = axes(fig, 'Position',[.10, .1, .25, .7], 'NextPlot','add');
axR = axes(fig, 'Position',[.38, .1, .6, .7], 'NextPlot','add');
% 绘制桑基图 (Render the Sankey diagram)
adjMat = mergeAdjMat(ASet);
SK = SSankey(axR, [],[],[], 'AdjMat',adjMat);
SK.Layer = kron(1:LN, ones(1, NN));
SK.ColorList = repmat(CList, [LN, 1]);
SK.NodeList = repmat({''}, 1, LN*NN);
SK.RenderingMethod = 'left';
SK.BlockScale = .08;
SK.Align = 'down';
SK.Sep = .02;
SK.ZeroNodeHeightRatio = 0;
SK.draw()
% 绘制堆叠柱状图 (Draw Stacked bar chart)
Y = sum(ASet{1}, 2);
for i = 2:LN
Y(:, i) = sum(ASet{i - 1}, 1).';
end
bHdl = bar(axL, Y(end:-1:1, :).', 'stacked', 'EdgeColor','none');
for i = 1:length(bHdl)
bHdl(i).FaceColor = CList(length(bHdl) + 1 - i,:);
end
% 坐标区域属性设置 (Set properties for axes)
set(axR, 'Box','on', 'LineWidth',2, 'XColor','k', 'YColor','k', 'TickLength',[0,0], ...
'FontSize',15, 'XTick',1:LN, 'YTick',[], 'XTickLabel',layerNames)
set(axL, 'Box','on', 'LineWidth',2, 'XColor','k', 'YColor','k', 'TickLength',[0,0], ...
'FontSize',15, 'XTick',1:LN, 'XLim',[.5, .5 + LN], 'YLim',[0,1], 'XTickLabel',layerNames)
set(axL.YLabel, 'String','Percentage (%)', 'FontSize',21)
% 图例 (Legend)
legend(axL, nodeNames, 'Position',[.18,.85,.45,.06], 'NumColumns',NN)
annotation('textbox', [.1,.85,.08,.06], 'String', 'Legend', ...
'LineWidth',2, 'FontSize',21, 'VerticalAlignment','middle', 'HorizontalAlignment','center')