Leonhart Dreyse
Leonhart Dreyse

Reputation: 311

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

Upvotes: 21

Views: 32190

Answers (4)

medium-dimensional
medium-dimensional

Reputation: 2253

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.

Upvotes: 7

Mohamad Sobhi
Mohamad Sobhi

Reputation: 401

Start using

nx.draw_networkx(G, with_labels=True)

instead of

nx.draw(G, with_labels=True)

Upvotes: 39

Alif Nur Iman
Alif Nur Iman

Reputation: 61

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 https://anaconda.org/conda-forge/networkx. 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.

Upvotes: 6

avalencia
avalencia

Reputation: 63

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

Upvotes: 0

Related Questions