8000 Also update the backend when doing rcParams["backend"] = "foo". · matplotlib/matplotlib@6725bf4 · GitHub
[go: up one dir, main page]

Skip to content

Commit 6725bf4

Browse files
committed
Also update the backend when doing rcParams["backend"] = "foo".
1 parent a477c8e commit 6725bf4

File tree

3 files changed

+24
-10
lines changed

3 files changed

+24
-10
lines changed

lib/matplotlib/rcsetup.py

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -260,13 +260,28 @@ def validate_backend(s):
260260
lambda s:
261261
s if s.startswith("module://")
262262
else ValidateInStrings('backend', all_backends, ignorecase=True)(s))(s)
263+
pyplot = sys.modules.get("matplotlib.pyplot")
263264
if len(candidates) == 1:
264-
return candidates[0]
265+
backend, = candidates
266+
if pyplot:
267+
# This import needs to be delayed (below too) because it is not
268+
# available at first import.
269+
from matplotlib import rcParams
270+
# Don't recurse.
271+
old_backend = rcParams["backend"]
272+
if old_backend == backend:
273+
return backend
274+
dict.__setitem__(rcParams, "backend", backend)
275+
try:
276+
pyplot.switch_backend(backend)
277+
except Exception:
278+
dict.__setitem__(rcParams, "backend", old_backend)
279+
raise
280+
return backend
265281
else:
266-
pyplot = sys.modules.get("matplotlib.pyplot")
267282
if pyplot:
268-
pyplot.switch_backend(candidates) # Actually resolves the backend.
269283
from matplotlib import rcParams
284+
pyplot.switch_backend(candidates) # Actually resolves the backend.
270285
return rcParams["backend"]
271286
else:
272287
return candidates

lib/matplotlib/tests/test_backend_qt4.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,8 @@
1212
except ImportError:
1313
import mock
1414

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

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

lib/matplotlib/tests/test_backend_qt5.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,10 @@
1515
except ImportError:
1616
import mock
1717

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

0 commit comments

Comments
 (0)
0