Adding two node labels to graph

8 vues (au cours des 30 derniers jours)
Deepa Maheshvare
Deepa Maheshvare le 28 Jan 2020
Modifié(e) : Adam Danz le 21 Fév 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

Réponse acceptée

Adam Danz
Adam Danz le 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 commentaires
Deepa Maheshvare
Deepa Maheshvare le 21 Fév 2020
Hi, could you please explain how you got the colorbars?
Adam Danz
Adam Danz le 21 Fév 2020
Modifié(e) : Adam Danz le 21 Fév 2020
You can add a colorbar to a plot by calling the function
colorbar()
just as you did in the code from your question :)

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Graph and Network Algorithms dans Help Center et File Exchange

Produits


Version

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by