Plotting a 3d network model using graph

4 views (last 30 days)
Shahar Goren
Shahar Goren on 23 Sep 2019
Moved: Walter Roberson on 16 Mar 2025
I want to plot a 3 dimensional network model using the graph function.
for example
edges1 = [1,1,1,2,2,2,2,2,2,2,3,3,3,3,3,3,4,4,4,4,4,5,6,6,6,6,6,7,8,8,8,8,10,11,11,11];
egdes2 = [2,3,4,3,4,5,6,7,8,9,4,5,6,10,11,12,7,8,10,11,13,6,8,9,11,12,14,8,9,11,13,14,11,12,13,14];
x = [0,0,0.5000,0.5000,0,0.5000,0,0.5000,0,1.0000,1.0000,1.0000,1.0000,1.0000];
y = [0,0.5000,0,0.5000,0,0.5000,1.0000,1.0000,1.0000,0,0.5000,0,1.0000,1.0000];
z = [0,0.5000,0.5000,0,1.0000,1.0000,0,0.5000,1.0000,0,0.5000,1.0000,0,1.0000];
G = graph(edges1,egdes2);
plot(G,'Xdata',x,'Ydata',y,'Zdata',z)
However, the line visualization does not give good depth percepsion, and 3d structure can only be percieved when roatating the image.
I'm looking for a way to give the edges some volume to improve visualization, without making the image too heavy to handle.
Is there a neat way to do this?
Thanks!

Answers (1)

ag
ag on 12 Mar 2025
Hi Shahar,
To improve the visualization of a 3D network model in MATLAB and give the edges some volume, you can use cylinders to represent the edges instead of simple lines. This approach can enhance depth perception and make the 3D structure more apparent.
The below code snippet demonstrates how can this be achieved:
% Define the edges and node coordinates
edges1 = [1,1,1,2,2,2,2,2,2,2,3,3,3,3,3,3,4,4,4,4,4,5,6,6,6,6,6,7,8,8,8,8,10,11,11,11];
edges2 = [2,3,4,3,4,5,6,7,8,9,4,5,6,10,11,12,7,8,10,11,13,6,8,9,11,12,14,8,9,11,13,14,11,12,13,14];
x = [0,0,0.5,0.5,0,0.5,0,0.5,0,1,1,1,1,1];
y = [0,0.5,0,0.5,0,0.5,1,1,1,0,0.5,0,1,1];
z = [0,0.5,0.5,0,1,1,0,0.5,1,0,0.5,1,0,1];
% Create the graph
G = graph(edges1, edges2);
% Plot the nodes
figure;
scatter3(x, y, z, 100, 'filled', 'MarkerFaceColor', 'b');
hold on;
% Plot the edges with volume
numEdges = numedges(G);
for i = 1:numEdges
% Get the start and end points of each edge
startNode = edges1(i);
endNode = edges2(i);
% Get coordinates for the start and end nodes
xStart = x(startNode);
yStart = y(startNode);
zStart = z(startNode);
xEnd = x(endNode);
yEnd = y(endNode);
zEnd = z(endNode);
% Create a cylinder between the nodes
[X, Y, Z] = cylinder(0.02, 8); % Adjust radius for thickness
Z = Z * sqrt((xEnd - xStart)^2 + (yEnd - yStart)^2 + (zEnd - zStart)^2);
% Rotate and translate the cylinder to the correct position
[theta, phi, r] = cart2sph(xEnd - xStart, yEnd - yStart, zEnd - zStart);
R = makehgtform('axisrotate', [0 -1 0], phi, 'axisrotate', [0 0 1], theta);
for j = 1:numel(X)
pt = R * [X(j); Y(j); Z(j); 1];
X(j) = pt(1);
Y(j) = pt(2);
Z(j) = pt(3);
end
surf(X + xStart, Y + yStart, Z + zStart, 'FaceColor', 'r', 'EdgeColor', 'none');
end
axis equal;
grid on;
xlabel('X');
ylabel('Y');
zlabel('Z');
title('3D Network Model with Volume on Edges');
hold off;
For more details, please refer to the following MathWorks documentation:
Hope this helps!
  1 Comment
shahar goren
shahar goren on 16 Mar 2025
Moved: Walter Roberson on 16 Mar 2025
Thank you @ag,
This might be the best approach, but I was looking for a method that can work without adding each edge separately, because this would mean the graphics computations (for example, when rotating the image) will be very slow if the network is large.
Anyhow, Thank you for your answer after all this time!

Sign in to comment.

Categories

Find more on Networks in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!