Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 3fd58ed

Browse files
tacaswellQuLogic
andcommitted
FIX: enable install_repl_displayhook when switching backends
closes #23042 In #22005 we change `pyplot` so that at import time we do not force a switch of the backend and install the repl displayhook. However, this meant that in some cases (primarily through `ipython --pylab`) to end up with a session where `install_repl_displayhook` had never been called. Co-authored-by: Elliott Sales de Andrade <[email protected]>
1 parent f25c2d0 commit 3fd58ed

File tree

2 files changed

+28
-6
lines changed

2 files changed

+28
-6
lines changed

lib/matplotlib/pyplot.py

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -206,12 +206,6 @@ def _get_backend_mod():
206206
# will (re)import pyplot and then call switch_backend if we need to
207207
# resolve the auto sentinel)
208208
switch_backend(dict.__getitem__(rcParams, "backend"))
209-
# Just to be safe. Interactive mode can be turned on without calling
210-
# `plt.ion()` so register it again here. This is safe because multiple
211-
# calls to `install_repl_displayhook` are no-ops and the registered
212-
# function respects `mpl.is_interactive()` to determine if it should
213-
# trigger a draw.
214-
install_repl_displayhook()
215209
return _backend_mod
216210

217211

@@ -302,6 +296,10 @@ class backend_mod(matplotlib.backend_bases._Backend):
302296
# See https://github.com/matplotlib/matplotlib/issues/6092
303297
matplotlib.backends.backend = newbackend
304298

299+
# make sure the repl display hook is installed in case we become
300+
# interactive
301+
install_repl_displayhook()
302+
305303

306304
def _warn_if_gui_out_of_main_thread():
307305
if (_get_required_interactive_framework(_get_backend_mod())

lib/matplotlib/tests/test_pyplot.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import difflib
22
import numpy as np
3+
import os
34
import subprocess
45
import sys
56
from pathlib import Path
@@ -367,3 +368,26 @@ def test_set_current_axes_on_subfigure():
367368
assert plt.gca() != ax
368369
plt.sca(ax)
369370
assert plt.gca() == ax
371+
372+
373+
def test_pylab_integration():
374+
pytest.importorskip("IPython")
375+
subprocess.run(
376+
[
377+
sys.executable,
378+
"-m",
379+
"IPython",
380+
"--pylab",
381+
"-c",
382+
";".join((
383+
"import matplotlib.pyplot as plt",
384+
"assert plt._REPL_DISPLAYHOOK == plt._ReplDisplayHook.IPYTHON",
385+
)),
386+
],
387+
env={**os.environ, "SOURCE_DATE_EPOCH": "0"},
388+
timeout=5,
389+
check=True,
390+
stdout=subprocess.PIPE,
391+
stderr=subprocess.PIPE,
392+
universal_newlines=True,
393+
)

0 commit comments

Comments
 (0)