Adding two node labels to graph

조회 수: 8 (최근 30일)
Deepa Maheshvare
Deepa Maheshvare 2020년 1월 28일
편집: Adam Danz 2020년 2월 21일
I've the following graph
t = [1 1 1 1 2 2 3 4 4 5 6];
h = [2 3 4 5 3 6 6 5 7 7 7];
H = graph(t,h)
H.Nodes.Name = cellstr(string(1:height(H.Nodes))')
H.Nodes.Value = (1:height(H.Nodes))';
plt = plot(H)
plt.Marker = 'o';
plt.MarkerSize = 15;
plt.NodeCData = H.Nodes.Value;
colorbar
%second node label
%[0.12;0.13;1.24;10.24;2.3;4.56;1.00]
By default, the names of nodes are added as node label in the graph, I'd like to know how to add a second node label .I'd like to position the first label above and second label below the node

채택된 답변

Adam Danz
Adam Danz 2020년 1월 28일
There are lots of ways to do this. Here's a simple approach that uses labelpoints from the file exchange instead of labeling the nodes with the NodeLabel property. The labelpoints function is just a wrapper to the text() function that makes positioning of the text a little easier.
% Remove NodeLabels
plt.NodeLabel = {};
% Define upper and lower labels (they can be numeric, characters, or strings)
upperLabels = 1:numel(plt.XData);
lowerLabels = [0.12;0.13;1.24;10.24;2.3;4.56;1.00];
% label each node and offset the labels in the North and South directions.
% You can play around with this offset values ------vvv
labelpoints(plt.XData, plt.YData, upperLabels, 'N', 0.3);
labelpoints(plt.XData, plt.YData, lowerLabels, 'S', 0.3);
200127 231636-MATLAB Online R2019b.png
  댓글 수: 2
Deepa Maheshvare
Deepa Maheshvare 2020년 2월 21일
Hi, could you please explain how you got the colorbars?
Adam Danz
Adam Danz 2020년 2월 21일
편집: Adam Danz 2020년 2월 21일
You can add a colorbar to a plot by calling the function
colorbar()
just as you did in the code from your question :)

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Graph and Network Algorithms에 대해 자세히 알아보기

제품


릴리스

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by