Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 40 additions & 1 deletion axelrod/plot.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import numpy

matplotlib_installed = True
try:
import matplotlib.pyplot as plt
Expand All @@ -11,7 +13,6 @@ class Plot(object):

def __init__(self, result_set):
self.result_set = result_set
# self._nplayers = self.result_set.nplayers
self.matplotlib_installed = matplotlib_installed

@property
Expand Down Expand Up @@ -55,6 +56,44 @@ def boxplot(self):
plt.title(self._boxplot_title)
return figure

@property
def _winplot_dataset(self):
# Sort wins by median
wins = self.result_set.wins
players = self.result_set.players
medians = map(numpy.median, wins)
medians = sorted([(m, i) for (i, m) in enumerate(medians)], reverse=True)
# Reorder and grab names
wins = [wins[x[1]] for x in medians]
ranked_names = [str(players[x[1]]) for x in medians]
return wins, ranked_names

@property
def _winplot_title(self):
return ("Distributions of wins:"
" {} turns repeated {} times ({} strategies)").format(
self.result_set.turns,
self.result_set.repetitions,
len(self.result_set.ranking))

def winplot(self):
if not self.matplotlib_installed:
return None

wins, ranked_names = self._winplot_dataset
maximum = max(max(w) for w in wins)

figure = plt.figure()
plt.boxplot(wins)
plt.xticks(
self._boxplot_xticks_locations,
ranked_names,
rotation=90)
plt.tick_params(axis='both', which='both', labelsize=7)
plt.title(self._winplot_title)
plt.ylim(-0.5, 0.5 + maximum)
return figure

def payoff(self):

if not self.matplotlib_installed:
Expand Down
21 changes: 20 additions & 1 deletion axelrod/tests/unit/test_plot.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,13 @@ def setUpClass(cls):
cls.expected_boxplot_xticks_labels = ['Player3', 'Player1', 'Player2']
cls.expected_boxplot_title = ('Mean score per stage game over 5 turns'
' repeated 2 times (3 strategies)')

cls.expected_payoff_dataset = [
[0.0, 3.2, 3.2],
[4.2, 0.0, 2.0],
[3.6, 1.8, 0.0]]
cls.expected_winplot_dataset = ([[2, 4], [0, 2], [0, 0]],
['Player1', 'Player2', 'Player3'])
cls.expected_winplot_title = "Distributions of wins: 5 turns repeated 2 times (3 strategies)"

def test_init(self):
result_set = self.test_result_set
Expand Down Expand Up @@ -67,6 +69,23 @@ def test_boxplot(self):
else:
self.skipTest('matplotlib not installed')

def test_winplot_dataset(self):
plot = axelrod.Plot(self.test_result_set)
self.assertSequenceEqual(
plot._winplot_dataset,
self.expected_winplot_dataset)

def test_winplot_title(self):
plot = axelrod.Plot(self.test_result_set)
self.assertEqual(plot._winplot_title, self.expected_winplot_title)

def test_winplot(self):
if matplotlib_installed:
plot = axelrod.Plot(self.test_result_set)
self.assertIsInstance(plot.winplot(), matplotlib.pyplot.Figure)
else:
self.skipTest('matplotlib not installed')

def test_payoff_dataset(self):
plot = axelrod.Plot(self.test_result_set)
self.assertSequenceEqual(
Expand Down
2 changes: 1 addition & 1 deletion axelrod/tournament_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ def _save_plots(self, tournament, ecosystem=None, image_format="svg"):
self._logger.error('The matplotlib library is not installed. '
'No plots will be produced')
return
for plot_type in ('boxplot', 'payoff'):
for plot_type in ('boxplot', 'payoff', 'winplot'):
figure = getattr(plot, plot_type)()
file_name = self._output_file_path(
tournament.name + '_' + plot_type, image_format)
Expand Down
Loading