11

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

Luís Otávio
  • 183
  • 1
  • 13

5 Answers5

8

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.

Sparky05
  • 4,692
  • 1
  • 10
  • 27
  • Weirdly i am alredy using 5.0.1, is there a chance it haven't been fixed yet? – Luís Otávio Apr 02 '21 at 17:26
  • In fact it looks like they are having a problem, they released 5.0.0, 5.0.1, 5.0.2 and 5.0.3 today, neither of them woks, thaks for your help! I'll wait and it wil be surely fixed! – Luís Otávio Apr 02 '21 at 17:35
  • Are you on python2 or python3? I checked decorator project page and they currently don't have any open issues (last solved with 5.0.3, if I understood correctly) – Sparky05 Apr 02 '21 at 17:45
  • I'm using python3, the recently released 5.0.4 and it is still broken. I decided to wait until they stop releasing new versions, by then it should be fixed. For now I'll stick to 4.4.2 – Luís Otávio Apr 03 '21 at 17:35
  • 5.0.5 should now be available and solve your issue - but don't ask me which benefits 5.0.5 has over 4.4.2. – Sparky05 Apr 06 '21 at 09:40
  • Yeah, now it works, I haven't noticed any difference too, and couldn't find nothing on github or the Pypy page – Luís Otávio Apr 07 '21 at 00:53
5

I was using decorator==5.0.6, and got the same error. However, upgrading to 5.0.7 solves my problem.

Mehdi Mostafavi
  • 880
  • 1
  • 12
  • 25
Andy Tseng
  • 51
  • 3
  • NOTE: same issue in July 2021, had `5.0.6` but installing `decorator==5.0.7` worked for me using `pip install decorator==5.0.7` – Chidi Jul 01 '21 at 15:06
2

If you are using MacOS - BigSur, networkx won't work the way you want it to. I needed to go and open my project in Ubuntu.

Dharman
  • 30,962
  • 25
  • 85
  • 135
0

Its worked for me, after downgrade the networkx and decorator

networkx                  2.3                      pypi_0    pypi
decorator                 4.3.0                    pypi_0    pypi

OS: Mac OS BigSur

0

Since I am using Windows 10, the suggestion given by Sparky05 worked for me by updating the version of decorator module in python by using pip install decorator==5.0.7. Also you can update the version of networkx library. I will share the link for updating it later, but it will not work if you are using Spyder IDE. It will work in VSCode.

karel
  • 5,489
  • 46
  • 45
  • 50
Shaina Mehta
  • 120
  • 1
  • 9
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Oct 15 '21 at 01:50