Skip to content

Commit 307c1ec

Browse files
committed
Also update the backend when doing rcParams["backend"] = "foo".
1 parent 2047ace commit 307c1ec

File tree

3 files changed

+24
-10
lines changed

3 files changed

+24
-10
lines changed

lib/matplotlib/rcsetup.py

+18-3
Original file line numberDiff line numberDiff line change
@@ -262,13 +262,28 @@ def validate_backend(s):
262262
lambda s:
263263
s if s.startswith("module://")
264264
else ValidateInStrings('backend', all_backends, ignorecase=True)(s))(s)
265+
pyplot = sys.modules.get("matplotlib.pyplot")
265266
if len(candidates) == 1:
266-
return candidates[0]
267+
backend, = candidates
268+
if pyplot:
269+
# This import needs to be delayed (below too) because it is not
270+
# available at first import.
271+
from matplotlib import rcParams
272+
# Don't recurse.
273+
old_backend = rcParams["backend"]
274+
if old_backend == backend:
275+
return backend
276+
dict.__setitem__(rcParams, "backend", backend)
277+
try:
278+
pyplot.switch_backend(backend)
279+
except Exception:
280+
dict.__setitem__(rcParams, "backend", old_backend)
281+
raise
282+
return backend
267283
else:
268-
pyplot = sys.modules.get("matplotlib.pyplot")
269284
if pyplot:
270-
pyplot.switch_backend(candidates) # Actually resolves the backend.
271285
from matplotlib import rcParams
286+
pyplot.switch_backend(candidates) # Actually resolves the backend.
272287
return rcParams["backend"]
273288
else:
274289
return candidates

lib/matplotlib/tests/test_backend_qt4.py

+2-4
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,8 @@
1313
except ImportError:
1414
import mock
1515

16-
with matplotlib.rc_context(rc={'backend': 'Qt4Agg'}):
17-
qt_compat = pytest.importorskip('matplotlib.backends.qt_compat')
18-
from matplotlib.backends.backend_qt4 import (
19-
MODIFIER_KEYS, SUPER, ALT, CTRL, SHIFT) # noqa
16+
pytest.importorskip('PyQt4')
17+
qt_compat = pytest.importorskip('matplotlib.backends.qt_compat')
2018

2119
QtCore = qt_compat.QtCore
2220
_, ControlModifier, ControlKey = MODIFIER_KEYS[CTRL]

lib/matplotlib/tests/test_backend_qt5.py

+4-3
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,10 @@
1616
except ImportError:
1717
import mock
1818

19-
with matplotlib.rc_context(rc={'backend': 'Qt5Agg'}):
20-
qt_compat = pytest.importorskip('matplotlib.backends.qt_compat',
21-
minversion='5')
19+
pytest.importorskip('PyQt5')
20+
qt_compat = pytest.importorskip('matplotlib.backends.qt_compat',
21+
minversion='5')
22+
2223
from matplotlib.backends.backend_qt5 import (
2324
MODIFIER_KEYS, SUPER, ALT, CTRL, SHIFT) # noqa
2425

0 commit comments

Comments
 (0)