Skip to content

Commit 90be7ca

Browse files
committed
Fix bug: correct plotting on axes.
Closes #896
1 parent 7d46b8f commit 90be7ca

File tree

2 files changed

+52
-11
lines changed

2 files changed

+52
-11
lines changed

axelrod/plot.py

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -65,13 +65,14 @@ def _violinplot(
6565
spacing = 4
6666
positions = spacing * arange(1, self.nplayers + 1, 1)
6767
figure.set_size_inches(width, height)
68-
plt.violinplot(data, positions=positions, widths=spacing / 2,
69-
showmedians=True, showextrema=False)
70-
plt.xticks(positions, names, rotation=90)
71-
plt.xlim(0, spacing * (self.nplayers + 1))
72-
plt.tick_params(axis='both', which='both', labelsize=8)
68+
ax.violinplot(data, positions=positions, widths=spacing / 2,
69+
showmedians=True, showextrema=False)
70+
ax.set_xticks(positions)
71+
ax.set_xticklabels(names, rotation=90)
72+
ax.set_xlim([0, spacing * (self.nplayers + 1)])
73+
ax.tick_params(axis='both', which='both', labelsize=8)
7374
if title:
74-
plt.title(title)
75+
ax.set_title(title)
7576
return figure
7677

7778
# Box and Violin plots for mean score, score differences, wins, and match
@@ -260,11 +261,11 @@ def stackplot(
260261
ax.yaxis.set_label_position("right")
261262
ax.yaxis.labelpad = 25.0
262263

263-
plt.ylim([0.0, 1.0])
264-
plt.ylabel('Relative population size')
265-
plt.xlabel('Turn')
264+
ax.set_ylim([0.0, 1.0])
265+
ax.set_ylabel('Relative population size')
266+
ax.set_xlabel('Turn')
266267
if title is not None:
267-
plt.title(title)
268+
ax.set_title(title)
268269

269270
trans = transforms.blended_transform_factory(
270271
ax.transAxes, ax.transData)

axelrod/tests/unit/test_plot.py

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,23 @@ def test_boxplot(self):
137137
else: # pragma: no cover
138138
self.skipTest('matplotlib not installed')
139139

140+
def test_boxplot_with_passed_axes(self):
141+
# Test that can plot on a given matplotlib axes
142+
if matplotlib_installed:
143+
fig, axarr = plt.subplots(2, 2)
144+
self.assertEqual(axarr[0, 1].get_ylim(), (0, 1))
145+
plot = axelrod.Plot(self.test_result_set)
146+
plot.boxplot(ax=axarr[0, 1])
147+
self.assertNotEqual(axarr[0, 1].get_ylim(), (0, 1))
148+
149+
# Plot on another axes with a title
150+
plot.boxplot(title="Test", ax=axarr[1, 0])
151+
self.assertNotEqual(axarr[1, 0].get_ylim(), (0, 1))
152+
self.assertEqual(axarr[1, 0].get_title(), "Test")
153+
154+
else: # pragma: no cover
155+
self.skipTest('matplotlib not installed')
156+
140157
def test_boxplot_with_title(self):
141158
if matplotlib_installed:
142159
plot = axelrod.Plot(self.test_result_set)
@@ -219,10 +236,11 @@ def test_payoff_with_title(self):
219236
else: # pragma: no cover
220237
self.skipTest('matplotlib not installed')
221238

222-
def test_ecosystem(self):
239+
def test_stackplot(self):
223240
if matplotlib_installed:
224241
eco = axelrod.Ecosystem(self.test_result_set)
225242
eco.reproduce(100)
243+
226244
plot = axelrod.Plot(self.test_result_set)
227245
fig = plot.stackplot(eco)
228246
self.assertIsInstance(fig, matplotlib.pyplot.Figure)
@@ -236,6 +254,28 @@ def test_ecosystem(self):
236254
else: # pragma: no cover
237255
self.skipTest('matplotlib not installed')
238256

257+
def test_stackplot_with_passed_axes(self):
258+
# Test that can plot on a given matplotlib axes
259+
if matplotlib_installed:
260+
eco = axelrod.Ecosystem(self.test_result_set)
261+
eco.reproduce(100)
262+
plot = axelrod.Plot(self.test_result_set)
263+
264+
fig, axarr = plt.subplots(2, 2)
265+
self.assertEqual(axarr[0, 1].get_xlim(), (0, 1))
266+
267+
plot.stackplot(eco, ax=axarr[0, 1])
268+
self.assertNotEqual(axarr[0, 1].get_xlim(), (0, 1))
269+
270+
# Plot on another axes with a title
271+
plot.stackplot(eco ,title="dummy title", ax=axarr[1, 0])
272+
self.assertNotEqual(axarr[1, 0].get_xlim(), (0, 1))
273+
self.assertEqual(axarr[1, 0].get_title(), "dummy title")
274+
275+
else: # pragma: no cover
276+
self.skipTest('matplotlib not installed')
277+
278+
239279
def test_all_plots(self):
240280
if matplotlib_installed:
241281
plot = axelrod.Plot(self.test_result_set)

0 commit comments

Comments
 (0)