Graphplot nodes How to ensure generating only existing nodes.

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

 Réponse acceptée

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 commentaires

Thank you Steven. It works great with small graphs. How can I enforce keeping the names of the nodes while ploting a relatively big graph?. Starting with some size of a graph D, plot generates Graphplot with empty Node Label. :(
p =
GraphPlot with properties:
NodeColor: [1 0 0]
MarkerSize: 1
Marker: '.'
EdgeColor: [0 0.4470 0.7410]
LineWidth: 0.5000
LineStyle: '-'
NodeLabel: {}
EdgeLabel: {}
XData: [1×10001 double]
YData: [1×10001 double]
ZData: [1×10001 double]
OK...I got it. This does the trick:
aa=G.Nodes;bb=aa.Name;
p.NodeLabel=bb;
OK...now if I have to manipulate the position of the individual nodes in Graphplot by affecting e.g. p.XData, how do I enumerate to get the proper relationship between enumeration as in S in T and the enumeration in XData?
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.
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.

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Graph and Network Algorithms dans Centre d'aide et File Exchange

Produits

Version

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by