1+ import numpy
2+
13matplotlib_installed = True
24try :
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 :
0 commit comments