<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绘制网络图
基本流程:
- 导入networkx,matplotlib包
- 建立网络
- 绘制网络 nx.draw()
- 建立布局 pos = nx.spring_layout美化作用
最基本画图程序 networkx nx
(G,[pos,ax,hold])
(G,[pos,with_labels]) (G,pos,[nodelist]) 绘制网络G的节点图 (G,pos[edgelist]) 绘制网络G的边图 (G,pos[,...]) 绘制网络G的边图,边有label
---有layout 布局画图函数的分界线--- ?Draw the graph G with a circular layout. ?Draw the graph G with a random layout. ?Draw the graph G with a spectral layout. ?Draw the graph G with a spring layout. ?Draw networkx graph with shell layout. ?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,不带标签的网络图。
= nx.spring_layout
<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 (编辑:李大同)
【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!
|