- Click on 'Edit' and select 'Axes Properties...' option from the drop-down.
- Modify 'YDir' value from normal to reverse.
How to manually set layers for graph plot?
58 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
James Anderson
el 10 de Feb. de 2023
Comentada: James Anderson
el 10 de Feb. de 2023
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!
0 comentarios
Respuesta aceptada
Divyank
el 10 de Feb. de 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:
Hope this helps!
Más respuestas (0)
Ver también
Categorías
Más información sobre 2-D and 3-D Plots en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!