Graphplot nodes How to ensure generating only existing nodes.

1 view (last 30 days)
My definition of the graph by means of source-target vectors do not cover all node indices. Eg. S=[1 30] ;T=[2, 1]. Thus nodes 3 to 29 do not show up in the graph definition. G=(di)graph(S,T) have a notion of all 30 nodes G:Nodes is table 30xempty. The "unconnected" nodes 3-29 are not of interest and not meant to be plotted in Graphplot. How can i enforce removing the unconnected nodes? My best solution so far is to "move" unwanted nodes to the side of a plot by modifying p.XData.
Looking for a nicer solution to the problem

Accepted Answer

Steven Lord
Steven Lord on 9 Apr 2021
S=[1 30];
T=[2, 1];
D1 = digraph(S, T) % use S and T as node numbers, 30 nodes
D1 =
digraph with properties: Edges: [2×1 table] Nodes: [30×0 table]
plot(D1)
D2 = digraph(string(S), string(T)) % use S and T as node names, 3 nodes
D2 =
digraph with properties: Edges: [2×1 table] Nodes: [3×1 table]
plot(D2)
  5 Comments
Steven Lord
Steven Lord on 13 Apr 2021
For more information on some of the customization you can perform on a plotted graph, see this documentation page. As stated on that page: "Node labels are included automatically in plots of graphs that have 100 or fewer nodes." Your plot has 10001 nodes, so MATLAB doesn't automatically add the node labels (otherwise the plot would be way too cluttered.
You could use labelnode to label individual nodes or if you're okay with the clutter you could change the NodeLabelMode property to 'auto'. Compare these two plots of the same 200 node graph, one with node labels and one without.
R = sprandsym(200, 200, 0.1);
G = graph(R);
G.Nodes.Name = string(1:200).';
h = plot(G);
title('No node labels')
figure
h2 = plot(G, 'NodeLabelMode', 'auto');
title('All node labels')
Now add node labels to a handful of nodes.
figure
h3 = plot(G);
labelnode(h3, 1:10:200, string(1:10:200))
title('Some node labels')
As for the mapping between nodes and XData element N of XData is the XData for node N:
S=[1 30];
T=[5, 1];
D1 = digraph(string(S), string(T));
h = plot(D1);
D1.Nodes.X = h.XData.';
% Change node 2's position
figure
h2 = plot(D1);
h2.XData(2) = h2.XData(2) + 3;
D1.Nodes.X2 = h2.XData.';
D1.Nodes
ans = 3×3 table
Name X X2 ______ _ __ {'1' } 1 1 {'5' } 1 4 {'30'} 1 1
I stored the X coordinates from the two plots in the Nodes table as additional information so you can see I did change the X coordinate for the second node.
Krystian
Krystian on 13 Apr 2021
Unfortunately my only useful index is rather coded now in Name, so as in your example above I need to change position of node '5' (not actually even knowing that it is now a node 2) by referring to it by index 5. I would rather refrain of "finding" it via find or scanning through thenames in the whole graph. Background: I would like to arrange my graph to a layout similar to 'radial' as in tree, unfortunatelly not available for graphplot, thus, I have to change position of all nodes accordingly.BTW a Collatz-graph. Thanks for your tips so far.

Sign in to comment.

More Answers (0)

Categories

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

Products


Release

R2019b

Community Treasure Hunt

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

Start Hunting!