Home > database >  TypeError due to float() argument when I create a network with nodes colored using a colormap
TypeError due to float() argument when I create a network with nodes colored using a colormap

Time:09-22

I have the following two datasets

Node     Edge
A         B
A         D
B         N
B         A
B         X
S         C

and

Nodes    Attribute
A           -9
B           
C           -1.5
D           1.0
...
N           1.0
... 
X           0.0
Y           -1.5
W          -1.5
Z           1.0

where Attribute type is float64. For replication, you can use the following (unique Attribute elements, i.e., uni_val): array([ 9. , 0. , 1. , 0.5, -0.5, -1. , -1.5, -2. ])

I want to create a network using a color map for coloring nodes. I did as follows:

# Create map of color

uni_val=df2.Attribute.unique()
colors = plt.cm.jet(np.linspace(0,1,len(df2.Attribute.unique())))

n=0
val=[]
col=[]

for i in uni_val:
    val.append(i)
    col.append(colors[n])
    n=n 1

mapper=dict(zip(val,col))
colour_map = df2.set_index('Nodes')['Attribute'].map(mapper)

# Create the network

G = nx.from_pandas_edgelist(df1, source='Node', target='Edge')
# Add Attribute to each node
nx.set_node_attributes(G, colour_map, name="colour")

# Then draw with colours based on attribute values:
nx.draw(G, 
        node_color=nx.get_node_attributes(G, 'colour').values(),
        with_labels=True)

I am getting the following error:

TypeError: float() argument must be a string or a number, not 'dict_values'

but I do not understand what is causing it and how I can fix it. It would be great if you could help me to better understand it and show me a way to fix it.

CodePudding user response:

The current mapping is incorrect. A valid colour code format is needed not arrays:

From the docs coloured graph

  • Related