Search code examples
pythonnetworkxigraph

Prevent nodes from overlapping the edge of the image in python iGraph


I am using iGraph for python to draw some graphs and I cannot seem to prevent the nodes from hitting the edge of the image, especially with very few nodes. In cases with thousands of nodes the opposite is actually true, they are too clustered in the center. Is there a way to influence the behavior of the layout to control the density of hte nodes, or at least stop them from getting cut off? here is the code so far:

    visual_style = {}
    visual_style["vertex_size"] = sz
    visual_style["layout"] = gg.layout_fruchterman_reingold()
    visual_style["bbox"] = (4000, 4000)
    visual_style["margin"] = 20
    visual_style["edge_arrow_size"] = 0
    visual_style["edge_color"] = 'White'
    visual_style['edge_width'] = ew
    visual_style["edge_curved"] = .2
    # print(visual_style)
    plot(gg,"./images/"+str(i)+".png", autocurve=True,rescale=False, background='Black',asp=0, **visual_style)

and a sample of the cut off nodes enter image description here


Solution

  • Yes, there is a way to define a margin within the visual style dictionary. You can use the following command: visual_style['margin'] and define your margins as a dictionary within a dictionary.

    visual_style['unit'] = 'cm'
    visual_style['margin'] = {'top': 1, 'bottom': 1, 'left': 3, 'right': 3}
    

    The code above will return margins with cm measures.