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 Center および File ExchangeGraph 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