
Adding two node labels to graph
2 views (last 30 days)
Show older comments
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
0 Comments
Accepted Answer
Adam Danz
on 28 Jan 2020
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);

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!