Is it possible to change the position of graph plot node labels?

62 views (last 30 days)
Abece on 27 Aug 2019
Commented: Adam Danz on 27 Aug 2019
I used GraphPlot to create a graph but some node labels are on top of edges. Is it possible to change the label positions? If not, are there any alternatives to create visually appealing graphs?

Accepted Answer

Adam Danz
Adam Danz on 27 Aug 2019
I don't think there are documented properties of a GraphPlot that allow you to change the placement of the labels relative to the nodes. However, you could remove the labels and replace them with your own matching text labels. That way you have complete control over the placement of each label.
Here's a demo.
% Creat a graphplot
s = [1 1 1 1 1 1 1 9 9 9 9 9 9 9];
t = [2 3 4 5 6 7 8 2 3 4 5 6 7 8];
G = graph(s,t);
h = plot(G)
% Add new labels that are to the upper, right of the nodes
text(h.XData+.01, h.YData+.01 ,h.NodeLabel, ...
'HorizontalAlignment', 'left',...
'FontSize', 7)
% Remove old labels
h.NodeLabel = {};
As you can see, the demo manipulates the horizontal and vertical placement of the lables relative to the nodes. The demo also offsets the labels by 0.1 from the nodes. Check out text properties for many more options.
Adam Danz
Adam Danz on 27 Aug 2019
Nice! Just keep in mind that your offsets are unique to this graph. So if you were to apply this code to another graph, the offsets may not work in your favor (that's true of any type of hard coded offsets).

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!