-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplotting.py
409 lines (343 loc) · 14.7 KB
/
plotting.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
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
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
# -*- coding: utf-8 -*-
"""
A collection of plotting functions used to generate figures
"""
import os
import numpy as np
import igraph as ig
from numpy.random import normal
from matplotlib import pyplot as plt
COLORS = (
'tomato', 'mediumseagreen', 'cornflowerblue', 'mediumorchid',
'sandybrown', 'gold', 'deepskyblue', 'orangered', 'goldenrod'
)
###########################################################
# #
# Plotting functions #
# #
###########################################################
def plot_traj(traj, suffix, output_dir='plots/', colors=COLORS):
os.makedirs(output_dir, exist_ok=True)
plt.figure(figsize=(4, 3))
for i, key in enumerate(sorted(traj.keys())):
plt.plot(traj[key], color=colors[i % len(colors)], label=str(key))
plt.ylim(0, 1)
plt.legend()
plt.xlabel('time steps [in hundreds]')
plt.ylabel('fraction of N')
plt.title('Trajectory during thermalization')
plt.tight_layout()
plt.savefig('{}/trajectory{}.png'.format(output_dir, suffix))
plt.close('all')
def plot_hist(distribution, name, suffix, bins_num=21, output_dir='plots/', colors=COLORS):
os.makedirs(output_dir, exist_ok=True)
for idx, key in enumerate(sorted(distribution.keys())):
col = colors[idx % len(colors)]
distr = distribution[key]
plt.figure(figsize=(4, 3))
plt.hist(distr, bins=np.linspace(0.0, 1.0, bins_num), range=(0, 1), density=True, color=col)
avg = np.mean(distr)
std = np.std(distr)
plt.axvline(avg, linestyle='-', color='black')
plt.axvline(avg - std, linestyle='--', color='black')
plt.axvline(avg + std, linestyle='--', color='black')
plt.title('Histogram of seats share, avg={}, std={}'.format(round(avg, 2), round(std, 2)))
plt.xlabel('fraction of seats')
plt.ylabel('probability')
plt.tight_layout()
plt.savefig(output_dir + name + '_' + str(key) + suffix + '.png')
plt.close('all')
def plot_indexes(indexes, name, suffix, output_dir='plots/'):
for index, values in indexes.items():
lim0 = np.min([0, np.min(values)])
lim1 = np.max([1, np.max(values)])
plt.figure(figsize=(4, 3))
plt.hist(values, bins=np.linspace(lim0, lim1, 21), range=(0, 1), density=True)
avg = np.mean(values)
std = np.std(values)
plt.axvline(avg, linestyle='-', color='black')
plt.axvline(avg - std, linestyle='--', color='black')
plt.axvline(avg + std, linestyle='--', color='black')
plt.title(f'Histogram of {index} \n avg={round(avg, 2)}, std={round(std, 2)}')
plt.xlabel(f'{index}')
plt.ylabel('probability')
plt.tight_layout()
plt.savefig(output_dir + name + '_' + index + suffix + '.png')
plt.close('all')
def plot_mean_std(x, y, std, quantity, election_system, suffix, xlab,
ylab='election result of a', ylim=(), save_file=True):
"""
Plots mean +/- std of given variable vs quantity (eg number of zealots)
:param x: array with considered quantitity (zealots / media influence)
:param y: given variable
:param std: standard deviation of
:param quantity: we calculate susceptibility of that quantity
:param election_system: name of election system
:param suffix: suffix with params values
:param xlab: x-axis label
:param ylab: y-axis label
:param ylim: y-axis limits as [ymin, ymax]
:param save_file: bool if save plot to file
"""
plt.figure(figsize=(4, 3))
plt.plot(x, y, color='cornflowerblue', linestyle='-')
plt.plot(x, y + std, color='tomato', linestyle='--')
plt.plot(x, y - std, color='tomato', linestyle='--')
if ylim:
plt.ylim(ylim)
plt.title(election_system)
plt.xlabel(xlab)
plt.ylabel(ylab)
plt.tight_layout()
if save_file:
s = suffix.format(valuetoinsert='')
plt.savefig(f'plots/{quantity}_susceptibility_{election_system}{s}.png')
else:
plt.show()
plt.close('all')
def plot_mean_std_two_systems(x, y1, std1, y1_name, y2, std2, y2_name, quantity, suffix, xlab,
ylab='election result of a', ylim=(), save_file=True):
"""
Plots mean +/- std of 2 variables vs quantity (eg number of zealots)
:param x: array with considered quantity (zealots / media influence)
:param y1: variable 1
:param std1: standard deviation of y1
:param y1_name: name of y1 in the legend
:param y2: variable 2
:param std2: standard deviation of y2
:param y2_name: name of y2 in the legend
:param quantity: we calculate susceptibility over that quantity
:param suffix: suffix with params values
:param xlab: x-axis label
:param ylab: y-axis label
:param ylim: y-axis limits as [ymin, ymax]
:param save_file: bool if save plot to file
"""
plt.figure(figsize=(4, 3))
plt.plot(x, y1, color='cornflowerblue', linestyle='-', label=y1_name)
plt.plot(x, y2, color='tomato', linestyle='-', label=y2_name)
plt.fill_between(x, y1 - std1, y1 + std1, color='cornflowerblue', linewidth=0, alpha=0.5)
plt.fill_between(x, y2 - std2, y2 + std2, color='tomato', linewidth=0, alpha=0.5)
if ylim:
plt.ylim(ylim)
plt.title(f'{quantity} susceptibility')
plt.xlabel(xlab)
plt.ylabel(ylab)
plt.legend()
plt.tight_layout()
if save_file:
s = suffix.format(valuetoinsert='')
plt.savefig(f'plots/{quantity}_sus_comparison{s}.png')
else:
plt.show()
plt.close('all')
def plot_mean_std_all(voting_systems, x, results, quantity, suffix, xlab, ylab='election result of a', ylim=(),
save_file=True):
"""
Plots mean +/- std of all electoral systems from results vs quantity (eg number of zealots)
:param voting_systems: iterable with names of electoral systems
:param x: array with considered quantity (zealots / media influence)
:param results: dict with results with keys 'mean_set' and 'std_set' (e.g. prepared in zealot_susceptibility.py)
:param quantity: we calculate susceptibility over that quantity
:param suffix: suffix with params values
:param xlab: x-axis label
:param ylab: y-axis label
:param ylim: y-axis limits as [ymin, ymax]
:param save_file: bool if save plot to file
"""
plt.figure(figsize=(4, 3))
for i, system in enumerate(voting_systems):
plt.plot(x, results[system]['mean_set'], color=COLORS[i % len(COLORS)], linestyle='-', label=system)
plt.fill_between(x, results[system]['mean_set'] - results[system]['std_set'],
results[system]['mean_set'] + results[system]['std_set'],
color=COLORS[i % len(COLORS)], linewidth=0, alpha=0.5)
if ylim:
plt.ylim(ylim)
plt.title(f'{quantity} susceptibility')
plt.xlabel(xlab)
plt.ylabel(ylab)
plt.legend()
plt.tight_layout()
if save_file:
s = suffix.format(valuetoinsert='')
plt.savefig(f'plots/{quantity}_sus_all{s}.png')
else:
plt.show()
plt.close('all')
def plot_std(x, std, quantity, election_system, suffix, xlab,
ylab='election result of 1', ylim=(), save_file=True):
"""
Plots std of given variable vs quantity (eg number of zealots)
:param x: array with considered quantitity (zealots / media influence)
:param std: standard deviation of
:param quantity: we calculate susceptibility of that quantity
:param election_system: name of election system
:param suffix: suffix with params values
:param xlab: x-axis label
:param ylab: y-axis label
:param ylim: y-axis limits as [ymin, ymax]
:param save_file: bool if save plot to file
"""
plt.figure(figsize=(4, 3))
plt.plot(x, std, color='tomato', linestyle='-')
if ylim:
plt.ylim(ylim)
plt.title(election_system)
plt.xlabel(xlab)
plt.ylabel(ylab)
plt.tight_layout()
if save_file:
s = suffix.format(valuetoinsert='')
plt.savefig(f'plots/{quantity}_std_{election_system}{s}.png')
else:
plt.show()
plt.close('all')
def plot_mean_diff(x, y, quantity, election_system, suffix, xlab,
ylab='derivative of susceptibility', ylim=(), save_file=True):
"""
Plots (right) derivative of mean of given variable vs quantity (eg number of zealots)
:param x: array with considered quantitity (zealots / media influence)
:param y: given variable
:param quantity: we calculate susceptibility of that quantity
:param election_system: name of election system
:param suffix: suffix with params values
:param xlab: x-axis label
:param ylab: y-axis label
:param ylim: y-axis limits as [ymin, ymax]
:param save_file: bool if save plot to file
"""
plt.figure(figsize=(4, 3))
plt.plot(x[:-1], (y[1:]-y[:-1])/np.diff(x), color='mediumseagreen', linestyle='-')
if ylim:
plt.ylim(ylim)
plt.title(election_system)
plt.xlabel(xlab)
plt.ylabel(ylab)
plt.tight_layout()
if save_file:
s = suffix.format(valuetoinsert='')
plt.savefig(f'plots/{quantity}_derivative_{election_system}{s}.png')
else:
plt.show()
plt.close('all')
def plot_mean_per(x, y, quantity, election_system, suffix, xlab,
ylab='relative result', ylim=(), save_file=True):
"""
Plots change of mean of given variable divided by quantity (eg divided by number of zealots)
:param x: array with considered quantitity (zealots / media influence)
:param y: given variable
:param quantity: we calculate susceptibility of that quantity
:param election_system: name of election system
:param suffix: suffix with params values
:param xlab: x-axis label
:param ylab: y-axis label
:param ylim: y-axis limits as [ymin, ymax]
:param save_file: bool if save plot to file
"""
plt.figure(figsize=(4, 3))
plt.plot(x[1:], (y[1:]-y[0])/x[1:], color='mediumseagreen', linestyle='-')
if ylim:
plt.ylim(ylim)
plt.title(election_system)
plt.xlabel(xlab)
plt.ylabel(ylab)
plt.tight_layout()
if save_file:
s = suffix.format(valuetoinsert='')
plt.savefig(f'plots/{quantity}_susceptibilityPer_{election_system}{s}.png')
else:
plt.show()
plt.close('all')
def plot_heatmap(heatmap, l_bins, quantity, election_system, suffix, xlab='number of zealots',
ylab='distribution of 1', save_file=True, colormap='jet'):
"""
Plots a heatmap of given variable vs given quantity (eg number of zealots)
:param heatmap: histogram of given variable
:param l_bins: number of bins in the distribution
:param quantity: we calculate susceptibility of that quantity
:param election_system: name of electoral system
:param suffix: suffix with params values
:param xlab: x-axis label
:param ylab: y-axis label
:param save_file: bool if save plot to file
:param colormap: change colormap
"""
transposed_heatmap = np.transpose(heatmap)
plt.figure(figsize=(3.5, 3.1))
plt.imshow(transposed_heatmap, origin='lower', aspect='auto', cmap=colormap)
cb = plt.colorbar()
cb.ax.tick_params(labelsize=9)
plt.title(election_system)
plt.yticks(np.linspace(0, l_bins, 5) - 0.5, np.linspace(0, 1, 5))
plt.xlabel(xlab)
plt.ylabel(ylab)
plt.tight_layout()
if save_file:
s = suffix.format(valuetoinsert='')
plt.savefig(f'plots/heatmap_{quantity}_susceptibility_{election_system}{s}.png')
else:
plt.show()
plt.close('all')
def plot_network(graph, config, mode=None, layout=None, save_as=None, node_size=10, std=0.00015, ig_layout=None):
"""
Plotting igraph.Graph() object in various ways, indicating with color districts or states of the dones.
:param graph: the network to plot, igraph.Graph() object
:param config: configuration of the simulation, config.Config() object
:param mode: either 'districts' to plot districts, or 'states' to plot states
:param layout: 'default', 'geo', or 'geo_strict', the mode of plotting the net, if None it'll be 'default'
:param save_as: destination file to save the plot, use .png or .pdf
:param node_size: the size of the node on the plot
:param std: this param defines the size of the area that nodes from one district will take on the plot,
if 0 all nodes from the same district will overlap on the plot
:param ig_layout: the layout as from ig.Graph.layout() to plot the nodes in a fixed position
:return: None
"""
if mode == 'districts':
for v in graph.vs():
v['color'] = COLORS[v['district'] % len(COLORS)]
elif mode == 'states':
for v in graph.vs():
v['color'] = COLORS[config.all_states.index(v['state']) % len(COLORS)]
else:
raise ValueError(f"Mode '{mode}' not implemented!")
background = 'white'
if layout is not None and 'geo' in layout:
for v in graph.vs():
v['x'] = (config.district_coords[v['district']][1]) / 180
v['y'] = (-config.district_coords[v['district']][0]) / 90
x_min = min(graph.vs()['x'])
x_max = max(graph.vs()['x'])
y_min = min(graph.vs()['y'])
y_max = max(graph.vs()['y'])
x_diff = x_max - x_min
y_diff = y_max - y_min
if layout == 'geo':
if x_diff > y_diff:
x_scale = x_diff / y_diff
y_scale = 1
else:
x_scale = 1
y_scale = y_diff / x_diff
elif layout == 'geo_strict':
x_scale = 1
y_scale = 1
else:
raise ValueError(f"Layout '{layout}' not implemented!")
avg_size = np.mean(config.district_sizes)
for v in graph.vs():
v['x'] = v['x'] + normal(0.0, x_scale * std * config.district_sizes[v['district']] / avg_size)
v['y'] = v['y'] + normal(0.0, y_scale * std * config.district_sizes[v['district']] / avg_size)
if layout == 'geo_strict':
diff = max(x_diff, y_diff) / 2
x_middle = (x_min + x_max) / 2
y_middle = (y_min + y_max) / 2
graph.add_vertex(x=x_middle + diff, y=y_middle + diff, color='black')
graph.add_vertex(x=x_middle - diff, y=y_middle + diff, color='black')
graph.add_vertex(x=x_middle + diff, y=y_middle - diff, color='black')
graph.add_vertex(x=x_middle - diff, y=y_middle - diff, color='black')
background = None
graph.vs()['size'] = node_size
if save_as is not None:
ig.plot(graph, layout=ig_layout, background=background, target=save_as)
else:
ig.plot(graph, layout=ig_layout, background=background)