python networkx 根據圖的權重畫圖實現(xiàn)
更新時間:2019年07月10日 09:46:17 作者:CS青雀
這篇文章主要介紹了python networkx 根據圖的權重畫圖實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
首先輸入邊和邊的權重,隨后畫出節(jié)點位置,根據權重大小劃分實邊和虛邊

#coding:utf-8
#!/usr/bin/env python
"""
An example using Graph as a weighted network.
"""
__author__ = """Aric Hagberg (hagberg@lanl.gov)"""
try:
import matplotlib.pyplot as plt
except:
raise
import networkx as nx
G=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['weight'] <=0.5]
#節(jié)點位置
pos=nx.spring_layout(G) # positions for all nodes
#首先畫出節(jié)點位置
# nodes
nx.draw_networkx_nodes(G,pos,node_size=700)
#根據權重,實線為權值大的邊,虛線為權值小的邊
# edges
nx.draw_networkx_edges(G,pos,edgelist=elarge,
width=6)
nx.draw_networkx_edges(G,pos,edgelist=esmall,
width=6,alpha=0.5,edge_color='b',style='dashed')
# labels標簽定義
nx.draw_networkx_labels(G,pos,font_size=20,font_family='sans-serif')
plt.axis('off')
plt.savefig("weighted_graph.png") # save as png
plt.show() # display
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
Python的Django框架中settings文件的部署建議
這篇文章主要介紹了Python的Django框架中settings文件的部署建議,包括對local_settings的弊病的一些簡單分析,需要的朋友可以參考下2015-05-05
使用Python對Syslog信息進行分析并繪圖的實現(xiàn)
這篇文章主要介紹了使用Python對Syslog信息進行分析并繪圖的實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2021-04-04
Python strip lstrip rstrip使用方法
Python中的strip用于去除字符串的首位字符,同理,lstrip用于去除左邊的字符,rstrip用于去除右邊的字符。這三個函數(shù)都可傳入一個參數(shù),指定要去除的首尾字符。2008-09-09

