Skip to content

Drawing

The new variant of drawing tries to reduce the many and complex arguments which need to be passed to the draw-function for a successful visualization. Instead of those arguments, settings are now applied successively to an object to compose an image step-by-step. This procedure is called method chaining.

This is an example of what this could look like:

ig = dnb.ImageGenerator(graph) # (1)
ig.nodes.set_colors(dnb.fixed("blue")) # (2)
ig.nodes.labels.set_labels({n: n for n in graph}) # (3)
ig.description.set_text("A description") # (4)
ig.edges.set_colors(fixed("grey")) # (5)

(
    ig.
    .draw_edges() # (6)
    .draw_nodes()
    .draw_labels()
    .draw_description()
    .write_file("my_image.svg") # (7)
) # (8)
1. Initialisation of an ImageGenerator object and assignment to ig. 2. Setting the colors of the nodes. 3. Adding all labels to the object. 4. Adding the description. 5. Setting the colors of the edges. 6. Drawing of the individual elements. First the edges, then the nodes, then the labels and the description. 7. Writing the image to a file. 8. To write multi-line code, the method calls need to be in brackets.

The parts of the image are drawn individually one after the other. That's why the order is as it is. First the edges, then the nodes, then the labels and the description comes last as it goes on the bottom. The image that was created this way is now in the working memory and still has to be saved locally. That's what we do in (7).

Warning

To chain multiple lines in python, the whole expression has to be put in round brackets.

"Difference between0 and None"

To understand the individual arguments used when drawing, there is a basic distinction made in net-bench:

Nodes and edges which have no value are None. None can e.g. mean that a page that has been searched holds no texts. If there a simply no hits for the searched values, we get a 0. 0 is an actual value which can be used in the following code. If successful searches are not possible, we get None. If they are possible, meaning there is a base to be searched, but we simply got no hits, we get a 0.

We can use this in the drawing module in different ways. It also is what the fallback-argument is for. We can for example give a specific color to nodes which have not received any values because of errors, making them disappear or highlighting them.

More complicated images

Method chaining makes it possible to compose more complex images, which is very useful when we want to draw many images with slight differences. Let's say we want to draw two images which picture the degree.

First we create our default setup:

ig = dsslab.net_bench.ImageGenerator(graph)
ig.nodes.set_positions("./positions.json").set_colors(fixed("green"))
ig.edges.set_colors("darkgrey")

The positions, node and edge colors should be constant through all images. We write them to the object ig. We use this object again as follows:

ig.nodes.set_sizes("degree")
(
    ig
    .draw_nodes()
    .draw_edges()
    .write_file("img/degree.svg")
)
ig.nodes.set_sizes("indegree")
(
    ig
    .draw_nodes()
    .draw_edges()
    .write_file("img/indegree.svg")
)

We used the object ig two times and set node sizes to degree the first time and indegree the second time. We also adjusted the filename to reflect that.

Note

Nodes and edges have to be drawn again after changing settings. Edges need the size of the nodes to position the arrow at the end correctly.

Complex colouring

For more complex color settings see here.

Graph Keys (Legends and colorbars)

Note

So-called Graph Keys are made from legends (small boxes which explain qualitative colors and sizes) and a colorbar (which shows the gradient which visualizes the mapping to the corresponding values). They form their own page, which is exported as a separate image.

Legends/Colorbars for the image are drawn on a different page. This page is constructed successively from images with complex mapping. They can represent color or size of nodes respectively. It is automatically determined from the mapping if the image is a textbox or a colorbar.

  • For numerical values, the biggest value, the smallest value and the fallback value are drawn.
  • For qualitative values, every unique value that appears in the network is drawn.

A legend can be drawn with a label if the optional parameter label is used.

Creating a legend with an already existing ImageGenerator ig works like this:

ig.edges.set_sizes("indegree")
ig.set_graph_key(image.edges.sizes, "edges", label="edges widths")

Colorbars are created in a similar way, but they depend on the original mapping, which has to include cmap as a parameter. It works like this:

ig.nodes.set_colors("degree", cmap="viridis") #image.color.sizes wird ein komplexeres Mapping mit einer cmap
ig.set_graph_key(image.color.sizes, label="node degree")