forked from zhuqiwen/cewit_visualization
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest1.py
More file actions
121 lines (76 loc) · 3.09 KB
/
test1.py
File metadata and controls
121 lines (76 loc) · 3.09 KB
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
import networkx as nx
import matplotlib.pyplot as plt
import csv
import pprint as pp
from math import log, e
import matplotlib as mpl
# function to read from finalized_cewit_faculty.csv and make a dictionary from it.
def get_dep_fac_dict():
with open('finalized_cewit_faculty.csv') as f:
csv_content = csv.DictReader(f)
dep_fac_dict = {}
names = []
iu_dep_list = []
for row in csv_content:
fn = row['Name'].split(',')[1].strip().lower()
ln = row['Name'].split(',')[0].strip().lower()
dep_fac_dict.setdefault(row['Department'].title(),[]).append((fn,ln))
return dep_fac_dict
def get_gradien_color():
with open('gradient_colors') as f:
color_dict ={}
colors = []
for c in f.readlines():
colors.append('#'+c.strip())
for i in range(5,35):
color_dict[i] = colors[i-5].strip()
return color_dict, colors
dep_fac_dict = get_dep_fac_dict()
color_dict, C = get_gradien_color()
print C
# pp.pprint(color_dict)
# because there are too many departments/nodes in the graph, so we only keep the top 25 departments
for d in dep_fac_dict.keys():
if len(dep_fac_dict[d]) < 5:
dep_fac_dict.pop(d)
G = nx.Graph()
edge_width_list = []
for d in dep_fac_dict.keys():
# if len(dep_fac_dict[d]) <= 4:
# width = 4
# else:
# width = len(dep_fac_dict[d])
width = len(dep_fac_dict[d])
edge_tuple = ('CEWIT', d)
edge_width_list.append((edge_tuple,width))
for tuple, width in edge_width_list:
G.add_edge(tuple[0], tuple[1], weight = width/5) # the weight determines the width of links between departments and CEWIT
node_labels = {}
node_sizes = {}
node_colors = {}
for d in dep_fac_dict.keys():
node_labels[d] = d + '\n' + str(len(dep_fac_dict[d]))
# node_sizes[d] = log(len(dep_fac_dict[d]),1.5) * 200 # use log() to decrease the difference between huge node and small node
node_sizes[d] = len(dep_fac_dict[d]) * 150
# node_colors[d] = '#E1D8B7'
node_colors[d] = color_dict[len(dep_fac_dict[d])]
# color_index.append(len(dep_fac_dict[d]))
node_labels['CEWIT'] = 'CEWIT'
node_sizes['CEWIT'] = 1500
node_colors['CEWIT'] = '#E1D8B7'
# print node_sizes.keys()
# print node_sizes.values()
# print node_labels.keys()
# print node_labels.values()
edges = G.edges()
weights = [G[u][v]['weight'] for u,v in edges]
fig = plt.figure('Distribution of CEWIT Faculty Members',figsize=(8,8))
cm = mpl.colors.ListedColormap(C)
pos = nx.spring_layout(G, k=0.9, scale = 2)
nx.draw(G, pos, linewidths = 0.5,labels = node_labels, font_size = 12, edges = edges, width = weights, nodelist = node_sizes.keys(), node_size = node_sizes.values(), font_family = 'Century Gothic', node_color=node_colors.values(), edge_color='#44697D',with_labels=True)
sm = plt.cm.ScalarMappable(cmap=cm, norm=plt.normalize(vmin=5, vmax=35))
sm._A = []
plt.colorbar(sm, shrink = 0.7, pad = 0.005)
figtext = 'Distribution of CEWIT Faculty Members'
fig.text(.43,.1,figtext, weight = 'heavy', size = 20, ha = 'center', family = 'Century Gothic')
plt.show() # display