Change the alignment and font size of edgelabels

17 ビュー (過去 30 日間)
Deepa Maheshvare
Deepa Maheshvare 2020 年 2 月 4 日
回答済み: Christine Tobler 2020 年 2 月 4 日
Hi,
For example, if Ihave the following graph
s = [1 1 1 2 2 3 3 4 4 5 6 7];
t = [2 3 4 5 6 5 7 6 7 8 8 8];
G = digraph(s,t)
eLabels = {'x' 'y' 'z' 'y' 'z' 'x' 'z' 'x' 'y' 'z' 'y' 'x'};
nLabels = {'{0}','{x}','{y}','{z}','{x,y}','{x,z}','{y,z}','{x,y,z}'};
plot(G,'Layout','force','EdgeLabel',eLabels,'NodeLabel',nLabels)
How to change the alignment of the edge labels? Say, from horizontal to vertical and I'd also like to know how to increase the font size of edge labels.
Any suggestions ?

採用された回答

Christine Tobler
Christine Tobler 2020 年 2 月 4 日
The edge labels provided with the plot of a graph can't be modified in terms of their alignment. However, you can add standard text elements, which have more options, in the middle of each edge:
s = [1 1 1 2 2 3 3 4 4 5 6 7];
t = [2 3 4 5 6 5 7 6 7 8 8 8];
G = digraph(s,t);
eLabels = {'textx' 'texty' 'textz' 'texty' 'textz' 'textx' 'textz' 'textx' 'texty' 'textz' 'texty' 'textx'};
nLabels = {'{0}','{x}','{y}','{z}','{x,y}','{x,z}','{y,z}','{x,y,z}'};
h=plot(G,'Layout','force');
[sSorted, tSorted] = findedge(G);
midEdgeX = (h.XData(sSorted) + h.XData(tSorted))/2;
midEdgeY = (h.YData(sSorted) + h.YData(tSorted))/2;
edgelabel = [];
for ii=1:length(eLabels)
[sii, tii] = findedge(G, ii);
edgelabel(ii) = text(midEdgeX(ii), midEdgeY(ii), eLabels(ii), 'HorizontalAlignment', 'right');
end
See the doc page of text for more options.

その他の回答 (1 件)

fred  ssemwogerere
fred ssemwogerere 2020 年 2 月 4 日
編集済み: fred ssemwogerere 2020 年 2 月 4 日
Hello, I think something like this can work nicely:
% Edit the last line of your code to:
h=plot(G,'Layout','force');
h.EdgeLabel=eLabels;
h.NodeLabel=nLabels;
% To change the Edge label font size;
h.EdgeFontSize=12; % '12' is an arbitrary choice here
% To get a list of any other properties of the object you would like to change, just type the name of the object handle, i.e. "h" (my arbitrary variable), in the command prompt.
  2 件のコメント
Deepa Maheshvare
Deepa Maheshvare 2020 年 2 月 4 日
Hi, Thank you. I tried your suggestion
s = [1 1 1 2 2 3 3 4 4 5 6 7];
t = [2 3 4 5 6 5 7 6 7 8 8 8];
G = digraph(s,t)
eLabels = {'textx' 'texty' 'textz' 'texty' 'textz' 'textx' 'textz' 'textx' 'texty' 'textz' 'texty' 'textx'};
nLabels = {'{0}','{x}','{y}','{z}','{x,y}','{x,z}','{y,z}','{x,y,z}'};
h=plot(G,'Layout','force');
h.EdgeLabel=eLabels;
h.NodeLabel=nLabels;
h.EdgeFontSize=12; % '12' is an a
Unfortunately, the alignment of text of edgelabels doesn't change. I want the edge labels to be oriented perpendicular to the edge i.e vertical .
Any suggestions?
fred  ssemwogerere
fred ssemwogerere 2020 年 2 月 4 日
Hello, unfortunately, i think there is no option for changing the alignment of labels along the edges of a digraph.

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

カテゴリ

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