Graphplot nodes How to ensure generating only existing nodes.
6 views (last 30 days)
Show older comments
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
0 Comments
Accepted Answer
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
plot(D1)
D2 = digraph(string(S), string(T)) % use S and T as node names, 3 nodes
plot(D2)
5 Comments
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
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.
More Answers (0)
See Also
Categories
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!