Change the alignment and font size of edgelabels

11 次查看(过去 30 天)
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
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
Hello, unfortunately, i think there is no option for changing the alignment of labels along the edges of a digraph.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Annotations 的更多信息

产品


版本

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by