python plot

画复杂网络的度分布双对数图像

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
import networkx as nx
import numpy as np
import collections,math

import matplotlib.pyplot as plt
import matplotlib

def (ax,G,title,alpha,bias):
degree_sequence=nx.degree(G)
size = len(degree_sequence) # total number of nodes
cnt = collections.Counter(degree_sequence.values())

# first, make dict_keys() type to list!!!
x = list(cnt.keys())
y = [n/size for n in list(cnt.values())] # list comprehension

# in subplot, should use ax.set_XXX, while plt.XXX is okay
ax.set_xscale('log')
ax.set_yscale('log')
ax.set_xlim((1e0,1e3)) # set the limit of x-axis
ax.set_ylim((1e-4,1e0))
ax.scatter(x,y,marker='x')

x.remove(0) # remove the first encountered 0
# ajust slope and bias
y = [math.pow(n,-alpha)*math.pow(10,bias) for n in x]
line, =ax.plot(x,y,linestyle='--',c='g')

ax.set_title(title)
ax.set_xlabel("Degree, k")
ax.set_ylabel("Probability, P(k)")
ax.legend([line], ['slope=-'+str(alpha)])


fG1 = nx.read_graphml("film1.graphml")
fG2 = nx.read_graphml("film2.graphml")

aG1 = nx.read_graphml("artist1.graphml")
aG2 = nx.read_graphml("artist2.graphml")

plt.close('all')

# Two subplots, the axes array is 1-d
f, axarr = plt.subplots(2, 2)

draw(axarr[0,0],fG1, "Simple Weighting Film Projection Network",2.2,1.1)
draw(axarr[0,1],fG2, "Hyperbolic Weighting Film Projection Network",2.2,1.1)
draw(axarr[1,0],aG1, "Simple Weighting Artist Projection Network",2.1,0.75)
draw(axarr[1,1],aG2, "Hyperbolic Weighting Artist Projection Network",2.1,0.75)

plt.show()