MATLAB Answers

Is there another way to plot a graph 3D without digraph ?

15 views (last 30 days)
Joel Sande
Joel Sande on 12 Apr 2016
Commented: NA on 12 Feb 2020
Hi everybody, I want to do something using Matlab 2014 that mimic digraph of Matlab 2015.
% let's say we have 3 vectors of x,y,z coordinates of 11 nodes
x = [2 5 6 2 8 4 9 1 6 2 3];
y = [3 1 9 6 7 5 4 2 3 6 8];
z = [2 6 9 6 3 2 4 7 5 8 9];
% we also have the interactions Matrice A between those nodes.
A = [0 1 1 0 0 1 0 1 1 0 0 ; -1 0 -1 0 0 -1 0 0 -1 0 0 ; 1 1 0 0 0 1 0 1 0 1 1 ; 0 0 0 0 -1 -1 0 -1 0 -1 0; -1 -1 -1 0 0 -1 0 -1 -1 0 0 ; -1 -1 0 0 -1 0 0 0 -1 0 0 ; 1 0 1 0 0 1 0 0 0 1 0 ; 0 1 0 1 1 1 0 0 0 1 0 ; 1 1 0 1 0 0 1 1 0 0 1 ; 1 0 1 0 0 0 1 1 0 0 1 ; 0 0 0 -1 0 0 -1 -1 0 -1 0];
% we also have the strength Matrice B, of those interactions.
B = [0 1 7 0 0 3 0 2 6 0 0 ; 5 0 3 0 0 3 0 0 7 0 0 ; 5 1 0 0 0 1 0 2 0 9 7 ; 0 0 0 0 3 7 0 1 0 4 0; 6 5 3 0 0 7 0 3 3 0 0 ; 8 9 0 0 6 0 0 0 9 0 0 ; 9 0 1 0 0 8 0 0 0 9 0 ; 0 1 0 3 6 3 0 0 0 8 0 ; 1 6 0 3 0 0 1 8 0 0 9 ; 8 0 3 0 0 0 6 1 0 0 1 ; 0 0 0 3 0 0 9 5 0 3 0];
% The strength will be perceived on the graph as the intensity of the given color blue or red.
We may also use dash ------, - - - - - -, - - - - - -, motif to differentiate between 9 to 1, or something else, depending on you, or even write the number dirrectlly on the narrow as it is done in digraph.
% naturally, interactions between a given neuron and itself = 0 if you look very well, 1 = blue narrow, -1 = red narrow.
% and we have the real distance between 2 node is a kid of curve(same like what we see in 2015)
% the curve length is the cartesien distance + (% 0 to 10 % that cartesien distance)(I have already written the function that do that). To make it easy for you, let s consider only the cartesien distance (strait lines).
% How can we plot this graph ?

Accepted Answer

Mike Garrity
Mike Garrity on 13 Apr 2016
You'd be hard pressed to get all of the features of digraph's plot, but the patch function will let you draw a wireframe with a different color per edge. That would look something like this:
nnodes = length(x);
verts = [x',y',z'];
faces = [];
colors = [];
for i=1:size(A,1)
for j=1:size(A,2)
if A(i,j)
faces(end+1,:) = [i, j, nan];
colors(end+1) = B(i,j);
end
end
end
patch('Faces',faces,'Vertices',verts,'FaceVertexCData',colors', ...
'EdgeColor','flat','FaceColor','none', ...
'MarkerFaceColor','none','MarkerEdgeColor','none')
view(3)
colorbar
If you want to use different line widths or patterns, you're going to need to create a different line object for each edge. You'll find that doesn't scale well for the reasons I explained in this blog post .
for i=1:size(A,1)
for j=1:size(A,2)
if A(i,j)
line([x(i) x(j)],[y(i) y(j)],[z(i) z(j)],'LineWidth',B(i,j))
end
end
end
view(3)
  6 Comments
Mike Garrity
Mike Garrity on 14 Apr 2016
Yes !
Warning: Patch FaceVertexCData length (53) must equal Vertices length (11) for flat EdgeColor
Well that's annoying. You appear to have hit an old bug I'd forgotten about. It was fixed in R2014b, but that doesn't do you much good.
I would suggest using the approach I showed in the LineWidth example. That'll work for colors as well. As I said, it'll have scaling issues if the number of edges gets really large, but if your graphs are reasonably small it should work fine. And it really is a lot simpler than the patch approach.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!