'_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

4 Answers

Start using

nx.draw_networkx(G, with_labels=True)

instead of

nx.draw(G, with_labels=True)
1

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:


EDIT:

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

8

The error is related to a deprecated feature of matplotlib. The problem is occurred because you are running networkx version 2.8.4. (The most recent stable version of networkx now is ver. 3.0.0).

Based on your explanation, I suppose you're using the conda environment, which you can find at . The most recent version is still 2.8.4.

I think the solution for your problem are.

  1. Remove the networkx package from your conda environment (conda uninstall networkx)
  2. Instead of using conda install, use pip install (pip install networkx)
  3. Verify that your current networkx version is now ver 3.

I don't have rep to comment directly on @medium-dimensional. But check if you're managing your env in pycharm with conda. After having the same issue realised that latest version through conda still (until the day of this reply) 2.8.4.

My suggestion trying to run

conda activate <your env> 
pip install --update networkx

pip should catch that you already have the package installed and will update to version 3.0 in that very location

1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Sarah Jenkins

Sarah Jenkins

Senior Technology Editor & AI Specialist

Sarah Jenkins is a veteran tech journalist with over 12 years of experience covering artificial intelligence, mobile innovations, and digital ethics. Her insights have appeared in leading technology publications worldwide.

Share this article
Twitter Facebook Pinterest