Adding two node labels to graph

5 views (last 30 days)
Deepa Maheshvare
Deepa Maheshvare on 28 Jan 2020
Edited: Adam Danz on 21 Feb 2020
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

Accepted Answer

Adam Danz
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);
200127 231636-MATLAB Online R2019b.png
  2 Comments
Deepa Maheshvare
Deepa Maheshvare on 21 Feb 2020
Hi, could you please explain how you got the colorbars?
Adam Danz
Adam Danz on 21 Feb 2020
Edited: Adam Danz on 21 Feb 2020
You can add a colorbar to a plot by calling the function
colorbar()
just as you did in the code from your question :)

Sign in to comment.

More Answers (0)

Categories

Find more on 2-D and 3-D Plots in Help Center and File Exchange

Products


Release

R2019b

Community Treasure Hunt

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

Start Hunting!