framed nodelabel in a graph

1 回表示 (過去 30 日間)
Gaetano Pavone
Gaetano Pavone 2020 年 1 月 28 日
編集済み: Adam Danz 2020 年 2 月 7 日
Is it possible to plot a graph with framed nodelabels?
I would like to obtain nodelabels inserted in a square.
  2 件のコメント
Sahithi Kanumarlapudi
Sahithi Kanumarlapudi 2020 年 2 月 7 日
Could you provide more information about what you mean by framed nodelabels?
Gaetano Pavone
Gaetano Pavone 2020 年 2 月 7 日
I mean label of nodes surrounded by a figure (square, circle...)

サインインしてコメントする。

採用された回答

Adam Danz
Adam Danz 2020 年 2 月 7 日
編集済み: Adam Danz 2020 年 2 月 7 日
If there were a way to access the handles to the node label text objects, it would be easy to draw rectangels (or circles) around the labels. But I am not aware of such a method.
An alternative is to remove the node labels and add them back in as text objects. Then we would have the handles to the text objects and their coordinates, extent, etc. The demo below demonstrates how to do this. Note that this comes at a cost. The text objects acting as node labels are no longer associated with the nodes. So, if there's a change in the graph that causes a rearrangement of the nodes, the text objects will not follow.
% Create graph and plot it
A = triu(magic(4));
names = {'alpha' 'beta' 'gamma' 'delta'};
G = graph(A,names,'upper','omitselfloops');
h = plot(G);
% Store a copy of node labels, then remove the labels
NodeLabels = h.NodeLabel;
h.NodeLabel = repmat({''},size(h.NodeLabel));
% Add text objects for the labels.
% * You may have to adjust the HorizontalAlignment & VerticalAlignment to suit your needs
buffer = range(xlim())*0.02;
text(h.XData + buffer, h.YData, NodeLabels, 'HorizontalAlignment', 'Left', ...
'VerticalAlignment', 'Middle', 'EdgeColor', 'k', 'FontSize', h.NodeFontSize, ...
'Color', 'k', 'FontName', h.NodeFontName);
Note that you can also set the BackgroundColor of the text objects.

その他の回答 (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