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

Source: Internet
Author: User

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

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.