(3-3)networkx之绘制复杂图

本文详述了使用NetworkX进行复杂网络分析与可视化的方法,包括调整绘图参数、统计网络特征、绘制ER随机网络及小世界网络,探讨了社团划分与疾病传播动力学模型,同时介绍了PageRank算法的应用。

networkx 画图参数:

  • node_size: 指定节点的尺寸大小(默认是300,单位未知
  • node_color: 指定节点的颜色 (默认是红色,可以用字符串简单标识颜色,例如’r’为红色,'b’为绿色等,具体可查看手册),用“数据字典”赋值的时候必须对字典取值(.values())后再赋值
  • node_shape: 节点的形状(默认是圆形,用字符串’o’标识,具体可查看手册)
  • alpha: 透明度 (默认是1.0,不透明,0为完全透明)
  • width: 边的宽度 (默认为1.0)
  • edge_color: 边的颜色(默认为黑色)
  • style: 边的样式(默认为实现,可选: solid|dashed|dotted,dashdot)(实线|虚线|点线,dashdot)
  • with_labels: 节点是否带标签(默认为True)
  • font_size: 节点标签字体大小 (默认为12)
  • font_color: 节点标签字体颜色(默认为黑色)
  • nodelist:只绘制指定的nodelistl里的节点(默认G.nodes())
  • edgelist:只绘制指定的edgelistl里的边(默认= G.edges())
  • arrows:bool,optional(default = True) 对于有向图,如果为真,则绘制箭头。
  • pos:dictionary 将节点作为键和位置作为值的字典。 位置应该是长度为2的序列。
  • ax:Matplotlib Axes对象,可选 在指定的Matplotlib轴中绘制图形。

networkx 提供的布局方式有:
layout=[
nx.layout.circular_layout:节点在一个圆环上均匀分布
nx.layout.spring_layout: 用Fruchterman-Reingold算法排列节点
nx.layout.random_layout:节点随机分布
nx.layout.shell_layout:节点在同心圆上分布
nx.layout.spectral_layout:根据图的拉普拉斯特征向量排列节
nx.layout.fruchterman_reingold_layout
nx.layout.kamada_kawai_layout
]
在这里插入图片描述
在这里插入图片描述

import networkx as nx             #导入networkx包
import matplotlib.pyplot as plt 
G = nx.random_graphs.barabasi_albert_graph(50,1)   #生成一个BA无标度网络G
pos=nx.layout.circular_layout(G)#节点在一个圆环上均匀分布
print(pos) #pos:dictionary 将节点作为键和位置作为值的字典。 位置应该是长度为2的序列。
nx.draw(G,pos=pos,node_size=20)

在这里插入图片描述
在这里插入图片描述

import networkx as nx
import matplotlib.pyplot as plt
g=nx.karate_club_graph()
nx.draw(g)
fig,ax=plt.subplots(figsize=(8,6))
pos=nx.layout.spring_layout(g) #布局
NodeId=list(g.nodes())
node_size=[g.degree(i)**1.2*90 for i in NodeId]
options={
   
   
    'node_size':node_size,
    'lone_color':'grey',
    'linewidths':0.1,
    'width':0.4,
    'style':'solid',
    'nodelist':NodeId,
    'node_color':node_size,
    'font_color':'w'    
    }
    #颜色和大小都是根据该节点的度的大小来的
nx.draw(g,pos=pos,ax=ax,with_labels=True,**options)

在这里插入图片描述




从现在开始,
以下内容因为比较复杂,所以每个正规代码前都有些琐碎的知识点作为铺垫

一、最短距离用坐标分布图表示出来

先来补充些基础知识:

1.if-else的简写

1.通常写法

b=4
if b>4:
    print('对了')
else:
    print('错了')
 
   
2.简写

'对了' if b>4 else '错了'

2.for in循环的简写

q=[(a,b) for a in range(10) for b in range(10) if a>0 and a%2==0 and b%2==0]

这条语句的阅读顺序是 for a in range(10) --> for b in range(10) -->if a>0 and a%2==0 and b%2==0 -->(a,b)
也就是说for inif else组合在一起时就是从前往后读
q=[(a,b) for a in range(10) for b in range(10) if a>0 and a%2==0 and b%2==0]

print(q)
# [(2, 0), (2, 2), (2, 4), (2, 6), (2, 8), (4, 0), (4, 2), (4, 4), (4, 6), (4, 8), (6, 0), (6, 2), (6, 4), (6, 6), (6, 8), (8, 0), (8, 2), (8, 4), (8, 6), (8, 8)]
q=[(a+b) for a in range(10) for b in range(10) if a>0 and a%2==0 and b%2==0]

print(q)
# [2, 4, 6, 8, 10, 4, 6, 8, 10, 12, 6, 8, 10, 12, 14, 8, 10, 12, 14, 16]

3.reshape(-1)

import numpy as np
c=np.array([[1,2,3],[4,5,6]])
print('两行三列')
print(c.reshape(2,3))
[[1 2 3]
 [4 5 6]]

print('三行两列')
print(c.reshape(3,2))
[[1 2]
 [3 4]
 [5 6]]

print('我也不知道是几行,反正就是1列')
print(c.reshape(-1,1))
[[1]
 [2]
 [3]
 [4]
 [5]
 [6]]

print('我也不知道是几列,反正就是1行')
print(c.reshape(1,-1))
[[1 2 3 4 5 6]]

print('不分行列,就是一串数字')
print(c.reshape(-1))
[1 2 3 4 5 6]
z = np.array([[1, 2, 3, 4],  [5, 6, 7,8],  [9, 10, 11, 12],  [13, 14, 15, 16]])
print(z.reshape(-1)) 
输出:[ 1 2  3  4  5  6  7  8  9 10 11 12 13 14 15 16] 

4.关于np.array()

import networkx as nx
import numpy as np
G=nx.graph_atlas(100)

aa=[[nx.shortest_path_length(G,i,j) for j in G if i != j]for i in G]
bb=np.array([[nx.shortest_path_length(G,i,j) for j in G 
评论 1
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

BlackTurn

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值