加入收藏 | 设为首页 | 会员中心 | 我要投稿 安卓应用网 (https://www.0791zz.com/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Python > 正文

networkx笔记:绘制基本网络图

发布时间:2020-05-29 01:19:30 所属栏目:Python 来源:互联网
导读:h1id=articleHeader1style=font-family:'OpenSans','HelveticaNeue',Helvetica,Arial,STHeiti,'MicrosoftYahei',sans-serif;line-height:1.2;color:rgb(51,51,51);font-size:1.57143

<h1 id="articleHeader1" style="font-family:'Open Sans','Helvetica Neue',Helvetica,Arial,STHeiti,'Microsoft Yahei',sans-serif;line-height:1.2;color:rgb(51,51,51);font-size:1.57143em;">
绘制基本网络图
<p style="color:rgb(51,51);font-family:'Open Sans',sans-serif;font-size:14px;line-height:22.3999996185303px;">
用matplotlib绘制网络图

基本流程:

  1. 导入networkx,matplotlib包
  2. 建立网络
  3. 绘制网络 nx.draw()
  4. 建立布局 pos = nx.spring_layout美化作用

    最基本画图程序


      (G,[pos,ax,hold])
    1. (G,[pos,with_labels])
    2. (G,pos,[nodelist]) 绘制网络G的节点图
    3. (G,pos[edgelist]) 绘制网络G的边图
    4. (G,pos[,...]) 绘制网络G的边图,边有label
      ---有layout 布局画图函数的分界线---
    5. Draw the graph G with a circular layout.
    6. Draw the graph G with a random layout.
    7. Draw the graph G with a spectral layout.
    8. Draw the graph G with a spring layout.
    9. Draw networkx graph with shell layout.
    10. Draw networkx graph with graphviz layout.

    -: 指定节点的尺寸大小(默认是300,单位未知,就是上图中那么大的点)
    -: 指定节点的颜色 (默认是红色,可以用字符串简单标识颜色,例如'r'为红色,'b'为绿色等,具体可查看手册),用“数据字典”赋值的时候必须对字典取值(.values())后再赋值
    -: 节点的形状(默认是圆形,用字符串'o'标识,具体可查看手册)
    -: 透明度 (默认是1.0,不透明,0为完全透明)
    -: 边的宽度 (默认为1.0)
    -: 边的颜色(默认为黑色)
    -: 边的样式(默认为实现,可选: solid|dashed|dotted,dashdot)
    -: 节点是否带标签(默认为True)
    -: 节点标签字体大小 (默认为12)
    -: 节点标签字体颜色(默认为黑色)
    e.g. nx.draw(G,node_size = 30,with_label = False)
    绘制节点的尺寸为30,不带标签的网络图。


    
    

  • circular_layout:节点在一个圆环上均匀分布
  • random_layout:节点随机分布
  • shell_layout:节点在同心圆上分布
  • spring_layout: 用Fruchterman-Reingold算法排列节点(这个算法我不了解,样子类似多中心放射状)
  • spectral_layout:根据图的拉普拉斯特征向量排列节

    布局也可用pos参数指定,例如,nx.draw(G,pos = spring_layout(G)) 这样指定了networkx上以中心放射状分布.


    <h1 id="articleHeader2" style="font-family:'Open Sans',51);font-size:1.57143em;">
    绘制划分后的社区
    <p style="color:rgb(51,sans-serif;font-size:14px;line-height:22.3999996185303px;">
    先看一段代码,代码源自<a href="http://perso.crans.org/aynaud/communities/" rel="nofollow" style="color:rgb(0,142,89);text-decoration:none;background:0px 0px;">site

    
    

<span class="hljs-keyword" style="color:rgb(133,0);">for com <span class="hljs-keyword" style="color:rgb(133,0);">in <span class="hljs-keyword" style="color:rgb(133,0);">values()) :
<span class="hljs-keyword" style="color:rgb(133,0);">count = <span class="hljs-keyword" style="color:rgb(133,0);">count + <span class="hljs-number" style="color:rgb(42,152);">1.
list_nodes = [nodes <span class="hljs-keyword" style="color:rgb(133,0);">for nodes <span class="hljs-keyword" style="color:rgb(133,0);">partition.keys()
<span class="hljs-keyword" style="color:rgb(133,0);">if <span class="hljs-keyword" style="color:rgb(133,0);">partition[nodes] == com]
nx.draw_networkx_nodes(G,list_nodes,node_size = <span class="hljs-number" style="color:rgb(42,152);">50,node_color = <span class="hljs-keyword" style="color:rgb(133,0);">str(<span class="hljs-keyword" style="color:rgb(133,0);">count / <span class="hljs-keyword" style="color:rgb(133,0);">size))

nx.draw_networkx_edges(<span class="hljs-keyword" style="color:rgb(133,0);">User,with_labels = <span class="hljs-literal">True,alpha=<span class="hljs-number" style="color:rgb(42,152);">0.5 )
plt.<span class="hljs-keyword" style="color:rgb(133,0);">show()

<p style="color:rgb(51,sans-serif;font-size:14px;line-height:22.3999996185303px;">
communit.best_partition 是社区划分方法,算法是根据Vincent D.Blondel 等人于2008提出,是基于modularity optimization的heuristic方法.

partition的结果存在字典数据类型:
<code style="font-size:.92857em;font-family:Consolas,246);">{'1': 0,'3': 1,'2': 0,'5': 1,'4': 0,'6': 0}


单引号里的数据是key,也就是网络中节点编号。

冒号后面的数值,表示网络中节点的编号属于哪个社区。也就是社区标号。如<code style="font-size:.92857em;font-family:Consolas,246);">'6': 0
表示6节点属于0社区


<pre class="hljs coffeescript" style="overflow:auto;font-size:.92857em;font-family:Consolas,monospace;color:inherit;background-color:transparent;"> list_nodes = [nodes <span class="hljs-keyword" style="color:rgb(133,0);">in partition.keys()
<span class="hljs-keyword" style="color:rgb(133,0);">if partition[nodes] == com]

<p style="color:rgb(51,sans-serif;font-size:14px;line-height:22.3999996185303px;">
每次循环list_nodes结果是社区i对应的用户编号。

如第一次循环结果是com = 0,list_nodes= ['1','2','4','6']

第二次循环的结果是com = 1,list_nodes = ['3','6']

这样每次循环,画出一个社区的所有节点:


<pre class="hljs perl" style="overflow:auto;font-size:.92857em;font-family:Consolas,monospace;color:inherit;background-color:transparent;"> nx.draw_networkx_nodes(G,<span class="hljs-keyword" style="color:rgb(133,0);">pos,node_color = str(count / size))

<p style="color:rgb(51,sans-serif;font-size:14px;line-height:22.3999996185303px;">
循环结束后通过颜色来标识不同社区


<p style="color:rgb(51,sans-serif;font-size:14px;">
http://segmentfault.com/a/1190000000527216

(编辑:安卓应用网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读