How to adjust vertex size on plot based on the number of connections a vertex has

I have a directed graph which is filled and displayed in the following way:

    def visualize_relations(self, relations: List[Relation]) -> None:
        extracted_triplets = []
        for relation in relations:
            triplet = [relation.from_item.identifier, relation.connecting_property.identifier, relation.to_item.identifier]
            extracted_triplets.append(triplet)

        g = Graph(directed=True)

        for triplet in extracted_triplets:
            subj_vertex = self.add_or_find_vertex(g, triplet[0])
            obj_vertex = self.add_or_find_vertex(g, triplet[2])
            g.add_edge(subj_vertex, obj_vertex, uri=triplet[1], label=triplet[1])

        visual_style = {}
        colours = ['#fecc5c', '#a31a1c']
        visual_style["bbox"] = (4000, 4000)
        visual_style["margin"] = 300
        visual_style["vertex_color"] = 'grey'
        visual_style["vertex_size"] = 300
        visual_style["vertex_label_size"] = 100
        visual_style["edge_curved"] = False
        my_layout = g.layout_fruchterman_reingold()
        visual_style["layout"] = my_layout

        plot(g, **visual_style)
        plot(g, "output/visualize_relations.png", **visual_style)

    def add_or_find_vertex(self, g, uri):
        try:
            return g.vs.find(name=uri)
        except (KeyError, ValueError):
            g.add_vertex(name=uri, label=uri)
            return g.vs.find(name=uri)

I would like to make each node’s size on the plot analogous to the number of incoming connections it has. How could I do that? Note that the graphs I’m planning to plot will not be too large, and therefore I don’t mind more computationally intensive solutions.

igraph-python version: 0.8.3
OS: Windows 10
Python 3.8

Simply set the vertex_size to be the in-degree:

import igraph as ig
g=ig.Graph.Barabasi(50, 2, directed=True)
ig.plot(g, vertex_size=g.degree(mode="in"))

Here’s a version which enforces a minimum vertex size, scales the vertex area (not radius) with in-degree, and makes the plot a bit nicer.

ig.plot(g, vertex_size=[max(1, 5*math.sqrt(d)) for d in g.degree(mode="in")], edge_arrow_size=0.5, bbox=(300,300))

You really should upgrade to the 0.9 series. 0.9.6 is the latest version.

1 Like