Rotate and Position Node Labels
13 views (last 30 days)
Show older comments
Kamal Premaratne
on 23 Feb 2021
Commented: Kamal Premaratne
on 14 Mar 2022
I am plotting a digraph with node labels. However, the node labels are difficult to see because they are overwritten by the edges. See atatched digraph plot. My questions are the following:
(a) Is it possible to move the node labels so that node labels '066', '065', and '004' are vertical and above their nodes, 'ER' (the node in the middle of the plot) is horizontal and to the right of the node, and the other node labels are vertically below the nodes?
(b) If individal positiioning of the node labels are not possible, can we rotate and move all the node labels by the same amount? Of course, (a) is preferable but I can live with (b) in case (a) is impossible.
Thank you so much.
-- Kamal
0 Comments
Accepted Answer
Prahlad Gowtham Katte
on 14 Mar 2022
Edited: Prahlad Gowtham Katte
on 14 Mar 2022
Hello
As per my understanding of your query you wish to customize the position and orientation of the nodes so that they can be seen easily. A possible workaround to achieve the same is by removing the labels and replace them with your own matching text labels. That way you have complete control over the placement of each label. The following code is an example of how you can change the position of a label for a digraph.
G=digraph(s,t,weights,16);
p=plot(G);
p.NodeColor='red';
p.LineWidth=weights;
%Xdata and Ydata are positions of the node starting from 1
p.XData=[10 20 30 40 50 60 70 80 90 100 110 120 60 50 60 70];
p.YData=[30 30 30 30 30 30 30 30 30 30 30 30 60 90 90 90];
%Changing all nodes' labels properties.
text(p.XData-2,p.YData+5,p.NodeLabel, ... %the first 2 arguments provide the position and we can give individual X and Ypositions of each node
'VerticalAlignment','top',...
'HorizontalAlignment', 'left',... % alignment with the node
'FontSize', 9)
%Changing only the first node's label properties
% text([p.XData(1)-2 p.XData(2:end)],[ p.YData(1)+5 p.YData(2:end)] ,p.NodeLabel, ...
% 'VerticalAlignment','top',...
% 'HorizontalAlignment', 'left',... % alignment with the node
% 'FontSize', 9)
%Without this line labels will be duplicated.
p.NodeLabel={};%Changing labels to empty as we have created a text for each label.
For more information on plot properties please refer to the following link.
Hope it helps
More Answers (0)
See Also
Categories
Find more on Graph and Network Algorithms 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!