Sun Steven
Sun Steven

Reputation: 3

Matlab 3D plot with connections between nodes

I have already generated the graph. the problem is I don't know how to create the connection between point in the figure.

Given I already have:

id=[1;2;3;...];
x=[x1;x2;x3;...];
y=[y1;y2;y3;...];
z=[z1;z2;z3;...];
connect_a=[id1;id2;id3;...];
connect_b=[id1';id2';id3';...];
scatter3(x,y,z,10,z);

all number in connect_a and connect_b are id number, and I want to build a connection between them. for example id1 should connect to id1'.

How should I solve this problem?

Upvotes: 0

Views: 325

Answers (1)

Robert Seifert
Robert Seifert

Reputation: 25232

I'm not gonna give a full generic answer, as you haven't shown any effort. But here is an example how you could do it in general.

Just draw some 3D-lines inbetween with plot3.

Example

%\\ my marker points 
A = CCD;   
x1 = A(:,1);
x2 = A(:,2);
x3 = A(:,3);

%\\ line style definition
style = {'color', 'black','LineStyle','-.','linewidth' ,1.5}

hf = figure(1);

%\\ connecting lines
plot3(x1,x2,x3,'.k', 'MarkerSize',25); hold on 
plot3([1 1 1 1 1],[-1 1 1 -1 -1],[1 1 -1 -1 1],style{:}); hold on
plot3([0 0 0 0 0],[-1 1 1 -1 -1],[1 1 -1 -1 1],style{:}); hold on
plot3([-1 1 1 -1 -1],[0 0 0 0 0],[1 1 -1 -1 1],style{:}); hold on
plot3([-1 1 1 -1 -1],[1 1 -1 -1 1],[0 0 0 0 0],style{:}); hold on
plot3([-1 -1 -1 -1 -1],[-1 1 1 -1 -1],[1 1 -1 -1 1],style{:}); hold on
plot3([1 -1],[-1 -1],[1 1],style{:}); hold on
plot3([1 -1],[-1 -1],[-1 -1],style{:}); hold on
plot3([1 -1],[1 1],[1 1],style{:}); hold on
plot3([1 -1],[1 1],[-1 -1],style{:}); hold on

%\\ some axes
plot3([-2 2],[0 0],[0,0],'color', 'black','linewidth' ,1); hold on
plot3([0 0],[-4 4],[0,0],'color', 'black','linewidth' ,1); hold on
plot3([0 0],[0 0],[-1.8 1.8],'color', 'black','linewidth' ,1); hold on
view(30,10)

%\\ some other axes limits and so on...
axis equal; grid on; hold off
set(gca,'xlim',[-1.682, 1.682],'XTick',[-1,0,1],'ylim',[-1.682, 1.682],'YTick',[-1,0,1],'zlim',[-1.682, 1.682],'ZTick',[-1,0,1]);

enter image description here

Of course you need to substitute all 1 with your ids. But I think you can manage that.

Now put all plot3 commands in a loop an ddo it dynamically.

Upvotes: 2

Related Questions