First, the weights of the edges and edges are entered, then the node positions are drawn, and the real and imaginary edges are divided according to the weight size.
<pre name= "code" class= "Python" > #coding: Utf-8#!/usr/bin/env Python "" "An example using Graph as a weighted network." "" __author__ = "" "Aric Hagberg ([email protected])" "" Try:import Matplotlib.pyplot as Pltexcept:raiseimport NE Tworkx as Nxg=nx. Graph () #添加带权边G. Add_edge (' A ', ' B ', weight=0.6) G.add_edge (' A ', ' C ', weight=0.2) G.add_edge (' C ', ' d ', weight=0.1) g.add_ Edge (' C ', ' e ', weight=0.7) G.add_edge (' C ', ' F ', weight=0.9) G.add_edge (' A ', ' d ', weight=0.3) #按权重划分为重权值得边和轻权值的边elarge =[ (u,v) for (u,v,d) in G.edges (data=true) if d[' weight '] >0.5]esmall=[(u,v) for (u,v,d) in G.edges (data=true) if d[' weigh T '] <=0.5] #节点位置pos =nx.spring_layout (G) # Positions for all nodes# first draw the node position # nodesnx.draw_networkx_nodes (g,pos,node_ size=700) #根据权重, a solid line is a weight-weighted edge, and dashed lines are weighted small edges # edgesnx.draw_networkx_edges (g,pos,edgelist=elarge, width=6) nx.draw_ne Tworkx_edges (G,pos,edgelist=esmall, width=6,alpha=0.5,edge_color= ' B ', style= ' dashed ') # labels label definition Nx.draw _networkx_labels (G,POS,FONT_SIZE=20,FOnt_family= ' Sans-serif ') plt.axis (' Off ') plt.savefig ("Weighted_graph.png") # Save As Pngplt.show () # Display
Copyright NOTICE: Welcome reprint, Reprint please indicate the source http://blog.csdn.net/ztf312/
Python-networkx: Drawing According to the weight of the graph