0

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())

Result: enter image description here

# 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))

Result: enter image description here

print(G.nodes(),
      G.edges())

Result: enter image description here

#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))

Result: enter image description here


However, when comment-outing 'node_color' and 'node_size', the function works.See: enter image description here

Trenton McKinney
  • 56,955
  • 33
  • 144
  • 158
  • Errors/data/code should be posted as text, not screenshots. – Trenton McKinney Mar 10 '23 at 20:11
  • This question is not reproducible without **data**. This question needs a [SSCCE](http://sscce.org/). Please see [How to provide a reproducible dataframe](https://stackoverflow.com/q/52413246/7758804), then **[edit] your question**, and paste the clipboard into a code block. Always provide a [mre] **with code, data, errors, current output, and expected output, as [formatted text](https://stackoverflow.com/help/formatting)**. If relevant, plot images are okay. If you don't include an mre, it is likely the question will be downvoted, closed, and deleted. – Trenton McKinney Mar 10 '23 at 20:12
  • 2
    https://stackoverflow.com/a/52683100/6361531 Try using `nx.draw_networkx_nodes` – Scott Boston Mar 10 '23 at 20:39

0 Answers0