Change the alignment and font size of edgelabels

8 vues (au cours des 30 derniers jours)
Deepa Maheshvare
Deepa Maheshvare le 4 Fév 2020
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 ?

Réponse acceptée

Christine Tobler
Christine Tobler le 4 Fév 2020
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.

Plus de réponses (1)

fred  ssemwogerere
fred ssemwogerere le 4 Fév 2020
Modifié(e) : fred ssemwogerere le 4 Fév 2020
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 commentaires
Deepa Maheshvare
Deepa Maheshvare le 4 Fév 2020
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 le 4 Fév 2020
Hello, unfortunately, i think there is no option for changing the alignment of labels along the edges of a digraph.

Connectez-vous pour commenter.

Produits


Version

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by