@@ -28,25 +28,32 @@ def default_cmap():
28
28
29
29
30
30
class Plot (object ):
31
-
32
31
def __init__ (self , result_set ):
33
32
self .result_set = result_set
34
33
self .matplotlib_installed = matplotlib_installed
34
+ self .nplayers = self .result_set .nplayers
35
+ self .players = self .result_set .players
35
36
36
- def _violinplot (self , data , names , title = None ):
37
+ def _violinplot (self , data , names , title = None , ax = None ):
37
38
"""For making violinplots."""
38
39
if not self .matplotlib_installed :
39
40
return None
40
- nplayers = self .result_set .nplayers
41
- width = max (nplayers / 3 , 12 )
41
+
42
+ if ax is None :
43
+ _ , ax = plt .subplots ()
44
+ else :
45
+ ax = ax
46
+
47
+ figure = ax .get_figure ()
48
+ width = max (self .nplayers / 3 , 12 )
42
49
height = width / 2
43
- figure = plt .figure (figsize = (width , height ))
44
50
spacing = 4
45
- positions = spacing * arange (1 , nplayers + 1 , 1 )
51
+ positions = spacing * arange (1 , self .nplayers + 1 , 1 )
52
+ figure .set_size_inches (width , height )
46
53
plt .violinplot (data , positions = positions , widths = spacing / 2 ,
47
54
showmedians = True , showextrema = False )
48
55
plt .xticks (positions , names , rotation = 90 )
49
- plt .xlim (0 , spacing * (nplayers + 1 ))
56
+ plt .xlim (0 , spacing * (self . nplayers + 1 ))
50
57
plt .tick_params (axis = 'both' , which = 'both' , labelsize = 8 )
51
58
if title :
52
59
plt .title (title )
@@ -68,33 +75,32 @@ def _boxplot_xticks_locations(self):
68
75
def _boxplot_xticks_labels (self ):
69
76
return [str (n ) for n in self .result_set .ranked_names ]
70
77
71
- def boxplot (self , title = None ):
78
+ def boxplot (self , title = None , ax = None ):
72
79
"""For the specific mean score boxplot."""
73
80
data = self ._boxplot_dataset
74
81
names = self ._boxplot_xticks_labels
75
- figure = self ._violinplot (data , names , title = title )
82
+ figure = self ._violinplot (data , names , title = title , ax = ax )
76
83
return figure
77
84
78
85
@property
79
86
def _winplot_dataset (self ):
80
87
# Sort wins by median
81
88
wins = self .result_set .wins
82
- players = self .result_set .players
83
89
medians = map (median , wins )
84
90
medians = sorted (
85
91
[(m , i ) for (i , m ) in enumerate (medians )], reverse = True )
86
92
# Reorder and grab names
87
93
wins = [wins [x [- 1 ]] for x in medians ]
88
- ranked_names = [str (players [x [- 1 ]]) for x in medians ]
94
+ ranked_names = [str (self . players [x [- 1 ]]) for x in medians ]
89
95
return wins , ranked_names
90
96
91
- def winplot (self , title = None ):
97
+ def winplot (self , title = None , ax = None ):
92
98
"""Plots the distributions for the number of wins for each strategy."""
93
99
if not self .matplotlib_installed :
94
100
return None
95
101
96
102
data , names = self ._winplot_dataset
97
- figure = self ._violinplot (data , names , title )
103
+ figure = self ._violinplot (data , names , title = title , ax = ax )
98
104
# Expand ylim a bit
99
105
maximum = max (max (w ) for w in data )
100
106
plt .ylim (- 0.5 , 0.5 + maximum )
@@ -108,17 +114,16 @@ def _sd_ordering(self):
108
114
def _sdv_plot_dataset (self ):
109
115
ordering = self ._sd_ordering
110
116
diffs = self .result_set .score_diffs
111
- players = self .result_set .players
112
117
# Reorder and grab names
113
118
diffs = [diffs [i ] for i in ordering ]
114
- ranked_names = [str (players [i ]) for i in ordering ]
119
+ ranked_names = [str (self . players [i ]) for i in ordering ]
115
120
return diffs , ranked_names
116
121
117
- def sdvplot (self , title = None ):
122
+ def sdvplot (self , title = None , ax = None ):
118
123
"""Score difference violinplots to visualize the distributions of how
119
124
players attain their payoffs."""
120
125
diffs , ranked_names = self ._sdv_plot_dataset
121
- figure = self ._violinplot (diffs , ranked_names , title )
126
+ figure = self ._violinplot (diffs , ranked_names , title = title , ax = ax )
122
127
return figure
123
128
124
129
@property
@@ -128,15 +133,13 @@ def _lengthplot_dataset(self):
128
133
for length in rep [playeri ]] for playeri in
129
134
self .result_set .ranking ]
130
135
131
- def lengthplot (self , title = None ):
136
+ def lengthplot (self , title = None , ax = None ):
132
137
"""For the specific match length boxplot."""
133
138
data = self ._lengthplot_dataset
134
139
names = self ._boxplot_xticks_labels
135
- figure = self ._violinplot (data , names , title = title )
140
+ figure = self ._violinplot (data , names , title = title , ax = ax )
136
141
return figure
137
142
138
- # Payoff heatmaps
139
-
140
143
@property
141
144
def _payoff_dataset (self ):
142
145
pm = self .result_set .payoff_matrix
@@ -156,17 +159,20 @@ def _pdplot_dataset(self):
156
159
ranked_names = [str (players [i ]) for i in ordering ]
157
160
return matrix , ranked_names
158
161
159
- def _payoff_heatmap (self , data , names , title = None ):
162
+ def _payoff_heatmap (self , data , names , title = None , ax = None ):
160
163
"""Generic heatmap plot"""
161
164
if not self .matplotlib_installed :
162
165
return None
163
166
164
- nplayers = self .result_set .nplayers
165
- width = max (nplayers / 4 , 12 )
167
+ if ax is None :
168
+ _ , ax = plt .subplots ()
169
+ else :
170
+ ax = ax
171
+
172
+ figure = ax .get_figure ()
173
+ width = max (self .nplayers / 4 , 12 )
166
174
height = width
167
- figure , ax = plt .subplots ()
168
- figure .set_figwidth (width )
169
- figure .set_figheight (height )
175
+ figure .set_size_inches (width , height )
170
176
cmap = default_cmap ()
171
177
mat = ax .matshow (data , cmap = cmap )
172
178
plt .xticks (range (self .result_set .nplayers ))
@@ -182,29 +188,33 @@ def _payoff_heatmap(self, data, names, title=None):
182
188
plt .colorbar (mat , cax = cax )
183
189
return figure
184
190
185
- def pdplot (self , title = None ):
191
+ def pdplot (self , title = None , ax = None ):
186
192
"""Payoff difference heatmap to visualize the distributions of how
187
193
players attain their payoffs."""
188
194
matrix , names = self ._pdplot_dataset
189
- return self ._payoff_heatmap (matrix , names , title )
195
+ return self ._payoff_heatmap (matrix , names , title = title , ax = ax )
190
196
191
- def payoff (self , title = None ):
197
+ def payoff (self , title = None , ax = None ):
192
198
"""Payoff heatmap to visualize the distributions of how
193
199
players attain their payoffs."""
194
200
data = self ._payoff_dataset
195
201
names = self .result_set .ranked_names
196
- return self ._payoff_heatmap (data , names , title )
202
+ return self ._payoff_heatmap (data , names , title = title , ax = ax )
197
203
198
204
# Ecological Plot
199
205
200
- def stackplot (self , eco , title = None , logscale = True ):
201
-
206
+ def stackplot (self , eco , title = None , logscale = True , ax = None ):
202
207
if not self .matplotlib_installed :
203
208
return None
204
209
205
210
populations = eco .population_sizes
206
211
207
- figure , ax = plt .subplots ()
212
+ if ax is None :
213
+ _ , ax = plt .subplots ()
214
+ else :
215
+ ax = ax
216
+
217
+ figure = ax .get_figure ()
208
218
turns = range (len (populations ))
209
219
pops = [[populations [iturn ][ir ] for iturn in turns ] for ir in self .result_set .ranking ]
210
220
ax .stackplot (turns , * pops )
@@ -225,7 +235,7 @@ def stackplot(self, eco, title=None, logscale=True):
225
235
x = - 0.01
226
236
y = (i + 0.5 ) * 1.0 / self .result_set .nplayers
227
237
ax .annotate (n , xy = (x , y ), xycoords = trans , clip_on = False ,
228
- va = 'center' , ha = 'right' , fontsize = 5 )
238
+ va = 'center' , ha = 'right' , fontsize = 5 )
229
239
ticks .append (y )
230
240
ax .set_yticks (ticks )
231
241
ax .tick_params (direction = 'out' )
0 commit comments