First, you enter the weights for edges and edges, and then you draw the node position. Dividing real and imaginary edges by weight size
#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 netw Orkx 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
Python-networkx: Drawing According to the weight of the graph