From bda8f01520ee8a7e44f5635021bcec25503b2ca0 Mon Sep 17 00:00:00 2001
From: Joseph Weston <joseph.weston08@gmail.com>
Date: Tue, 5 Dec 2017 10:22:10 +0100
Subject: [PATCH] use matplotlib's 'Agg' backend in tests if not already
 selected

This backend does not require a display to be connected,
which is good for tests.

Closes #83.
---
 kwant/tests/test_plotter.py | 16 ++++++++++++++--
 1 file changed, 14 insertions(+), 2 deletions(-)

diff --git a/kwant/tests/test_plotter.py b/kwant/tests/test_plotter.py
index 0772dca3..84a3a5c6 100644
--- a/kwant/tests/test_plotter.py
+++ b/kwant/tests/test_plotter.py
@@ -15,14 +15,26 @@ from math import cos, sin
 import scipy.integrate
 import scipy.stats
 import pytest
+import sys
 
 import kwant
-from kwant import plotter
 from kwant._common import ensure_rng
 
-if plotter.mpl_enabled:
+try:
     from mpl_toolkits import mplot3d
+    import matplotlib
+
+    # If the user did not already choose a backend, then choose
+    # the one with the least dependencies.
+    # This check is the same as the one performed inside matplotlib.use.
+    if 'matplotlib.backends' not in sys.modules:
+        matplotlib.use('Agg')
+
     from matplotlib import pyplot  # pragma: no flakes
+except ImportError:
+    pass
+
+from kwant import plotter
 
 
 def test_importable_without_matplotlib():
-- 
GitLab