Change label font in Graph plots

I am plotting a Graph object (created using the graph function). I figured out how to label nodes and edges in my graph, but I do not know how to change the font size and weight for the label text. How can it be done?

Answers (1)

You can change the node labels (that I assume you are referring to) by using a text object.
This code is from the documentation for graph, with my tweaks to change the node label font size:
s = [1 1 1 2 2 3 3 4 5 5 6 7];
t = [2 4 8 3 7 4 6 5 6 8 7 8];
weights = [10 10 1 10 1 10 1 1 12 12 12 12];
names = {'A' 'B' 'C' 'D' 'E' 'F' 'G' 'H'};
G = graph(s,t,weights,names);
figure(1)
h = plot(G,'EdgeLabel',G.Edges.Weight);
nl = h.NodeLabel;
h.NodeLabel = '';
xd = get(h, 'XData');
yd = get(h, 'YData');
text(xd, yd, nl, 'FontSize',20, 'FontWeight','bold', 'HorizontalAlignment','left', 'VerticalAlignment','middle')
The plot:

Products

Asked:

on 17 Jul 2016

Commented:

on 16 Nov 2016

Community Treasure Hunt

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

Start Hunting!