Networkx uses graphviz_layout for layout drawing

When I use python networkx graphviz_layout to draw hierarchical layouts, the corresponding edge disappears in the generated graph when I try to change the weight of the edge.

Below is the specific code and the image:

import numpy as np
import matplotlib.pyplot as plt
import networkx as nx
from networkx.drawing.nx_agraph import graphviz_layout

if __name__ == '__main__':
    edges_weight = np.array([
        [1, 2, 30], [2, 3, 22], [3, 4, 5.97],
        [4, 5, 5.97], [3, 6, 16.03], [6, 7, 4.03],
        [7, 8, 4.03], [5, 8, 5.97], [8, 9, 9],
        [9, 16, 10], [2, 10, 8], [10, 11, 8],
        [11, 12, 2], [12, 13, 2], [11, 13,  9], [13, 15, 8],
        [15, 16, 20], [6, 17, 12], [17, 18, 12],
        [18, 15, 12], [16, 14, 30]])

    G = nx.DiGraph()

    for u, v, w in edges_weight:
        G.add_edge(u, v, weight=w)
    edge_labels = nx.get_edge_attributes(G, 'weight')
    print(edge_labels)
    shell_pos = graphviz_layout(G, prog='dot')

    print(nx.edges(G))
    print(shell_pos.items())
    print(shell_pos.values())

    plt.figure(figsize=(10, 15))
    nx.draw_networkx_nodes(G, shell_pos, node_color='lightblue', node_size=1200)
    nx.draw_networkx_labels(G, shell_pos)
    nx.draw_networkx_edges(G, shell_pos, node_size=1200, arrowstyle='->', arrowsize=10, width=2)
    nx.draw_networkx_edge_labels(G, shell_pos, edge_labels=edge_labels)
    plt.show()

The data is now [11, 13, 10] , and the image is displayed normally at this time

If I change part of the data → [11, 13, 9], the corresponding frame disappears. I don’t know what has happened. Can anyone help me? Thank you very much!

I have searched for various information but have not yet found any valid results

Can you tell if the graphviz text being given to dot has the edge? I would start withfiguring out if the problem is in NetworkX’s output generation, or in graphviz’s rendering. Can you inspect the graphviz text in between the programs?

Hello, I use the code print(nx.edges(G)) to print the edges of the current network graph. The data in the current code is [11, 13, 9], and the result contains edges (11, 13).

[(1.0, 2.0), (2.0, 3.0), (2.0, 10.0), (3.0, 4.0), (3.0, 6.0), (4.0, 5.0), (5.0, 8.0), (6.0, 7.0), (6.0, 17.0), (7.0, 8.0), (8.0, 9.0), (9.0, 16.0), (16.0, 14.0), (10.0, 11.0), (11.0, 12.0), (11.0, 13.0), (12.0, 13.0), (13.0, 15.0), (15.0, 16.0), (17.0, 18.0), (18.0, 15.0)]

Then I used print(shell_pos.items()) to print the position information obtained through the layout algorithm, and found that the abscissas of the current nodes: 11, 12, and 13 are all the same.

dict_items([(1.0, (171.0, 594.0)), (2.0, (171.0, 522.0)), (3.0, (171.0, 450.0)), (4.0, (63.0, 378.0)), (5.0, (27.0, 306.0)), (6.0, (171.0, 378.0)), (7.0, (99.0, 306.0)), (8.0, (99.0, 234.0)), (9.0, (99.0, 162.0)), (16.0, (171.0, 90.0)), (10.0, (243.0, 450.0)), (11.0, (244.0, 378.0)), (12.0, (244.0, 306.0)), (13.0, (244.0, 234.0)), (15.0, (171.0, 162.0)), (17.0, (171.0, 306.0)), (18.0, (171.0, 234.0)), (14.0, (171.0, 18.0))])

Therefore, I think it may still be a problem with layout algorithm calculation, but I am not sure now.

Can you turn it into graphviz code? Graphviz DOT format. That is the format graphviz accepts, we are not familiar with NetworkX library. If you can frame your problem ideally in terms of running graphviz (“dot”) on some input from the command line and getting unexpected output, then we can understand and help.

Thank you, I changed the method and found that it works. Here is the code:

import numpy as np
from graphviz import Digraph

if __name__ == '__main__':
    edges_weight = np.array([
        [1, 2, 30], [2, 3, 22], [3, 4, 5.97],
        [4, 5, 5.97], [3, 6, 16.03], [6, 7, 4.03],
        [7, 8, 4.03], [5, 8, 5.97], [8, 9, 9],
        [9, 16, 10], [2, 10, 8], [10, 11, 8],
        [11, 12, 2], [12, 13, 2], [11, 13, 9], [13, 15, 8],
        [15, 16, 20], [6, 17, 12], [17, 18, 12],
        [18, 15, 12], [16, 14, 30]])

    dot = Digraph(format='dot')
    dot.attr(size='15,15')
    for u, v, w in edges_weight:
        dot.edge(str(u), str(v), label=str(w))
    dot.render('demo', format='png')

I initially used the graphviz_layout that comes with the networkx library. Maybe it has a bug. Now it is normal to use the graphviz api directly. This is my problem. I didn’t read the graphviz documentation carefully. Thank you very much!