Skip to content Skip to sidebar Skip to footer

Python Networks Change Color Of Nodes When Using Draw_network_nodes()

The goal is to obtain something similar to To define the graph I use: import matplotlib.pyplot as plt import networkx as nx graph = { '1': ['2', '3', '4'], '2': ['5','11'

Solution 1:

How about this? You can use matplotlib's colormaps to map values to colors for the nodes.

import matplotlib.pyplot as plt
import networkx as nx
graph = {
    '1': ['2', '3', '4'],
    '2': ['5','11','12','13','14','15'],
    '3' : ['6','7','66','77'],
    '5': ['6', '8','66','77'],
    '4': ['7','66','77'],
    '7': ['9', '10']
    }

MG = nx.DiGraph(graph)


plt.figure(figsize=(8,8))
pos=nx.graphviz_layout(MG,prog="twopi",root='1')

nodes = MG.nodes()
degree = MG.degree()
color = [degree[n] for n in nodes]
size = [2000 / (degree[n]+1.0) for n in nodes]

nx.draw(MG, pos, nodelist=nodes, node_color=color, node_size=size,
        with_labels=True, cmap=plt.cm.Blues, arrows=False)
plt.show()

enter image description here

Post a Comment for "Python Networks Change Color Of Nodes When Using Draw_network_nodes()"