From 175d52c8a9e5c265a6ff516fa67af0576136254d 2023-08-28 12:26:47 From: Matthias Bussonnier Date: 2023-08-28 12:26:47 Subject: [PATCH] Fix/mpl integration (#14128) - Closes https://github.com/matplotlib/matplotlib/issues/17454 - fixes qt5/qt6 confusion --- diff --git a/IPython/core/pylabtools.py b/IPython/core/pylabtools.py index deadf03..e5715a9 100644 --- a/IPython/core/pylabtools.py +++ b/IPython/core/pylabtools.py @@ -23,7 +23,7 @@ backends = { "qt4": "Qt4Agg", "qt5": "Qt5Agg", "qt6": "QtAgg", - "qt": "Qt5Agg", + "qt": "QtAgg", "osx": "MacOSX", "nbagg": "nbAgg", "webagg": "WebAgg", @@ -53,8 +53,8 @@ backend2gui["CocoaAgg"] = "osx" # supports either Qt5 or Qt6 and the IPython qt event loop support Qt4, Qt5, # and Qt6. backend2gui["QtAgg"] = "qt" -backend2gui["Qt4Agg"] = "qt" -backend2gui["Qt5Agg"] = "qt" +backend2gui["Qt4Agg"] = "qt4" +backend2gui["Qt5Agg"] = "qt5" # And some backends that don't need GUI integration del backend2gui["nbAgg"] @@ -208,10 +208,12 @@ def mpl_runner(safe_execfile): #print '*** Matplotlib runner ***' # dbg # turn off rendering until end of script - is_interactive = matplotlib.rcParams['interactive'] - matplotlib.interactive(False) - safe_execfile(fname,*where,**kw) - matplotlib.interactive(is_interactive) + with matplotlib.rc_context({"interactive": False}): + safe_execfile(fname, *where, **kw) + + if matplotlib.is_interactive(): + plt.show() + # make rendering call now, if the user tried to do it if plt.draw_if_interactive.called: plt.draw() @@ -317,9 +319,15 @@ def find_gui_and_backend(gui=None, gui_select=None): import matplotlib + has_unified_qt_backend = getattr(matplotlib, "__version_info__", (0, 0)) >= (3, 5) + + backends_ = dict(backends) + if not has_unified_qt_backend: + backends_["qt"] = "qt5agg" + if gui and gui != 'auto': # select backend based on requested gui - backend = backends[gui] + backend = backends_[gui] if gui == 'agg': gui = None else: @@ -336,7 +344,7 @@ def find_gui_and_backend(gui=None, gui_select=None): # ones allowed. if gui_select and gui != gui_select: gui = gui_select - backend = backends[gui] + backend = backends_[gui] return gui, backend diff --git a/IPython/core/tests/test_pylabtools.py b/IPython/core/tests/test_pylabtools.py index dd1a0ff..a06ad48 100644 --- a/IPython/core/tests/test_pylabtools.py +++ b/IPython/core/tests/test_pylabtools.py @@ -173,8 +173,8 @@ class TestPylabSwitch(object): # Save rcParams since they get modified self._saved_rcParams = matplotlib.rcParams self._saved_rcParamsOrig = matplotlib.rcParamsOrig - matplotlib.rcParams = dict(backend='Qt4Agg') - matplotlib.rcParamsOrig = dict(backend='Qt4Agg') + matplotlib.rcParams = dict(backend="QtAgg") + matplotlib.rcParamsOrig = dict(backend="QtAgg") # Mock out functions self._save_am = pt.activate_matplotlib