Skip to content

Commit efc3a0f

Browse files
committed
Plot for distribution of wins
1 parent c202f7f commit efc3a0f

File tree

2 files changed

+42
-2
lines changed

2 files changed

+42
-2
lines changed

axelrod/plot.py

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import numpy
2+
13
matplotlib_installed = True
24
try:
35
import matplotlib.pyplot as plt
@@ -11,7 +13,6 @@ class Plot(object):
1113

1214
def __init__(self, result_set):
1315
self.result_set = result_set
14-
# self._nplayers = self.result_set.nplayers
1516
self.matplotlib_installed = matplotlib_installed
1617

1718
@property
@@ -55,6 +56,45 @@ def boxplot(self):
5556
plt.title(self._boxplot_title)
5657
return figure
5758

59+
@property
60+
def _winplot_dataset(self):
61+
# Sort wins by median
62+
wins = self.result_set.wins
63+
ranked_names = self.result_set.ranked_names
64+
medians = map(numpy.median, wins)
65+
players = self.result_set.players
66+
medians = sorted([(m, i) for (i, m) in enumerate(medians)], reverse=True)
67+
# Reorder and grab names
68+
wins = [wins[x[1]] for x in medians]
69+
ranked_names = [str(players[x[1]]) for x in medians]
70+
#zipped = [(wins[i], str(self.players[i])) for (m, i) in medians]
71+
#wins, ranked_names = zip(*zipped) # unzip!
72+
return wins, ranked_names
73+
74+
@property
75+
def _winplot_title(self):
76+
return ("Distribution of wins"
77+
" {} turns repeated {} times ({} strategies)").format(
78+
self.result_set.turns,
79+
self.result_set.repetitions,
80+
len(self.result_set.ranking))
81+
82+
def winplot(self):
83+
if not self.matplotlib_installed:
84+
return None
85+
86+
wins, ranked_names = self._winplot_dataset
87+
88+
figure = plt.figure()
89+
plt.boxplot(wins)
90+
plt.xticks(
91+
self._boxplot_xticks_locations,
92+
ranked_names,
93+
rotation=90)
94+
plt.tick_params(axis='both', which='both', labelsize=7)
95+
plt.title(self._winplot_title)
96+
return figure
97+
5898
def payoff(self):
5999

60100
if not self.matplotlib_installed:

axelrod/tournament_manager.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ def _save_plots(self, tournament, ecosystem=None, image_format="svg"):
126126
self._logger.error('The matplotlib library is not installed. '
127127
'No plots will be produced')
128128
return
129-
for plot_type in ('boxplot', 'payoff'):
129+
for plot_type in ('boxplot', 'payoff', 'winplot'):
130130
figure = getattr(plot, plot_type)()
131131
file_name = self._output_file_path(
132132
tournament.name + '_' + plot_type, image_format)

0 commit comments

Comments
 (0)