Python-networkx: Drawing According to the weight of the graph

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.