matplotlib - Python networks change color of nodes when using draw_network_nodes() -
the goal obtain similar to
to define graph use:
import matplotlib.pyplot plt import networkx 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() mg.add_edges_from([(start, stop, {'weigth' : len(graph[start]) }) start in graph stop in graph[start]])
and code plot is:
plt.figure(figsize=(8,8)) pos=nx.graphviz_layout(mg,prog="twopi",root='1') n in mg.nodes_iter(): nx.draw_networkx_nodes(mg, pos, nodelist = [n], node_size = 2000 / float(len(mg[n[0]])+1), node_color = (len(mg[n[0]])+1), alpha = 1/float(len(mg[n[0]])+1), with_labels=true ) xmax=1.1*max(xx xx,yy in pos.values()) ymax=1.1*max(yy xx,yy in pos.values()) plt.xlim(0,xmax) plt.ylim(0,ymax) plt.show()
there 2 things here know:
how can have color node depending on number of links each node has (i.e. len(mg[0])
)?
where labels?
thanks
how this? can utilize matplotlib's colormaps map values colors nodes.
import matplotlib.pyplot plt import networkx 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() grade = mg.degree() color = [degree[n] n in nodes] size = [2000 / (degree[n]+1.0) 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()
python matplotlib networkx
No comments:
Post a Comment