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?

Louise Cullen
  • 97
  • 1
  • 8
  • Since `matplotlib` is a [package dependency](https://github.com/networkx/networkx/blob/main/requirements/default.txt), you can't. – Trenton McKinney Aug 24 '21 at 19:51
  • @TrentonMcKinney I guess my question is more: how can I do it without explicitly calling matplotlib? – Louise Cullen Aug 24 '21 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. – Trenton McKinney Aug 24 '21 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? – Frodnar Aug 25 '21 at 01: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` – Louise Cullen Aug 25 '21 at 12:46
  • Hmm. I can't reproduce this despite installing identical package versions. Interesting that @TrentonMcKinney can. Louise, can you reproduce this in a clean virtual environment? If so, can you provide any other details of the install and kernel startup process you used? Conda? Pip? Install before or after you started the notebook kernel? What OS? – Frodnar Aug 25 '21 at 18:19
  • @Frodnar I tested in `notebook 6.4.3`, `jupyterlab 3.1.7`, `matplotlib 3.4.2`, `jupyter_core 4.7.1`, `jupyter_client 6.1.12`, `networkx 2.6.2`. – Trenton McKinney Aug 25 '21 at 18:23
  • @TrentonMcKinney What about the result of calling `%matplotlib` in the notebook? Mine, for example, returns `Using matplotlib backend: MacOSX`. – Frodnar Aug 25 '21 at 18:45
  • @Frodnar I'm on windows. `%matplotlib` is `Using matplotlib backend: Qt5Agg`. However, I just used `%matplotlib inline` in a cell, which removed the need for `plt.show()`. I haven't had to use `%matplotlib inline` for several years. – Trenton McKinney Aug 25 '21 at 18:51
  • @TrentonMcKinney. HA! It has been years for me as well. Does upgrading to matplotlib 3.4.3 solve it? I briefly searched the [matplotlib issue tracker](https://github.com/matplotlib/matplotlib/issues) but couldn't find a directly related bug report. I'm a little out of my depth when it comes to dealing with matplotlib backends though. – Frodnar Aug 25 '21 at 19:23
  • I'm guessing the only potential resolution for the poster is a matplotlib or jupyter-notebook version change or changing matplotlib backends using [cell magics](https://stackoverflow.com/questions/30878666/matplotlib-python-inline-on-off) or the [matplotlib api](https://stackoverflow.com/questions/26600766/how-to-switch-to-one-of-the-agg-backends) directly. – Frodnar Aug 25 '21 at 19:24

0 Answers0