Repository navigation
Expand file tree
/
Copy pathImpact_Graphs.py
More file actions
300 lines (251 loc) · 8.86 KB
/
Copy pathImpact_Graphs.py
File metadata and controls
300 lines (251 loc) · 8.86 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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
"""
create graphs for output, rather than relying on Google Charts
"""
import matplotlib.pyplot as plt
import mpltern # this needs to be imported even though it is not directly referenced in the code
import numpy
from Impact_Common import encode_name
FIGSIZE = [6, 4] # default figure size
WIDESIZE = [8, 4] # for multiline with legends
class LineData:
def __init__(self):
self.x = []
self.y = []
self.style = "-"
self.label = ""
class GeneralData:
def __init__(self):
self.x_label = ""
self.y_label = ""
self.inc_legend = False
self.x_max = None
self.y_max = None
self.equal_axes = True
class AnnotationData:
def __init__(self):
self.label = ""
self.pos = None
self.textpos = None
class PointData:
def __init__(self):
self.x = []
self.y = []
self.label = ""
self.edgecolor = "black"
self.fillcolor = "black"
self.marker = "o"
class StackData:
def __init__(self):
self.x = []
self.y = {}
self.labels = []
# ---- history plotting functions ----
def save_figure(plt, name):
plt.savefig(f'webout/impact_figs/img_{encode_name(name)}.svg', format="svg", dpi=600)
def draw_graph_line_chart(name: str, metric_list: list):
base_metric = metric_list[0].metrics[name]
# get data for line plot
x = []
y = []
for metric_set in metric_list:
if metric_set.metrics[name].value == "n/a":
v = numpy.nan
else:
v = metric_set.metrics[name].value
x.append(metric_set.year())
y.append(v)
# create plot
fig, faxes = plt.subplots(figsize=FIGSIZE)
faxes.plot(x, y)
faxes.spines["right"].set_visible(False)
faxes.spines["top"].set_visible(False)
faxes.set_xlabel("Year")
faxes.set_ylabel(base_metric.axis_name)
plt.tight_layout()
save_figure(plt, name)
plt.close("all")
def draw_graph_multiline_left_chart(name: str, metric_list: list):
base_metric = metric_list[0].metrics[name]
# figure out how many values will be on the x-axis
maxx = 0
for metric_set in metric_list:
maxx = max(maxx, len(metric_set.metrics[name].value))
fig, faxes = plt.subplots(figsize=WIDESIZE, layout="constrained")
# get data
for metric_set in metric_list:
y = metric_set.metrics[name].value
x = [i+1 for i in range(len(y))]
faxes.plot(x, y, label=f"{metric_set.year()}")
faxes.spines["right"].set_visible(False)
faxes.spines["top"].set_visible(False)
faxes.set_ylabel(base_metric.axis_name)
fig.legend(loc="outside right center", prop={"size": "x-small"}, ncols=2)
save_figure(plt, name)
plt.close("all")
def draw_graph_multiline_center_chart(name: str, metric_list: list):
base_metric = metric_list[0].metrics[name]
# figure out how many values will be on the x-axis
maxx = 0
for metric_set in metric_list:
maxx = max(maxx, len(metric_set.metrics[name].value))
fig, faxes = plt.subplots(figsize=WIDESIZE, layout="constrained")
# get data
for metric_set in metric_list:
y = metric_set.metrics[name].value
n = len(y)
shift = (n - 1) // 2
x = [i - shift for i in range(n)]
faxes.plot(x, y, label=f"{metric_set.year()}")
faxes.spines["right"].set_visible(False)
faxes.spines["top"].set_visible(False)
faxes.set_ylabel(base_metric.axis_name)
fig.legend(loc="outside right center", prop={"size": "x-small"}, ncols=2)
save_figure(plt, name)
plt.close("all")
def draw_graph_two_line_chart(name: str, metric_list: list):
base_metric = metric_list[0].metrics[name]
# get data for line plot
x = []
y1 = []
y2 = []
for metric_set in metric_list:
v = metric_set.metrics[name].value
x.append(metric_set.year())
y1.append(v[0])
y2.append(v[1])
# create plot
fig, faxes = plt.subplots(figsize=FIGSIZE)
faxes.plot(x, y1, label=r"$rec_I$")
faxes.plot(x, y2, label=r"$rec_P$")
faxes.spines["right"].set_visible(False)
faxes.spines["top"].set_visible(False)
faxes.set_xlabel("Year")
faxes.set_ylabel(base_metric.axis_name)
faxes.legend()
plt.tight_layout()
save_figure(plt, name)
plt.close("all")
def draw_graph_line_combined_chart(name: str, metric_list: list):
base_metric = metric_list[0].metrics[name]
# get data for line plot
x = []
y = []
for metric_set in metric_list:
v = metric_set.metrics[name].value
x.append(metric_set.year())
y.append(v[0] + v[1]/10)
# create plot
fig, faxes = plt.subplots(figsize=FIGSIZE)
faxes.plot(x, y)
faxes.spines["right"].set_visible(False)
faxes.spines["top"].set_visible(False)
faxes.set_xlabel("Year")
faxes.set_ylabel(base_metric.axis_name)
plt.tight_layout()
save_figure(plt, name)
plt.close("all")
def draw_graph_ternary_chart(name: str, metric_list: list):
fig = plt.figure(figsize=FIGSIZE)
faxes = fig.add_subplot(projection="ternary")
faxes.grid()
faxes.set_axisbelow(True)
# get data
a, c, m, y = [], [], [], []
for metric_set in metric_list:
d = metric_set.metrics[name].value
a.append(d[0])
c.append(d[1])
m.append(d[2])
y.append(metric_set.year())
sc = faxes.scatter(a, c, m, c=y)
# the following functions are not errors despite PyCharm not recognizing them
# they are unique to the ternary plot
faxes.set_tlabel("Author")
faxes.set_llabel("Collaborator")
faxes.set_rlabel("Manager")
cax = faxes.inset_axes((1.05, 0.1, 0.05, 0.9), transform=faxes.transAxes)
fig.colorbar(sc, cax=cax)
plt.tight_layout()
save_figure(plt, name)
plt.close("all")
def draw_graph_3d_scatter_chart(name: str, metric_list: list):
fig = plt.figure(figsize=FIGSIZE)
faxes = fig.add_subplot(projection="3d")
# get data
years, x, y, z = [], [], [], []
for metric_set in metric_list:
d = metric_set.metrics[name].value
x.append(d[0])
y.append(d[1])
z.append(d[2])
years.append(metric_set.year())
sc = faxes.scatter(x, y, z, c=years)
faxes.set_xlabel("Total Publications")
faxes.set_ylabel("Total Citations")
faxes.set_zlabel(r"$\rho_R$")
fig.colorbar(sc, location="left")
plt.tight_layout()
save_figure(plt, name)
plt.close("all")
def draw_graph_five_line_chart(name: str, metric_list: list):
base_metric = metric_list[0].metrics[name]
# get data for line plot
x = []
y1, y2, y3, y4, y5 = [], [], [], [], []
for metric_set in metric_list:
v = metric_set.metrics[name].value
x.append(metric_set.year())
y1.append(v[0])
y2.append(v[1])
y3.append(v[2])
y4.append(v[3])
y5.append(v[4])
# create plot
fig, faxes = plt.subplots(figsize=FIGSIZE)
faxes.plot(x, y1, label=r"$h$")
faxes.plot(x, y2, label=r"$h_1$")
faxes.plot(x, y3, label=r"$h_2$")
faxes.plot(x, y4, label=r"$h_3$")
faxes.plot(x, y5, label=r"$h_\text{last}$")
faxes.spines["right"].set_visible(False)
faxes.spines["top"].set_visible(False)
faxes.set_xlabel("Year")
faxes.set_ylabel(base_metric.axis_name)
faxes.legend()
plt.tight_layout()
save_figure(plt, name)
plt.close("all")
# ---- example drawing function ----
def draw_example_figure(graph, metric_base_data):
fig, faxes = plt.subplots(figsize=FIGSIZE)
graph_data = graph.data(metric_base_data)
for data in graph_data:
if isinstance(data, LineData):
faxes.plot(data.x, data.y, linestyle=data.style, label=data.label)
elif isinstance(data, PointData):
faxes.scatter(data.x, data.y, label=data.label, edgecolors=data.edgecolor, c=data.fillcolor)
elif isinstance(data, AnnotationData):
faxes.annotate(data.label, xy=data.pos, xytext=data.textpos, va="center",
arrowprops=dict(facecolor="black", shrink=0.1))
elif isinstance(data, StackData):
bottom = [0 for _ in range(len(data.x))]
for d in data.labels:
dat = data.y[d]
faxes.bar(data.x, dat, bottom=bottom, label=d)
bottom = [bottom[i] + dat[i] for i in range(len(bottom))]
elif isinstance(data, GeneralData):
faxes.set_xlabel(data.x_label)
faxes.set_ylabel(data.y_label)
if data.inc_legend:
faxes.legend()
if data.x_max is not None:
faxes.set_xlim(0, data.x_max)
if data.y_max is not None:
faxes.set_ylim(0, data.y_max)
if data.equal_axes:
faxes.set_aspect("equal", adjustable="box")
faxes.spines["right"].set_visible(False)
faxes.spines["top"].set_visible(False)
plt.tight_layout()
save_figure(plt, graph.name)
plt.close("all")