Skip to content

Commit f3c3afd

Browse files
committed
Add missing test for heatmap and save_all_plots.
Also change some ResultSet reads to not display a progress bar. This pushes the coverage of this file to 100%.
1 parent 2427db6 commit f3c3afd

File tree

2 files changed

+27
-10
lines changed

2 files changed

+27
-10
lines changed

axelrod/plot.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -197,17 +197,17 @@ def _payoff_heatmap(
197197
matplotlib_version = matplotlib.__version__
198198
cmap = default_cmap(matplotlib_version)
199199
mat = ax.matshow(data, cmap=cmap)
200-
plt.xticks(range(self.result_set.nplayers))
201-
plt.yticks(range(self.result_set.nplayers))
200+
ax.set_xticks(range(self.result_set.nplayers))
201+
ax.set_yticks(range(self.result_set.nplayers))
202202
ax.set_xticklabels(names, rotation=90)
203203
ax.set_yticklabels(names)
204-
plt.tick_params(axis='both', which='both', labelsize=16)
204+
ax.tick_params(axis='both', which='both', labelsize=16)
205205
if title:
206-
plt.xlabel(title)
206+
ax.set_xlabel(title)
207207
# Make the colorbar match up with the plot
208208
divider = make_axes_locatable(plt.gca())
209209
cax = divider.append_axes("right", "5%", pad="3%")
210-
plt.colorbar(mat, cax=cax)
210+
plt.colorbar(mat, cax=cax, ax=ax)
211211
return figure
212212

213213
def pdplot(

axelrod/tests/unit/test_plot.py

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,8 @@ def setUpClass(cls):
4242
except KeyError:
4343
cls.interactions[index_pair] = [match.result]
4444

45-
cls.test_result_set = axelrod.ResultSet(cls.players, cls.interactions)
45+
cls.test_result_set = axelrod.ResultSet(cls.players, cls.interactions,
46+
progress_bar=False)
4647
cls.expected_boxplot_dataset = [
4748
[(17 / 5 + 9 / 5) / 2 for _ in range(3)],
4849
[(13 / 5 + 4 / 5) / 2 for _ in range(3)],
@@ -116,7 +117,7 @@ def test_init_from_resulsetfromfile(self):
116117
repetitions=2)
117118
tournament.play(filename=tmp_file.name, progress_bar=False)
118119
tmp_file.close()
119-
rs = axelrod.ResultSetFromFile(tmp_file.name)
120+
rs = axelrod.ResultSetFromFile(tmp_file.name, progress_bar=False)
120121

121122
plot = axelrod.Plot(rs)
122123
self.assertEqual(plot.result_set, rs)
@@ -159,9 +160,9 @@ def test_boxplot_with_passed_axes(self):
159160
self.assertNotEqual(axarr[0, 1].get_ylim(), (0, 1))
160161

161162
# Plot on another axes with a title
162-
plot.boxplot(title="Test", ax=axarr[1, 0])
163+
plot.boxplot(title="dummy title", ax=axarr[1, 0])
163164
self.assertNotEqual(axarr[1, 0].get_ylim(), (0, 1))
164-
self.assertEqual(axarr[1, 0].get_title(), "Test")
165+
self.assertEqual(axarr[1, 0].get_title(), "dummy title")
165166

166167
else: # pragma: no cover
167168
self.skipTest('matplotlib not installed')
@@ -248,6 +249,22 @@ def test_payoff_with_title(self):
248249
else: # pragma: no cover
249250
self.skipTest('matplotlib not installed')
250251

252+
def test_payoff_with_passed_axes(self):
253+
if matplotlib_installed:
254+
plot = axelrod.Plot(self.test_result_set)
255+
fig, axarr = plt.subplots(2, 2)
256+
self.assertEqual(axarr[0, 1].get_xlim(), (0, 1))
257+
258+
plot.payoff(ax=axarr[0, 1])
259+
self.assertNotEqual(axarr[0, 1].get_xlim(), (0, 1))
260+
261+
# Plot on another axes with a title
262+
plot.payoff(title="dummy title", ax=axarr[1, 0])
263+
self.assertNotEqual(axarr[1, 0].get_xlim(), (0, 1))
264+
self.assertEqual(axarr[1, 0].get_xlabel(), "dummy title")
265+
else: # pragma: no cover
266+
self.skipTest('matplotlib not installed')
267+
251268
def test_stackplot(self):
252269
if matplotlib_installed:
253270
eco = axelrod.Ecosystem(self.test_result_set)
@@ -298,6 +315,6 @@ def test_all_plots(self):
298315
self.assertIsNone(
299316
plot.save_all_plots(prefix="test_outputs/",
300317
title_prefix="A prefix",
301-
progress_bar=False))
318+
progress_bar=True))
302319
else: # pragma: no cover
303320
self.skipTest('matplotlib not installed')

0 commit comments

Comments
 (0)