Clear Filters
Clear Filters

Diagonal Node Labels for Bipartite Graph?? (Undirected Graph Formatting Help)

2 views (last 30 days)
Good evening,
I have really long node labels for my 2nd group of vertices on my bipartite graph. This causes my node labels to overlap with eachother and look incredibly ugly. A picture of this is shown below:
I've been trying for a while to make the graph look presentable, and I'm totally stumped. The node labels are formated "Race Sex PoliceComplaintType". So for example, "White Female Abuse of Authority". I tried rotating the node labels diagonally but couldn't figure out how to do it. I've tried adding carriage returns or new lines so that the node labels print like:
This works in my table, but it doesn't show up on the plot. The code that adds the new lines to the table are shown below:
race = string(D.RACE);
sex = string(D.SEX);
type = string(D.TYPE);
dummyStr = race + newline + sex + newline + type
D.COMBO = dummyStr;
I've already extended the spacing between each node by manually setting the XData. But the graph is already so wide, and I worry making it any wider will just make it even harder to read. Does anyone know of a way I can manipulate my Node Labels to make them appear legibly on my graph?
Code for the graph plotting is included below:
LWidths = 5*G2.Edges.Weight/max(G2.Edges.Weight);
% G2 is my graph
p = plot(G2);
% Don't label edges with weights less than or equal to 4. Helps keep graph clean
idx3 = find(G2.Edges.Weight > 4);
% Thicken edge widths proportionally to the edge weights
p.LineWidth = LWidths;
% set font size
p.EdgeFontSize = 10;
% splits vertices groupings. Group 1 (units) have YData = 2. Group 2 (complaint & demographic) YData = 1
% gives traditional bipartite appearance
p.YData = yData3;
% manually set xData for spacing of vertices
p.XData = xData3;
title(" Bipartite Graph: Units vs Combined Demographic & Complaint Data");

Accepted Answer

Kelly Kearney
Kelly Kearney on 21 Oct 2020
The node labels in a plotted graph are difficult to alter. You can get to them by digging into undocumented properties. For example:
nodes = {...
'a pretty long label'
'and an even longer label'
G = digraph(rand(3), nodes);
p = plot(G);
labeledge(p, 1, 'test');
set(p.NodeChildren(2), 'rotation', 45);
But any changes you make are going to be very fragile. For example, this rotation reverts if the figure is resized. In my experience, things like that also revert when you try to save a figure, which make it a bit useless.
The more robust way to deal with this would be to remove the labels from the graph altogether, and add your own text labels:
nodes = {...
'a pretty long label'
'and an even longer label'
G = digraph(rand(3), nodes);
p = plot(G, 'nodelabel', '');
labeledge(p, 1, 'test');
text(p.XData, p.YData, nodes, 'rotation', 45)

More Answers (0)


Find more on Graph and Network Algorithms in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!