-
Notifications
You must be signed in to change notification settings - Fork 0
/
plot-cooperators-ratio.py
76 lines (54 loc) · 2.13 KB
/
plot-cooperators-ratio.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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# %%
import pandas as pd
import numpy as np
import matplotlib as mpl
import matplotlib.colors as colors
mpl.rc('text', usetex=True)
mpl.rc('font', family='serif')
mpl.rc('font', size=9)
# %%
# file with data from the experiment
# Note: header=6 is for NetLogo data
exp_desc = 'cooperators-diversity'
data = pd.read_csv(exp_desc + '.csv', header=6)
sxs = { 'vN' : "von Neumann", 'rvN' : 'random von Neumann', 'M': 'Moore', 'rM': 'random Moore'} #, 'rvNM': 'random von Neumann or random Moore' }
#sxs = { 'vN' : "von Neumann"}
markers = ['o', 'x', 's', '^', '2']
colors = ['k--', 'r-.', 'b:', 'g-', 'm']
# %% calculate plotted data
# [
# '[run number]', 'noise-factor', 'synergy-factor', 'neighborhood-type',
# 'world-size', '[step]', 'cooperators-fraction', 'mean-cooperators1k'
# ]
v = [ 'synergy-factor', 'neighborhood-type', 'world-size', 'mean-cooperators-fraction' ]
# NOTE: start with some subset of data
var0s = data['synergy-factor'].unique()
var1s = data['neighborhood-type'].unique()[:4]
var2s = data['world-size'].unique()[:1]
exp_desc = exp_desc + '_' + str( var2s[0] )
df = pd.DataFrame(columns=v)
for v0 in var0s:
for v1 in var1s:
for v2 in var2s:
df.loc[len(df.index)] = [
v0,
v1,
v2,
data[(data[v[0]] == v0) & (data[v[1]] == v1) & (data[v[2]] == v2)]['cooperators-fraction'].mean()
]
# %% plot data
fig = mpl.figure.Figure(figsize=(4, 3))
axs = fig.add_subplot()
for i, nt in enumerate( list(sxs.values())):
plot_data = df[df['neighborhood-type'] == nt][['synergy-factor', 'mean-cooperators-fraction']].to_numpy()
axs.plot(plot_data.T[0], plot_data.T[1], color=colors[i][0], marker=markers[i], fillstyle='none', markersize=4, label=nt, linestyle='--', lw=0.75)
axs.set_xlabel('synergy factor $r$')
axs.set_ylabel('mean cooperators fraction')
axs.grid(True, linestyle=':', linewidth=0.5, c='k')
axs.legend(ncols=1,loc='lower right',fontsize='8')
# %%
fig.tight_layout()
display(fig)
fig.savefig("plot_" + exp_desc + ".pdf", format="pdf", bbox_inches='tight')