日韩久久久精品,亚洲精品久久久久久久久久久,亚洲欧美一区二区三区国产精品 ,一区二区福利

Python Matplotlib 基于networkx畫關(guān)系網(wǎng)絡(luò)圖

系統(tǒng) 1685 0

前言

昨天才開始接觸,鼓搗了一個(gè)下午,接下來會(huì)持續(xù)更新,如果哪里有錯(cuò)誤的地方,望各位大佬指出,謝謝!

數(shù)據(jù)描述

兩個(gè)文件,一個(gè)文件包含了網(wǎng)絡(luò)圖的節(jié)點(diǎn),節(jié)點(diǎn)存在類別(0,1,2,3)四類,但是0類別舍去,不畫出;另一個(gè)文件包含了網(wǎng)絡(luò)圖的邊,數(shù)據(jù)基本特征如下:

Python Matplotlib 基于networkx畫關(guān)系網(wǎng)絡(luò)圖_第1張圖片 ? ? ? ? ? ? ? Python Matplotlib 基于networkx畫關(guān)系網(wǎng)絡(luò)圖_第2張圖片

圖1中,id表示節(jié)點(diǎn),b是類別;圖2中,兩個(gè)數(shù)字表示邊連接的兩個(gè)點(diǎn)。

Networkx

安裝

我的系統(tǒng)是Mac OS,直接在terminal輸入sudo pip install networkx就可以安裝,由于代碼中涉及幾個(gè)函數(shù),在python3中會(huì)報(bào)錯(cuò),我用python2.7.13實(shí)現(xiàn)的

基本使用方法

            
import networkx as nx          #導(dǎo)入networkx包
import matplotlib.pyplot as plt   #導(dǎo)入繪圖包matplotlib(需要安裝,方法見第一篇筆記)
G =nx.random_graphs.barabasi_albert_graph(100,1)  #生成一個(gè)BA無標(biāo)度網(wǎng)絡(luò)G
nx.draw(G)             #繪制網(wǎng)絡(luò)G
plt.savefig("ba.png")      #輸出方式1: 將圖像存為一個(gè)png格式的圖片文件
plt.show()              #輸出方式2: 在窗口中顯示這幅圖像
          

參數(shù)介紹基本

- `node_size`: 指定節(jié)點(diǎn)的尺寸大小(默認(rèn)是300,單位未知,就是上圖中那么大的點(diǎn))
- `node_color`: 指定節(jié)點(diǎn)的顏色 (默認(rèn)是紅色,可以用字符串簡單標(biāo)識(shí)顏色,例如'r'為紅色,'b'為綠色等,具體可查看手冊(cè))
- `node_shape`: 節(jié)點(diǎn)的形狀(默認(rèn)是圓形,用字符串'o'標(biāo)識(shí),具體可查看手冊(cè))
- `alpha`: 透明度 (默認(rèn)是1.0,不透明,0為完全透明)
- `width`: 邊的寬度 (默認(rèn)為1.0)
- `edge_color`: 邊的顏色(默認(rèn)為黑色)
- `style`: 邊的樣式(默認(rèn)為實(shí)現(xiàn),可選: solid|dashed|dotted,dashdot)
- `with_labels`: 節(jié)點(diǎn)是否帶標(biāo)簽(默認(rèn)為True)
- `font_size`: 節(jié)點(diǎn)標(biāo)簽字體大小 (默認(rèn)為12)
- `font_color`: 節(jié)點(diǎn)標(biāo)簽字體顏色(默認(rèn)為黑色)

布局

  • circular_layout:節(jié)點(diǎn)在一個(gè)圓環(huán)上均勻分布
  • random_layout:節(jié)點(diǎn)隨機(jī)分布
  • shell_layout:節(jié)點(diǎn)在同心圓上分布
  • spring_layout: 用Fruchterman-Reingold算法排列節(jié)點(diǎn)
  • spectral_layout:根據(jù)圖的拉普拉斯特征向量排列節(jié)點(diǎn)

代碼

            
# coding:utf-8
 
 
import networkx as nx 
import matplotlib.pyplot as plt
import csv
 
with open('node-8.csv','rb') as csvfile:
	reader = csv.DictReader(csvfile)
	column = [row['b'] for row in reader]
	id_tag0 = [row['id'] for row in reader]
#print column
id_tag = []
for item in id_tag0:
	id_tag.append(int(item))
 
# =================Setting node parameters====================
node_0 = []
node_1 = []
node_2 = []
node_3 = []
node_color = []
node_color_y = []
node_color_r = []
node_color_g = []
node_color_b = []
node_shape = []
node_shape_0 = []
node_shape_1 = []
node_shape_2 = []
node_shape_3 = []
 
for i in range(len(column)):
	if int(column[i]) == 0:
		pass
	elif int(column[i]) == 1:
		color = 'r'
		shape = 'o'
		node_1.append(i)
		node_color_r.append(color)
		node_shape_1.append(shape)
	elif int(column[i]) == 2:
		color = 'g'
		shape = 'o'
		node_2.append(i)
		node_color_g.append(color)
		node_shape_2.append(shape)
	else:
		color = 'b'
		shape = '*'
		node_3.append(i)
		node_color_b.append(color)
		node_shape_3.append(shape)
	node_color.append(color)
	node_shape.append(shape)
# ==============================================================
 
 
with open('node-8.csv','rb') as csvfile:
	reader = csv.DictReader(csvfile)
	column1 = [row['b'] for row in reader]
	id_tag1 = [row['id'] for row in reader]
#print column
id_tag11 = []
for item in id_tag1:
	id_tag11.append(int(item))
 
 
 
edge = []
with open('edge-8.txt','r') as f: 
	data = f.readlines() 
	for line in data:
		#print line
		line = tuple(line.replace('\r','').replace('\n','').replace('\t','').split(','))
		edge.append(line)
#print edge
 
# ===============Setting edge parameters=========================
edge_color = []
edge_style = []
 
for item in edge:
	#print item
	if int(column1[int(item[0])]) == 0 or int(column1[int(item[1])]) == 0:
		pass
	elif int(column1[int(item[0])]) == 1 or int(column1[int(item[1])]) == 1:
		color = 'r'
		#style0 = 'dashdot'
		#color_r_list.append(color)
	elif int(column1[int(item[0])]) == 2 or int(column1[int(item[1])]) == 2:
		color = 'g'
		#style0 = 'dashed'
		#color_r_list.append(color)
	else:
		color = 'b'
		#style0 = 'dotted'
		#color_b_list.append(color)
	edge_color.append(color)
	#edge_style.append(style0)
 
 
G = nx.Graph()
#G.add_nodes_from(id_tag)
G.add_edges_from(edge)
 
#nx.draw(G,pos=nx.random_layout(G), nodelist = node_0, node_color = node_color_y, node_size=120, node_shape=node_shape_0)
#nx.draw(G,pos=nx.random_layout(G), nodelist = node_1, node_color = node_color_r, node_size=120, node_shape=node_shape_1)
#nx.draw(G,pos=nx.random_layout(G), nodelist = node_2, node_color = node_color_g, node_size=120, node_shape=node_shape_2)
#nx.draw(G,pos=nx.random_layout(G), nodelist = node_3, node_color = node_color_b, node_size=120, node_shape=node_shape_3)
 
nx.draw_networkx(G,pos=nx.random_layout(G),node_color=node_color,node_size=10,node_shape='o',edge_color=edge_color,width=0.3,style='solid',font_size=8) 
#nx.draw_networkx(G,pos=nx.random_layout(G),nodelist = node_1,node_color=node_color,node_size=100,node_shape='o',style='dashdot') 
#nx.draw_networkx(G,pos=nx.random_layout(G),node_color=color_g_list,node_size=150,node_shape='^',style='dashed') 
#nx.draw_networkx(G,pos=nx.random_layout(G),node_color=color_b_list,node_size=150,node_shape='*',style='dotted') 
 
#plt.legend()
#nx.draw_networkx(G)
plt.show()
          

畫圖

Python Matplotlib 基于networkx畫關(guān)系網(wǎng)絡(luò)圖_第3張圖片

以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。


更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號(hào)聯(lián)系: 360901061

您的支持是博主寫作最大的動(dòng)力,如果您喜歡我的文章,感覺我的文章對(duì)您有幫助,請(qǐng)用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點(diǎn)擊下面給點(diǎn)支持吧,站長非常感激您!手機(jī)微信長按不能支付解決辦法:請(qǐng)將微信支付二維碼保存到相冊(cè),切換到微信,然后點(diǎn)擊微信右上角掃一掃功能,選擇支付二維碼完成支付。

【本文對(duì)您有幫助就好】

您的支持是博主寫作最大的動(dòng)力,如果您喜歡我的文章,感覺我的文章對(duì)您有幫助,請(qǐng)用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長會(huì)非常 感謝您的哦?。?!

發(fā)表我的評(píng)論
最新評(píng)論 總共0條評(píng)論
主站蜘蛛池模板: 正阳县| 通榆县| 哈巴河县| 房山区| 内黄县| 祥云县| 景德镇市| 文成县| 遵化市| 临漳县| 孝昌县| 兴国县| 嘉兴市| 那坡县| 峨边| 桂东县| 鹰潭市| 霸州市| 张家港市| 博白县| 台北县| 海盐县| 邵阳市| 綦江县| 同德县| 霍林郭勒市| 武安市| 武邑县| 玛曲县| 凉城县| 延吉市| 沁水县| 徐水县| 临西县| 乌兰县| 周至县| 宜春市| SHOW| 修武县| 商河县| 富顺县|