swdata/comp_co_per_match.py
2018-03-11 17:27:59 +08:00

52 lines
1.4 KiB
Python

import json
from matplotlib import pyplot as plt
from island.match import Match
from island.matches import Matches
import numpy as np
def calc_co(matches):
x = [0,0,0,0,0,0]
for j in range(len(matches.data)):
if len(matches.data[j].query('action', 'done').raw_data) < 5:
continue
coop = 0
rows = matches.data[j].query('action', 'done').raw_data
for row in rows:
if row['act_a'] == 'C' and row['act_b'] == 'C':
coop += 1
if rows:
per = float(coop) / len(rows)
x[int(per*100)//20] += 1
x[4] += x[5]
x.pop()
s = sum(x)
for i in range(5):
x[i] /= s
return x
casual = Matches('wos-data-casual')
compete = Matches('wos-data-new')
ca = calc_co(casual)
co = calc_co(compete)
fig = plt.figure(figsize=(5,4))
ax = fig.gca()
index = np.arange(5)
bar_width = 0.35
rects1 = ax.bar(index, ca, bar_width, color='#00b894', label='Casual games')
# rects2 = ax.bar(index + bar_width, co, bar_width, color='#005CAF', label='Competition')
rects2 = ax.bar(index + bar_width, co, bar_width, color='#6c5ce7', label='Competition')
ax.set_xticks(index + bar_width / 2)
ax.set_xticklabels(['0~0.2','0.2~0.4','0.4~0.6','0.6~0.8','0.8~1'])
ax.legend()
# fig.autofmt_xdate()
fig.set_size_inches(5, 4)
plt.xlabel('Frequency of Cooperation Actions per Match')
plt.ylabel('Fraction of Matches')
fig.tight_layout()
# plt.show()
plt.savefig('graph/comp_co_per_match.eps')