How to manually set layers for graph plot?

58 views (last 30 days)
Was wondering if anyone knew any methods for manually setting the layering order of nodes in a graph plot. I have a graph with nodes that have values associated from 1 to 20. What I was hoping for was to be able to have all nodes of value 1 on the top row, all nodes of value 2 on the second row, all nodes of value 3 on the third row, and so on up to the 20th row.
Additionally, if a node at level 3 goes to a node at level 7, I want those two nodes to be on the third and seventh rows, with a line connecting them. Basically all nodes need to be at the level they are matched with as their value.
I am very close with the ability to set the Layout property of the graph plot to 'layered' so that it takes on the row appearance, but not with the specific behavior I'm describing above. Thank you!

Accepted Answer

Divyank
Divyank on 10 Feb 2023
Hello @James Anderson, you can control the position of nodes in a graph plot in MATLAB using the 'XData' and 'YData' properties of the 'Node' object. Here's one way to do what you're asking for:
% Define the values of nodes
values = [1, 2, 3, 7, 8, 9, 17, 18, 19];
% Define the number of nodes
n = length(values);
% Creates an adjacency matrix A of size n x n with random connections between nodes
A = zeros(n, n);
for i = 1:n
for j = i+1:n
if rand < 0.5
A(i,j) = 1;
A(j,i) = 1;
end
end
end
% Create a graph from the adjacency matrix
G = graph(A);
% Define node positions based on the values
xpos = zeros(n, 1);
ypos = zeros(n, 1);
for i = 1:n
xpos(i) = i;
ypos(i) = values(i);
end
% Plot the graph
h = plot(G, 'XData', xpos, 'YData', ypos, 'LineWidth', 2);
% Set the axis limits
xlim([0, n + 1]);
ylim([0, 20]);
This will generate a figure with all nodes present at the level according to their values, however in order for node with value 1 to be at top and so on, you will have to reverse the direction of Y-axis. This can be done using the following steps:
  1. Click on 'Edit' and select 'Axes Properties...' option from the drop-down.
  2. Modify 'YDir' value from normal to reverse.
Hope this helps!
  1 Comment
James Anderson
James Anderson on 10 Feb 2023
Hi Divyank! Thanks for the reply.
I knew about setting XData and YData, but wanted to see if this was the only way to accomplish what I'm looking for. The only gripe I have is that I cannot only specify YData and have MatLab arranage the nodes automatically for a good fit. I'll look into making a function that spaces the nodes out well.
One quick last question though, is there anyway to have the graph lines be curved in the way that the 'layered' Layout uses alongside this technique? I found the forum post below asking the same thing, but when using the suggested solution, it removes the positioning of the nodes:

Sign in to comment.

More Answers (0)

Categories

Find more on 2-D and 3-D Plots in Help Center and File Exchange

Products


Release

R2022b

Community Treasure Hunt

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

Start Hunting!