1

I'm trying to visualize graph in NetworkX. I need to colorize the graph like this: center node needs to be colored dark. Then, all nodes that are further away will need to be colored lighter, but when i run the code i get this error :

error: Cannot convert argument type < class 'numpy.ndarray' > to rgba array on the line :

  nx.draw_networkx_nodes(G,pos,nodelist=p.keys(),node_size=90,
  node_color=p.values(),cmap=plt.cm.Reds_r)

I think the problem is in:

node_color=p.values()

The code is:

import numpy
import pandas
import networkx as nx
import unicodecsv as csv
import community
import matplotlib.pyplot as plt

# Generate the Graph
G=nx.davis_southern_women_graph()

# Create a Spring Layout
pos=nx.spring_layout(G)

# Find the center Node
dmin=1
ncenter=0
for n in pos:
   x,y=pos[n]
   d=(x-0.5)**2+(y-0.5)**2
   if d<dmin:
      ncenter=n
      dmin=d

""" returns a dictionary of nodes and their distance to the node 
supplied as an argument. We will then use these distances 
to determine colors"""
p=nx.single_source_shortest_path_length(G,ncenter)

plt.figure(figsize=(8,8))
nx.draw_networkx_edges(G,pos,nodelist=[ncenter],alpha=0.4)
nx.draw_networkx_nodes(G,pos,nodelist=p.keys(),node_size=90,
   node_color=p.values(),cmap=plt.cm.Reds_r)

plt.show()

Full Traceback

Traceback (most recent call last):

  File "<ipython-input-4-da1414ba5e14>", line 1, in <module>
   runfile('C:/Users/Desktop/Marvel/finding_key_players.py',        wdir='C:/Users/Desktop/Marvel')

  File "C:\Users\Anaconda33\lib\site-   packages\spyderlib\widgets\externalshell\sitecustomize.py", line 685, in runfile
   execfile(filename, namespace)

  File "C:\Users\Anaconda33\lib\site packages\spyderlib\widgets\externalshell\sitecustomize.py", line 85, in execfile
exec(compile(open(filename, 'rb').read(), filename, 'exec'), namespace)

  File "C:/Users/Desktop/Marvel/finding_key_players.py", line 70, in
   <module>
      cmap=plt.cm.Reds_r)

  File "C:\Users\Anaconda33\lib\site-packages\networkx\drawing\nx_pylab.py", line 399, in draw_networkx_nodes
label=label)

  File "C:\Users\Anaconda33\lib\site-packages\matplotlib\axes\_axes.py", line 3606, in scatter
colors = mcolors.colorConverter.to_rgba_array(c, alpha)

  File "C:\Users\Anaconda33\lib\site-packages\matplotlib\colors.py",   line 391, in to_rgba_array
if alpha > 1 or alpha < 0:

ValueError: Cannot convert argument type <class 'numpy.ndarray'> to rgba array
9
  • Maybe you have a broken (or just older) version of either matplotlib or networkx? I have matplotlib-1.3.1 and networkx-1.11 and it works with those. Commented Feb 15, 2016 at 22:48
  • I upgraded both modules, but the error is still the same. Commented Feb 15, 2016 at 23:24
  • OK - to help you we'll need the full traceback with the error details. I believe your code is correct. It likely is a mismatch or problem with one of the libraries, likely matplotlib or numpy. Commented Feb 15, 2016 at 23:32
  • I put the whole traceback under the code. I thought the error is in matplotlib/colors.py, so i run over the original colors.py file with another, but obviously the previous file was OK too, because the error stays the same. Thank you for your help Commented Feb 16, 2016 at 13:18
  • Thanks for the traceback. I can see the error but I don't understand why it is getting triggered. Maybe someone else is familiar with this issue. It doesn't look like a networkx bug to me. Commented Feb 17, 2016 at 1:51

1 Answer 1

1

The error is in the function for drawing nodes. p.keys()values must be put in a list for nodelist, and node_color, otherwise it's not working.

So the correct line is:

nx.draw_networkx_nodes(G,pos,nodelist=list(p.keys()),node_size=80,node_color=list(p.values()), cmap=plt.cm.Reds_r)
plt.axis('off')
plt.show()
Sign up to request clarification or add additional context in comments.

Comments

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.