I'm looking for a solution to map color hex codes, and node size, to the respective nodes.
The code works without color and size. Unfortunately, those two attributes are mandatory for me.
The Code:
import networkx as nx
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline
#import dataset
nodes = pd.read_excel(r'...\Datasets\worksample_networkx_stackoverflow.xlsx')
display(nodes.head(),
nodes.info())
# defining colormap
colormap = {
'buggy':'#458B00',
'moody':'#1E90FF',
'stubborn':'#FF3030',
'brave':'#BCEE68',
'clumsy':'#FF8C00',
'eager':'#8B1A1A',
'cheeky':'#FFD700',
'curios':'#003462',
'nasty':'#E6E6FA',
'funny':'#ADD8E6',
'sleepy':'#68838B',
}
edges = nodes.copy()
#drawing networt
G = nx.Graph()
for index, row in nodes.iterrows():
G.add_node(row["client"], group = row["labels"], nodesize = row["size"] )
for index, row in edges.iterrows():
G.add_edge(row["labels"], row["client"], weight = row["character_magnitude"])
print(nx.info(G))
print(G.nodes(),
G.edges())
#writing function for graph
def draw_graph(G,size):
nodes = G.nodes()
node_color= [colormap[d['labels']] for n,d in G.nodes(data=True)]
node_size = [d['size']*10 for n,d in G.nodes(data=True)]
pos = nx.drawing.spring_layout(G,k=0.70,iterations=60)
plt.figure(figsize=size)
nx.draw_networkx(G,pos=pos,
node_color=node_color,
node_size=node_size,
edge_color='#D1D1D1',
width=1)
plt.show()
draw_graph(G,size=(10,10))
However, when comment-outing 'node_color' and 'node_size', the function works.See: