-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplotgraph.m
More file actions
92 lines (80 loc) · 3.15 KB
/
plotgraph.m
File metadata and controls
92 lines (80 loc) · 3.15 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
88
89
90
91
92
function plotgraph(A,xy,weights)
% PLOTGRAPH Plots a graph with each edge width proportional to
% its weight. Edges with positive weights are drawn in blue;
% edges with negative weights are drawn in red.
%
% Input parameters:
% A --- incidence matrix of the graph (size is n x m)
% (n is the number of nodes and m is the number of edges)
% xy --- horizontal and vertical positions of the nodes (n x 2 matrix)
% weights --- m vector giving edge weights
%
% Original by Lin Xiao
% Modified by Almir Mutapcic
% graph size
[n,m]= size(A);
% set the graph scale and normalize the coordinates to lay in [-1,1] square
R = max(max(abs(xy))); % maximum abs value of the xy coordinates
x = xy(:,1)/R; y = xy(:,2)/R;
% normalize weight vector to range between +1 and -1
weights = weights/max(abs(weights));
% internal parameters (tune these parameters to make the plot look pretty)
% (note that the graph coordinates and the weights have been rescaled
% to a common unity scale)
%rNode = 0.005; % radius of the node circles
rNode = 0; % set the node radius to zero if you do not want the nodes
wNode = 2; % line width of the node circles
PWColor = [0 0 1]; % color of the edges with positive weights
NWColor = [1 0 0]; % color of the edges with negative weights
Wmin = 0.0001; % minimum weight value for which we draw an edge
max_width = 0.05; % drawn width of edge with maximum absolute weight
% first draw the edges with patch widths proportional to the weights
for i=1:m
if ( abs(weights(i)) > Wmin )
Isrc = find( sign(weights(i))*A(:,i)>0 );
Idst = find( sign(weights(i))*A(:,i)<0 );
else
Isrc = find( A(:,i)>0 );
Idst = find( A(:,i)<0 );
end
% obtain edge patch coordinates
xdelta = x(Idst) - x(Isrc); ydelta = y(Idst) - y(Isrc);
RotAgl = atan2( ydelta, xdelta );
xstart = x(Isrc) + rNode*cos(RotAgl); ystart = y(Isrc) + rNode*sin(RotAgl);
xend = x(Idst) - rNode*cos(RotAgl); yend = y(Idst) - rNode*sin(RotAgl);
L = sqrt( xdelta^2 + ydelta^2 ) - 2*rNode;
if ( weights(i) > Wmin )
W = abs(weights(i))*max_width;
drawedge(xstart, ystart, RotAgl, L, W, PWColor);
hold on;
elseif ( weights(i) < -Wmin )
W = abs(weights(i))*max_width;
drawedge(xstart, ystart, RotAgl, L, W, NWColor);
hold on;
else
plot([xstart xend],[ystart yend],'k:','LineWidth',2.5);
end
end
% the circle to draw around each node
angle = linspace(0,2*pi,100);
xbd = rNode*cos(angle);
ybd = rNode*sin(angle);
% draw the nodes
for i=1:n
plot( x(i)+xbd, y(i)+ybd, 'k', 'LineWidth', wNode );
end;
axis equal;
set(gca,'Visible','off');
hold off;
%********************************************************************
% helper function to draw edges in the graph
%********************************************************************
function drawedge( x0, y0, RotAngle, L, W, color )
xp = [ 0 L L L L L 0 0 ];
yp = [-0.5*W -0.5*W -0.5*W 0 0.5*W 0.5*W 0.5*W -0.5*W];
RotMat = [cos(RotAngle) -sin(RotAngle); sin(RotAngle) cos(RotAngle)];
DrawCoordinates = RotMat*[ xp; yp ];
xd = x0 + DrawCoordinates(1,:);
yd = y0 + DrawCoordinates(2,:);
% draw the edge
patch( xd, yd, color );