Search code examples
pythonnetworkx

Unexpected error while drawing networkx graph


I have a simple graph and need to draw it on my screen, here is my code:

def gera_grafo(matriz):
grafo = nx.to_networkx_graph(matriz, create_using=nx.Graph)

nx.draw(grafo)
plt.show()

return grafo

Where matrix is an adjacency list containing the weights of the connections. The coda was working just fine, but i had to create a new python virtualenv and since then, even though all the required libraries are correctly installed it throws an error on the nx.draw() call. The error I got is:

    Traceback (most recent call last):
  File "/run/media/luisola/A216C03316C009ED/Users/Luis/Documents/Iniciação Científica/inicia-o-cient-fica/venv/lib/python3.9/site-packages/networkx/utils/decorators.py", line 396, in _random_state
    random_state_arg = args[random_state_index]
IndexError: tuple index out of range

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/run/media/luisola/A216C03316C009ED/Users/Luis/Documents/Iniciação Científica/inicia-o-cient-fica/criacao_do_grafo.py", line 58, in <module>
    grafo = gera_grafo(matriz)
  File "/run/media/luisola/A216C03316C009ED/Users/Luis/Documents/Iniciação Científica/inicia-o-cient-fica/criacao_do_grafo.py", line 39, in gera_grafo
    nx.draw(grafo)
  File "/run/media/luisola/A216C03316C009ED/Users/Luis/Documents/Iniciação Científica/inicia-o-cient-fica/venv/lib/python3.9/site-packages/networkx/drawing/nx_pylab.py", line 123, in draw
    draw_networkx(G, pos=pos, ax=ax, **kwds)
  File "/run/media/luisola/A216C03316C009ED/Users/Luis/Documents/Iniciação Científica/inicia-o-cient-fica/venv/lib/python3.9/site-packages/networkx/drawing/nx_pylab.py", line 333, in draw_networkx
    pos = nx.drawing.spring_layout(G)  # default to spring layout
  File "/run/media/luisola/A216C03316C009ED/Users/Luis/Documents/Iniciação Científica/inicia-o-cient-fica/venv/lib/python3.9/site-packages/decorator.py", line 214, in fun
    return caller(func, *(extras + args), **kw)
  File "/run/media/luisola/A216C03316C009ED/Users/Luis/Documents/Iniciação Científica/inicia-o-cient-fica/venv/lib/python3.9/site-packages/networkx/utils/decorators.py", line 400, in _random_state
    raise nx.NetworkXError("random_state_index is incorrect") from e
networkx.exception.NetworkXError: random_state_index is incorrect

Is this an error on my code? If so, what can i do? Thanks in advance


Solution

  • As stated for this question: networkx shows random_state_index is incorrect Their was a problem with decorator=5.0.0. As discussed in the related issue on GitHub (https://github.com/networkx/networkx/issues/4718) decorator>=5.0.X should be available soon. So either wait a little bit to upgrade or downgrade to an old version as suggested in the SO question above.

    Edit decorator==5.0.5 or >=5.0.7 fixes the error

    As discussed in the issue linked above, decorator has now been updated and fixed.