forked from socialnetworktool/SocialNetworkSimulator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
GenerateDegreeHisogram.py
50 lines (44 loc) · 1.29 KB
/
GenerateDegreeHisogram.py
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
import snap
import numpy
import matplotlib.pyplot as plt
mainpath = "C:\\Users\\GISer\\Desktop\\"
filename = "1-1.txt"
f = open(mainpath + filename, "r")
graph = snap.TUNGraph.New()
endofFile = False
line = f.readline()
strs = line.split("#")
numNodes = int(strs[1])
numEdges = int(strs[3])
index = 0
graphLabelToId = {}
for i in range(numNodes):
line = f.readline().rstrip("\n")
graph.AddNode(index)
graphLabelToId[line] = index
index += 1
for j in range(numEdges):
line = f.readline().rstrip("\n")
strs = line.split("#")
graph.AddEdge(graphLabelToId[strs[0]], graphLabelToId[strs[1]])
snap.PlotInDegDistr(graph, "wikiInDeg", "wiki-vote In Degree")
DegToCntV = snap.TIntPrV()
snap.GetDegCnt(graph, DegToCntV)
x = []
y = []
for item in DegToCntV:
x.append(item.GetVal1())
y.append(item.GetVal2())
print "%d nodes with degree %d" % (item.GetVal2(), item.GetVal1())
width = 0.35
rects = plt.bar(x, y, width)
xticks = numpy.array(x)
plt.xlabel('Degree')
plt.ylabel('The number of nodes')
plt.xticks(xticks + width/2.0, x)
for rect in rects:
height = rect.get_height()
plt.text(rect.get_x() + rect.get_width() / 2., height + 2,
'%d' % int(height),
ha='center', va='bottom')
plt.show()