【Python】Matplotlib画图(十)——基于networkx画关系网络图

╰+攻爆jí腚メ 2022-06-07 05:41 530阅读 0赞

前言

昨天才开始接触,鼓捣了一个下午,接下来会持续更新,如果哪里有错误的地方,望各位大佬指出,谢谢!

数据描述

两个文件,一个文件包含了网络图的节点,节点存在类别(0,1,2,3)四类,但是0类别舍去,不画出;另一个文件包含了网络图的边,数据基本特征如下:

Center Center 1

图1中,id表示节点,b是类别;图2中,两个数字表示边连接的两个点。

Networkx

安装

我的系统是Mac OS,直接在terminal输入sudo pip install networkx就可以安装,由于代码中涉及几个函数,在python3中会报错,我用python2.7.13实现的

基本使用方法

  1. import networkx as nx #导入networkx包
  2. import matplotlib.pyplot as plt #导入绘图包matplotlib(需要安装,方法见第一篇笔记)
  3. G =nx.random_graphs.barabasi_albert_graph(100,1) #生成一个BA无标度网络G
  4. nx.draw(G) #绘制网络G
  5. plt.savefig("ba.png") #输出方式1: 将图像存为一个png格式的图片文件
  6. plt.show() #输出方式2: 在窗口中显示这幅图像

参数介绍

基本

- `node_size`: 指定节点的尺寸大小(默认是300,单位未知,就是上图中那么大的点)
- `node_color`: 指定节点的颜色 (默认是红色,可以用字符串简单标识颜色,例如’r’为红色,’b’为绿色等,具体可查看手册)
- `node_shape`: 节点的形状(默认是圆形,用字符串’o’标识,具体可查看手册)
- `alpha`: 透明度 (默认是1.0,不透明,0为完全透明)
- `width`: 边的宽度 (默认为1.0)
- `edge_color`: 边的颜色(默认为黑色)
- `style`: 边的样式(默认为实现,可选: solid|dashed|dotted,dashdot)
- `with_labels`: 节点是否带标签(默认为True)
- `font_size`: 节点标签字体大小 (默认为12)
- `font_color`: 节点标签字体颜色(默认为黑色)

布局

circular_layout:节点在一个圆环上均匀分布
random_layout:节点随机分布
shell_layout:节点在同心圆上分布
spring_layout: 用Fruchterman-Reingold算法排列节点
spectral_layout:根据图的拉普拉斯特征向量排列节点

代码

  1. # coding:utf-8
  2. import networkx as nx
  3. import matplotlib.pyplot as plt
  4. import csv
  5. with open('node-8.csv','rb') as csvfile:
  6. reader = csv.DictReader(csvfile)
  7. column = [row['b'] for row in reader]
  8. id_tag0 = [row['id'] for row in reader]
  9. #print column
  10. id_tag = []
  11. for item in id_tag0:
  12. id_tag.append(int(item))
  13. # =================Setting node parameters====================
  14. node_0 = []
  15. node_1 = []
  16. node_2 = []
  17. node_3 = []
  18. node_color = []
  19. node_color_y = []
  20. node_color_r = []
  21. node_color_g = []
  22. node_color_b = []
  23. node_shape = []
  24. node_shape_0 = []
  25. node_shape_1 = []
  26. node_shape_2 = []
  27. node_shape_3 = []
  28. for i in range(len(column)):
  29. if int(column[i]) == 0:
  30. pass
  31. elif int(column[i]) == 1:
  32. color = 'r'
  33. shape = 'o'
  34. node_1.append(i)
  35. node_color_r.append(color)
  36. node_shape_1.append(shape)
  37. elif int(column[i]) == 2:
  38. color = 'g'
  39. shape = 'o'
  40. node_2.append(i)
  41. node_color_g.append(color)
  42. node_shape_2.append(shape)
  43. else:
  44. color = 'b'
  45. shape = '*'
  46. node_3.append(i)
  47. node_color_b.append(color)
  48. node_shape_3.append(shape)
  49. node_color.append(color)
  50. node_shape.append(shape)
  51. # ==============================================================
  52. with open('node-8.csv','rb') as csvfile:
  53. reader = csv.DictReader(csvfile)
  54. column1 = [row['b'] for row in reader]
  55. id_tag1 = [row['id'] for row in reader]
  56. #print column
  57. id_tag11 = []
  58. for item in id_tag1:
  59. id_tag11.append(int(item))
  60. edge = []
  61. with open('edge-8.txt','r') as f:
  62. data = f.readlines()
  63. for line in data:
  64. #print line
  65. line = tuple(line.replace('\r','').replace('\n','').replace('\t','').split(','))
  66. edge.append(line)
  67. #print edge
  68. # ===============Setting edge parameters=========================
  69. edge_color = []
  70. edge_style = []
  71. for item in edge:
  72. #print item
  73. if int(column1[int(item[0])]) == 0 or int(column1[int(item[1])]) == 0:
  74. pass
  75. elif int(column1[int(item[0])]) == 1 or int(column1[int(item[1])]) == 1:
  76. color = 'r'
  77. #style0 = 'dashdot'
  78. #color_r_list.append(color)
  79. elif int(column1[int(item[0])]) == 2 or int(column1[int(item[1])]) == 2:
  80. color = 'g'
  81. #style0 = 'dashed'
  82. #color_r_list.append(color)
  83. else:
  84. color = 'b'
  85. #style0 = 'dotted'
  86. #color_b_list.append(color)
  87. edge_color.append(color)
  88. #edge_style.append(style0)
  89. G = nx.Graph()
  90. #G.add_nodes_from(id_tag)
  91. G.add_edges_from(edge)
  92. #nx.draw(G,pos=nx.random_layout(G), nodelist = node_0, node_color = node_color_y, node_size=120, node_shape=node_shape_0)
  93. #nx.draw(G,pos=nx.random_layout(G), nodelist = node_1, node_color = node_color_r, node_size=120, node_shape=node_shape_1)
  94. #nx.draw(G,pos=nx.random_layout(G), nodelist = node_2, node_color = node_color_g, node_size=120, node_shape=node_shape_2)
  95. #nx.draw(G,pos=nx.random_layout(G), nodelist = node_3, node_color = node_color_b, node_size=120, node_shape=node_shape_3)
  96. 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)
  97. #nx.draw_networkx(G,pos=nx.random_layout(G),nodelist = node_1,node_color=node_color,node_size=100,node_shape='o',style='dashdot')
  98. #nx.draw_networkx(G,pos=nx.random_layout(G),node_color=color_g_list,node_size=150,node_shape='^',style='dashed')
  99. #nx.draw_networkx(G,pos=nx.random_layout(G),node_color=color_b_list,node_size=150,node_shape='*',style='dotted')
  100. #plt.legend()
  101. #nx.draw_networkx(G)
  102. plt.show()

画图

Center 2

参考链接

http://blog.sciencenet.cn/blog-404069-337865.html

https://www.zhihu.com/question/59653147

https://segmentfault.com/a/1190000000527216

https://networkx.github.io/documentation/networkx-1.10/tutorial/tutorial.html#what-to-use-as-nodes-and-edges

发表评论

表情:
评论列表 (有 0 条评论,530人围观)

还没有评论,来说两句吧...

相关阅读

    相关 networkx弯曲的边

    networkx里面自绘的边都是直的,当多个节点处于同一条直线还有连边的时候就特别难看。如果能否让networkx画弯曲的边,那么这种情况就可以好办的多了。 思路为: 1.

    相关 HTML5中canvas画图圆形

    利用canvas中的arc可以绘制圆形图案。函数原型为:context.arc(x,y,半径,开始角度,结束角度,是否逆时针旋转);所以可以通过修改开始角度和结束角度来绘制弧线