swdata/interactions_per_round.py
2018-03-27 21:48:53 +08:00

64 lines
1.6 KiB
Python

import json
from matplotlib import pyplot as plt
from island.match import Match
from island.matches import Matches
import numpy as np
matches = Matches('wos-data-new')
max_round = 14
coopr = []
yerr_min = []
yerr_max = []
x = np.arange(max_round)
bx = []
y = np.zeros((5, max_round))
sy = np.zeros(max_round)
survivals = {}
with open('survivals.json', 'r') as f:
survivals = json.load(f)
for i in range(max_round):
co = {}
for j in range(len(matches.data)):
if str(i+1) not in survivals[matches.names[j]]:
continue
for k in survivals[matches.names[j]][str(i+1)]:
co[k] = 0
rows = matches.data[j].query('action', 'done').where(lambda x: x['rno']==i+1).raw_data
for row in rows:
if row['a'] in co:
co[row['a']] += 1
if row['b'] in co:
co[row['b']] += 1
for j in co.values():
if j <= 4:
y[j][i] += 1
else:
y[4][i] += 1
sy[i] += 1
# bx.append(list(co.values()))
# plt.figure()
# plt.boxplot(bx, showmeans=True, meanline=True)
# plt.show()
labels = ["k = 0 ", "k = 1", "k = 2", "k = 3", "k ⩾ 4"]
colors = ['#0984e3', '#fdcb6e', '#00b894', '#6c5ce7', '#d63031', '#0984e3']
y /= sy
fig = plt.figure(figsize=(6, 4))
ax = fig.gca()
ax.stackplot(x+1, y, labels=labels, colors=colors)
ax.set_xticks(x+1)
ax.set_xticklabels(x+1)
ax.set_xlim(1,max_round)
ax.set_ylim(0, 1)
ax.set_xlabel('Round')
ax.set_ylabel('Fraction of k')
ax.legend()
plt.tight_layout()
plt.show()
# plt.savefig('graph/interaction_per_round.eps')