Rotate and Position Node Labels

조회 수: 13 (최근 30일)
Kamal Premaratne
Kamal Premaratne 2021년 2월 23일
댓글: Kamal Premaratne 2022년 3월 14일
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일
편집: 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
  댓글 수: 1
Kamal Premaratne
Kamal Premaratne 2022년 3월 14일
Thank you so much Prahlad. Yes, I think this works for me.

댓글을 달려면 로그인하십시오.

추가 답변 (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