0

I want to plot a graph using networkx. But module 'matplotlib.pyplot' has no attribute 'ishold' error is coming. I have tried using the earlier versions of matplotlib but it didn't work. Yesterday, the same code was running but now it is showing this error. Please help. Im stuck.

I have tried using the earlier versions of matplotlib but it didn't work. Yesterday, the same code was running but now it is showing this error. Please help. Im stuck.

import networkx as nx
import numpy as np
import matplotlib.pyplot as plt

G = nx.Graph()
G.add_edges_from(
    [('A', 'B'), ('A', 'C'), ('D', 'B'), ('E', 'C'), ('E', 'F'),
     ('B', 'H'), ('B', 'G'), ('B', 'F'), ('C', 'G')])

val_map = {'A': 1.0,
           'D': 0.5714285714285714,
           'H': 0.0}

values = [val_map.get(node, 0.25) for node in G.nodes()]
%matplotlib inline 
nx.draw_networkx(G, cmap = plt.get_cmap('jet'), node_color = values)

nx.draw(G)
plt.show()

AttributeError                            Traceback (most recent call last)
<ipython-input-13-127734f4f34b> in <module>()
     14 values = [val_map.get(node, 0.25) for node in G.nodes()]
     15 get_ipython().magic('matplotlib inline')
---> 16 nx.draw_networkx(G, cmap = plt.get_cmap('jet'), node_color = values)
     17 
     18 nx.draw(G)

1 frames
/usr/local/lib/python3.6/dist-packages/networkx/drawing/nx_pylab.py in draw_networkx_edges(G, pos, edgelist, width, edge_color, style, alpha, edge_cmap, edge_vmin, edge_vmax, ax, arrows, label, **kwds)
    520         lw = width
    521 
--> 522     if not cb.is_string_like(edge_color) \
    523            and cb.iterable(edge_color) \
    524            and len(edge_color) == len(edge_pos):

AttributeError: module 'matplotlib.cbook' has no attribute 'is_string_like'

enter image description here

sentence
  • 8,213
  • 4
  • 31
  • 40
anonymous
  • 21
  • 1
  • 5
  • The error shown is a totally different one than the one reported in the title. Which one do you want to inquire about? What are the versions of the libraries in use? – ImportanceOfBeingErnest Jun 03 '19 at 11:10
  • My code worked for matplotlib version 2.2.3 . In other versions, it was either showing 'matplotlib.pyplot' has no attribute 'ishold' or the error that I mentioned in my code. Thank you anyways! – anonymous Jun 04 '19 at 12:05
  • So your networkx version is not compatible with your matplotlib version. That's all one can say. – ImportanceOfBeingErnest Jun 04 '19 at 12:22
  • yeah.. I figured that later. But it seems the recent version of matplotlib does not support nx.draw functionality. – anonymous Jun 04 '19 at 12:34
  • networkx is not a dependency of matplotlib, but rather matplotlib is a dependency of networkx. So if anything, networkx might be broken. – ImportanceOfBeingErnest Jun 04 '19 at 12:54

2 Answers2

2

I had the same issue, it's due to a mismatch in the versions of networkx and matplotlib. I tried uninstalling and reinstalling the packages - it worked!

sudo pip3 uninstall networkx

sudo pip3 uninstall matplotlib

python3 -mpip uninstall matplotlib

sudo conda uninstall matplotlib

If you've installed networkx via other sources like mpip or conda, uninstall via those too.

Then install both of them:

sudo pip3 install networkx

sudo pip3 install matplotlib
Ajay Sivan
  • 2,807
  • 2
  • 32
  • 57
Bharg
  • 21
  • 2
1

I downgraded matplotlib version to 2.2.3 and it works.

just input

pip install matplotlib==2.2.3.

hope it helps~