Rotate and Position Node Labels

15 次查看(过去 30 天)
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

采纳的回答

Prahlad Gowtham Katte
编辑:Prahlad Gowtham Katte 2022-3-14
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

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Graph and Network Algorithms 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by