2

I am making interactive worksheets in Jupyter Notebook, introducing people to NetworkX. Suddenly, the nx.draw() command is not creating an output of a graph. It was working fine before, and I haven't changed anything about the code I was using. For example, one block of code that was working fine before was:

import networkx as nx

G2 = nx.Graph()

G2.add_nodes_from([1,2,3,4,5,6,7])

edgelist = [(1,4),(1,6),(1,7),(2,5),(2,6),(3,7),(4,6),(4,7),(5,7),(6,7)]
G2.add_edges_from(edgelist)

nx.draw_networkx(G2, with_labels=True, node_color = 'y') 

Now, no errors come up, but neither does a graph. I have looked and seen that other people fixed this issue bu using plt.show() but I don't understand why I suddenly need to include that when I didn't have to before. Does anyone know a way I can avoid importing matplotlib and using the plt.show() command?

Perhaps the issue is to do with some other package I have or something to do with jupyter notebook?

EDIT: It has been pointed out to me that matplotlib is a package dependency so technically there is no way of drawing a graph in networkx without using matplotlib. I understand that, but is there a way to do it without explicitly calling matplotlib?

11
  • Since matplotlib is a package dependency, you can't. Commented Aug 24, 2021 at 19:51
  • @TrentonMcKinney I guess my question is more: how can I do it without explicitly calling matplotlib? Commented Aug 24, 2021 at 19:53
  • You can't. There was an update, at least in Jupyter Lab, now plt.show seems to be required. Perhaps, updates to matplotlib make it required. I just know it's now required. Commented Aug 24, 2021 at 19:53
  • @LouiseCullen It works for me in the most updated pypi versions of all relevant packages in both a Jupyter Notebook and Jupyter Lab. What are the outputs of nx.__version__ and !jupyter --version from within the notebook? Commented Aug 25, 2021 at 1:42
  • @Frodnar the output of nx.version is '2.6.2' and the output of !jupyter --version is jupyter core: 4.7.1, jupyter-notebook: 6.4.3, qtconsole: 5.1.0, ipython: 7.26.0, ipykernel: 5.3.4, jupyter client: 6.1.12, jupyter lab: 3.1.7, nbconvert: 6.1.0, ipywidgets: 7.6.3, nbformat: 5.1.3, traitlets: 5.0.5 Commented Aug 25, 2021 at 12:46

0

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.