"Drawing basic Process"
(1) Import package and rename (for ease of use): Networkx,matplotlib.pyplot
(2) Establish network
(3) Drawing network: Nx.draw () #该方法可以讲网络进行美化, see Draw () method
(4) Set up the layout: Pos=nx.spring_layout (BG) #该方法为可选, you can create different layouts for the layout of the map to beautify
(5) Network Diagram display, commonly used in two ways: A Save as a picture file, B in the window display, can be saved as a picture form
Import Import Networkx as NX #导入networkx包
Import Matplotlib.pyplot as Plt #导入绘图包matplotlib (requires installation, method see first note)
G =nx.random_graphs.barabasi_albert_graph (100,1) #生成一个BA无标度网络G
Nx.draw (G) #绘制网络G
Plt.savefig ("Ba.png") #输出方式1: Save image as a picture file in PNG format
Plt.show () #输出方式2: Display this image in the window
"The Beautification of Network map"
Network map of the simple beautification work, mainly through two ways:
(1) The use of specific Nx.draw () method to beautify, concrete parameter usage:
-' node_size ': Specifies the size of the node (default is 300, the unit is unknown, that is, the large point in the picture above)
-' Node_color ': Specifies the color of the node (the default is red, you can use a simple string to identify the color, such as ' R ' is red, ' B ' is green, etc., you can view the manual, you must value the dictionary (. VALUES ()) When you assign a value to a "data dictionary", and then assign a value
-' node_shape ': The Shape of the node (the default is circular, with the string ' o ' logo, specific to see the manual)
-' alpha ': Transparency (default is 1.0, opaque, 0 is fully transparent)
-' width ': the width of the edge (default is 1.0)
-' edge_color ': The Color of the edge (default is black)
-' style ': The style of the edge (default is implementation, Optional: Solid|dashed|dotted,dashdot)
-' With_labels ': node with label (default is True)
-' Font_size ': node label font size (defaults to 12)
-' Font_color ': node label font color (default is black)
(2) through the use of different layouts (layout) to beautify, the current networkx mainly provided in the layout of the way are:
Circular_layout: The nodes are evenly distributed on a ring
Random_layout: node Random distribution
Shell_layout: Nodes are distributed on concentric circles
Spring_layout: Arrange nodes with Fruchterman-reingold algorithm (this algorithm I do not understand, looks like a multicenter radial shape)
Spectral_layout: Arranging nodes according to the Laplace eigenvector of graphs
code example: Pos=nx.spring_layout (BG)
"Statistical indicators"
This section mainly refers to the Shang Teacher's blog (http://blog.sciencenet.cn/blog-404069-337511.html), hereby acknowledge
(1) degree and degree distribution
A gets the degree distribution sequence of the nodes
Import Networkx as NX
G = Nx.random_graphs.barabasi_albert_graph (1000,3) #生成一个n =1000,m=3 BA scale-free network
Print G.degree (0) #返回某个节点的度
Print G.degree () #返回所有节点的度
Print Nx.degree_histogram (G) #返回图中所有节点的度分布序列 (frequency of occurrence from 1 to maximum)
B Draw System sequence diagram
Import Matplotlib.pyplot as Plt #导入科学绘图的matplotlib包
degree = Nx.degree_histogram (G) #返回图中所有节点的度分布序列
x = Range (len (degree)) #生成x轴序列, from 1 to most magnanimous
y = [Z/float (sum (degree)) for z in degree] #将频次转换为频率, this is a trick for Python: list content
Plt.loglog (x,y,color= "Blue", linewidth=2) #在双对数坐标轴上绘制度分布曲线
Plt.show () #显示图表
Note: for me that Python is unfamiliar with, use the following statement instead of "y = [z/float (sum (degree)) for Z-degree]",
Y1=float (sum (degree))
For z in degree:
Y=z/y1
Print Y
Although the printed Y is the same as the one written in the previous statement, the following error occurred while executing plt.loglog (x,y,color= "Blue", linewidth=2):
Valueerror:x and y must have same-a-dimension
The reason is: y = [z/float (sum (degree)) for z in degree] #得到的y为数组 [,,, ...]
For z in degree:
Y=z/y1# y is only one value
(2) Clustering coefficient
Nx.average_clustering (G) #平均群聚系数
Nx.clustering (G) #各个节点的群聚系数
(3) diameter and average distance
Nx.diameter (G) #返回图G的直径 (length of longest shortest path)
Nx.average_shortest_path_length (G) #返回图G所有节点间平均最短路径长度
(4) Match sex
Nx.degree_assortativity (G) #计算一个图的度匹配性
(5) Central (reference Shang teacher blog)
Degree centrality measures. (Point degree Center)
Degree_centrality (G) Compute the degree centrality for nodes.
In_degree_centrality (G) Compute the In-degree centrality for nodes.
Out_degree_centrality (G) Compute the Out-degree centrality for nodes.
Closeness centrality measures. (Close center)
Closeness_centrality (g[, V, Weighted_edges]) Compute closeness centrality for nodes.
Betweenness centrality measures. (center of the medium)
Betweenness_centrality (g[, normalized, ...)) Compute betweenness centrality for nodes.
Edge_betweenness_centrality (g[, normalized, ...)) Compute betweenness centrality for edges.
Current-flow closeness centrality measures. (Flow Close Center)
Current_flow_closeness_centrality (g[, ...]) Compute Current-flow closeness centrality for nodes.
Current-flow betweenness
Current-flow betweenness centrality measures. (Flow Medium Center)
Current_flow_betweenness_centrality (g[, ...]) Compute Current-flow betweenness centrality for nodes.
Edge_current_flow_betweenness_centrality (G) Compute current-flow betweenness centrality for edges.
eigenvector centrality. (Characteristic vector Center)
Eigenvector_centrality (g[, Max_iter, tol, ...]) Compute the eigenvector centrality for the graph G.
Eigenvector_centrality_numpy (g) Compute the eigenvector centrality for the graph G.
Load centrality.
Load_centrality (g[, V, cutoff, normalized, ...]) Compute load centrality for nodes.
Edge_load (g[, nodes, cutoff]) Compute edge load.
Explanation: Because my recent research mainly uses the scale-free chart and the two-point chart, so the following only for the two networks to introduce, if you need other types of online learning please refer to: Shang Teacher's blog (http://blog.sciencenet.cn/blog-404069-337689.html)
"No scale chart"
Import Networkx as NX
Import Matplotlib.pyplot as Plt
Ba= nx.random_graphs.barabasi_albert_graph (20,1) #生成n = 20, m=1 BA scale-free network
pos = nx.spring_layout (BA) #定义一个布局, where spring layout is used
Nx.draw (ba,pos,with_labels=false,node_size =) #绘制图形
Plt.show ()
"Two-point chart"
Import Networkx as NX
Import Matplotlib.pyplot as Plt
1. Generate a second-division chart
(1) generated by dots and edges
>>> Bg=nx. Graph ()
>>> Bg.add_edge (101,201)
>>> Bg.add_edge (101,202)
>>> Bg.add_edge (101,203)
>>> Bg.add_edge (102,202)
>>> Bg.add_edge (102,203)
>>> Bg.add_edge (102,204)
>>> Bg.add_edge (103,203)
>>> Bg.add_edge (103,205)
>>> Bg.add_edge (104,204)
>>> Bg.add_edge (104,206)
(2) Random generation
Networkx also provides a way to establish a variety of binary graph evolution models, which are listed here for your reference:
--Networkx.generators.classic.complete_bipartite_graph (N1, N2, Create_using=none)
Create a complete two-point diagram
--Networkx.generators.bipartite.bipartite_configuration_model (Aseq, Bseq, Create_using=none, Seed=none)
Establish a two-point chart based on the two-degree sequence
--Networkx.generators.bipartite.bipartite_random_regular_graph (d, N, Create_using=none, Seed=none)
Set up a random rule-two-chart
--Networkx.generators.bipartite.bipartite_preferential_attachment_graph (Aseq, p, Create_using=none, Seed=none)
Set up a two-point graph of priority connections
--Networkx.generators.bipartite.bipartite_havel_hakimi_graph (Aseq, Bseq, Create_using=none)
Create a Havel-hakimi pattern based on the two-degree sequence (the following two models are similar, I have not contacted this model, do not understand the specific meaning)
--Networkx.generators.bipartite.bipartite_reverse_havel_hakimi_graph (Aseq, Bseq, Create_using=none)
--Networkx.generators.bipartite.bipartite_alternating_havel_hakimi_graph (Aseq, Bseq, Create_using=none)
2. Draw the second-division chart
(1) Coloring the nodes
Cd=nx.algorithms.bipartite.basic.color (BG) #得到节点的颜色字典: {101:1, 102:1, 201:0, 202:0, 203:0, 204:0}, project node assigned color value 1, participant node of Yan The color value is 0
Nx.draw (Bg,pos,with_labels=false,node_size=50,node_color=cd.values ()) #得到
Note: Because the CD is a "key value pair", it must be evaluated before it can be assigned to Node_color
(2) Drawing drawings
Pos=nx.spring_layout (BG)
Plt.show ()
3. Two plot projection (split graph into single plot)
The Networkx.project (B, nodes) method provided by Networkx can complete this work. It accepts two parameters: one is the binary graph B, the other is the node set nodes.
For the extraction of node sets, the Networkx.bipartite.basic.sets method can be used to extract two kinds of nodes of a binary graph into two sets (X,y), where X is the project node and Y is the participant node.
>>> nset=nx.bipartite.basic.sets (BG) #nSet [0]: project node set; Nset[1]: Contributor node set
>>> Actor=nx.project (bg,nset[1]) #向参与者节点投影
4. Faction extraction (split-graph into a binary chart)
The Networkx.make_clique_bipartite method provided by Networkx can look up factions from the diagram and then construct a binary graph by connecting a faction as a project node and establishing a connection to the nodes in that faction.
>>> Bg1=nx.make_clique_bipartite (actor) #接3中代码
>>> print bg1.edges () #结果为: [(201,-1), (202,-2), (202,-1), (203,-2), (203,-1), (203,-3), (204,-4), (204,-2) , (205,-3), (206,-4)]