绘制networkx图时出现意外错误

时间:2021-04-02 16:29:58

标签: python networkx

我有一个简单的图形,需要在我的屏幕上绘制它,这是我的代码:

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

nx.draw(grafo)
plt.show()

return grafo

其中矩阵是包含连接权重的邻接列表。 coda 工作得很好,但我不得不创建一个新的 python virtualenv,从那时起,即使正确安装了所有必需的库,它也会在 nx.draw() 调用中引发错误。我得到的错误是:

    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

这是我的代码错误吗?如果是这样,我该怎么办?提前致谢

3 个答案:

答案 0 :(得分:6)

如该问题所述:networkx shows random_state_index is incorrect 他们的df$c <- c # not strictly required ggplot(df, aes(reorder(a, -c), b)) + geom_tile(aes(fill = c)) + scale_fill_gradientn( limits = range(c), colors = pallette, guide = guide_colorbar( frame.colour = "black", ticks.colour = "black" ) ) 有问题。正如 GitHub (https://github.com/networkx/networkx/issues/4718) 上的相关问题中所述,decorator=5.0.0>=5.0.X 应该很快可用。因此,请稍等片刻,按照上面 SO 问题中的建议升级或降级到旧版本。

编辑 decorator==5.0.5 或 >=5.0.7 修复错误

如上面链接的问题所述,装饰器现已更新和修复。

答案 1 :(得分:3)

我正在使用 decorator==5.0.6,但遇到了同样的错误。 但是,升级到 5.0.7 解决了我的问题。

答案 2 :(得分:1)

如果您使用的是 MacOS - BigSur,networkx 将无法按您希望的方式工作。我需要在 Ubuntu 中打开我的项目。