diff --git a/lib/matplotlib/pyplot.py b/lib/matplotlib/pyplot.py index 2aedcb5f362a..e5efe37a52cd 100644 --- a/lib/matplotlib/pyplot.py +++ b/lib/matplotlib/pyplot.py @@ -206,12 +206,6 @@ def _get_backend_mod(): # will (re)import pyplot and then call switch_backend if we need to # resolve the auto sentinel) switch_backend(dict.__getitem__(rcParams, "backend")) - # Just to be safe. Interactive mode can be turned on without calling - # `plt.ion()` so register it again here. This is safe because multiple - # calls to `install_repl_displayhook` are no-ops and the registered - # function respects `mpl.is_interactive()` to determine if it should - # trigger a draw. - install_repl_displayhook() return _backend_mod @@ -302,6 +296,10 @@ class backend_mod(matplotlib.backend_bases._Backend): # See https://github.com/matplotlib/matplotlib/issues/6092 matplotlib.backends.backend = newbackend + # make sure the repl display hook is installed in case we become + # interactive + install_repl_displayhook() + def _warn_if_gui_out_of_main_thread(): if (_get_required_interactive_framework(_get_backend_mod()) diff --git a/lib/matplotlib/tests/test_pyplot.py b/lib/matplotlib/tests/test_pyplot.py index 0dcc0c765afb..6f9ddd5ccf21 100644 --- a/lib/matplotlib/tests/test_pyplot.py +++ b/lib/matplotlib/tests/test_pyplot.py @@ -1,5 +1,6 @@ import difflib import numpy as np +import os import subprocess import sys from pathlib import Path @@ -367,3 +368,26 @@ def test_set_current_axes_on_subfigure(): assert plt.gca() != ax plt.sca(ax) assert plt.gca() == ax + + +def test_pylab_integration(): + pytest.importorskip("IPython") + subprocess.run( + [ + sys.executable, + "-m", + "IPython", + "--pylab", + "-c", + ";".join(( + "import matplotlib.pyplot as plt", + "assert plt._REPL_DISPLAYHOOK == plt._ReplDisplayHook.IPYTHON", + )), + ], + env={**os.environ, "SOURCE_DATE_EPOCH": "0"}, + timeout=60, + check=True, + stdout=subprocess.PIPE, + stderr=subprocess.PIPE, + universal_newlines=True, + )