Positioning
Before we can draw an image, we have to set the positions of the nodes in our network. There are two ways of doing that:
An easy way and a more advanced way:
The first way is automatically available with the installation of net-bench.
The second way is more complicated to install but provides a (subjectively)
better positioning.
Easy positioning
This is an example of how the Layouter-class is used to set positions:
First, an object layouter is created, which passes the path for the file in
which the positions are written via update_positions() as well as the graph itself and a so-called seed.
The seed (in this case 12) determines the randomness of the positions and the network. If you want to try different positions, you can iterate over the seed (for example with a range and a for-loop). The lines above can also be shortened:
The variable layout now holds a dictionary, which can be passed for drawing (see below).
The default layout is NetworkXs Spring Layout. It works with all kinds of graphs. There also is the possibility of using NetworkXs Kamada-Kawai Layout to create the graph.
Advanced positioning using Graphviz
Graphviz allows for more beautiful results in positioning, but is more
difficult to install. You have to install Graphviz or pygraphviz first.
Here you can find the explanation for how to install Graphviz.
You can also install the python integration pygraphviz
(Explanation
here,
but you still need Graphviz.)
When you have done that, you can directly call the graphviz Layouter and set
your positions:
If you want to use a differen Layout Engine (e.g. for big networks) pass it via the
parameter prog:
positions = (
dsslab.net_bench.GraphvizLayouter()
.update_positions("positions.json", graph, seed=12, prog="fdp")
)
For an Overview of the available Layout Engines in Graphviz look here.
Warning
The seed for Graphviz is not deterministic (there are different results on different systems, even with the same arguments). That's why the saving of positions on different computers always uses a positions file.
Note
Both options use an argument for the location and the name of the position files,
in the examples this is always positions.json. My suggestion is to include this file
into your version control when you have settled on a positioning. This allows you to
transfer your Setup to others in you team or to have a history of your positions. Your path
to your positions file should thus lie within your repositories.
Import from the graph itself
There is the possibility to decide the positions based on imported node attributes:
If you want to save those positionns in a file, you can do so within the same directory like this: