Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

'_AxesStack' object is not callable while using networkx to plot

Following one of online tutorials I found it difficult to run even a small piece of paragraph. Here is what I want to write into the graph: a Directed acyclic unweighted matrix into the graph, however I encountered the problem of matrix-representing and pic-representing. The former gives a warning and substitutes output matrix with arrays. However the pic plotting is always output only errors I don't know why. Here is my code

import networkx as nx
import matplotlib.pyplot as plt
import matplotlib
matplotlib.use('TkAgg')
import numpy as np
A = np.array([[0,1,1,0],
              [1,0,1,1],
              [1,1,0,0],
              [0,1,0,0]])
G=nx.from_numpy_array(A)
nx.draw(G,with_lables=True)

And my networkx version is 2.8.4, matplotlib version is 3.6.0, matching the versions tutorial mentioned. Here is the error:

'_AxesStack' object is not callable
like image 265
Leonhart Dreyse Avatar asked Nov 19 '25 20:11

Leonhart Dreyse


2 Answers

Start using

nx.draw_networkx(G, with_labels=True)

instead of

nx.draw(G, with_labels=True)
like image 62
Mohamad Sobhi Avatar answered Nov 21 '25 10:11

Mohamad Sobhi


The argument that draw() supports is with_labels. Please fix your typo.

The error has been discussed on this thread of networkx's GitHub repository, and suggests users with matplotlib 3.6.0rc1 to update networkx to its latest version.

With the fixed typo, and networkx version 2.8.7 and matplotlib version 3.5.1, the code produces following figure:

enter image description here


EDIT:

Please consider this answer from Mohamad Sobhi if you are using networkx v3.1, and do not wish to downgrade Matplotlib.

like image 30
medium-dimensional Avatar answered Nov 21 '25 09:11

medium-dimensional



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!