Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions lib/matplotlib/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -674,7 +674,12 @@ def __getitem__(self, key):
val = dict.__getitem__(self, key)
if val is rcsetup._auto_backend_sentinel:
from matplotlib import pyplot as plt
if plt._backend_mod is not None:
import matplotlib.backends
return matplotlib.backends.backend
plt.switch_backend(rcsetup._auto_backend_sentinel)
return dict.__getitem__(self, key)
return val

return dict.__getitem__(self, key)

Expand Down
27 changes: 27 additions & 0 deletions lib/matplotlib/tests/test_backends_interactive.py
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,33 @@ def test_lazy_auto_backend_selection():
timeout=_test_timeout)


def _impl_get_backend_preserves_figures():
import matplotlib
from matplotlib import get_backend, rc_context, rcsetup
from matplotlib import pyplot as plt

plt.close("all")
matplotlib.rcParams["backend"] = rcsetup._auto_backend_sentinel

with rc_context():
plt.figure()

matplotlib.rcParams["backend"] = rcsetup._auto_backend_sentinel
assert plt._backend_mod is not None

gcf = plt._pylab_helpers.Gcf
assert tuple(gcf.figs.items())
before = (id(gcf), tuple(gcf.figs.items()))
get_backend()
after = (id(gcf), tuple(gcf.figs.items()))
assert before == after


def test_get_backend_preserves_figures():
_run_helper(_impl_get_backend_preserves_figures,
timeout=_test_timeout)


def _implqt5agg():
import matplotlib.backends.backend_qt5agg # noqa
import sys
Expand Down