From 70e096805778cb79eb1729f5bb2f9ddebf85e001 Mon Sep 17 00:00:00 2001 From: Min RK Date: Wed, 13 Jul 2016 19:54:24 -0500 Subject: [PATCH 1/2] Make inline the default matplotlib backend uses MPLBACKEND env (matplotlib > 1.5). Trigger extra IPython inline setup when inline backend is loaded. We can't call the simpler `ip.enable_matplotlib()` here because this will be triggered during pyplot import, and enable_matplotlib() imports pyplot which isn't ready yet. --- ipykernel/kernelapp.py | 7 +++++++ ipykernel/pylab/backend_inline.py | 11 +++++++++++ 2 files changed, 18 insertions(+) diff --git a/ipykernel/kernelapp.py b/ipykernel/kernelapp.py index 729aa5f4c..70963578e 100644 --- a/ipykernel/kernelapp.py +++ b/ipykernel/kernelapp.py @@ -368,6 +368,13 @@ def init_kernel(self): def init_gui_pylab(self): """Enable GUI event loop integration, taking pylab into account.""" + # Register inline backend as default + # this is higher priority than matplotlibrc, + # but lower priority than anything else (mpl.use() for instance). + # This only affects matplotlib >= 1.5 + if not os.environ.get('MPLBACKEND'): + os.environ['MPLBACKEND'] = 'module://ipykernel.pylab.backend_inline' + # Provide a wrapper for :meth:`InteractiveShellApp.init_gui_pylab` # to ensure that any exception is printed straight to stderr. # Normally _showtraceback associates the reply with an execution, diff --git a/ipykernel/pylab/backend_inline.py b/ipykernel/pylab/backend_inline.py index 54f465ca3..d53b38607 100644 --- a/ipykernel/pylab/backend_inline.py +++ b/ipykernel/pylab/backend_inline.py @@ -143,3 +143,14 @@ def flush_figures(): # figurecanvas. This is set here to a Agg canvas # See https://github.com/matplotlib/matplotlib/pull/1125 FigureCanvas = FigureCanvasAgg + +def _enable_matplotlib_integration(): + """Enable extra IPython matplotlib integration when we are loaded as the matplotlib backend.""" + from matplotlib import get_backend + from IPython.core.pylabtools import configure_inline_support + ip = get_ipython() + backend = get_backend() + if ip and backend == 'module://%s' % __name__: + configure_inline_support(ip, backend) + +_enable_matplotlib_integration() From e4d555a8495d8e318cf56b9a77a97439642bdc38 Mon Sep 17 00:00:00 2001 From: Min RK Date: Tue, 26 Jul 2016 15:11:42 +0200 Subject: [PATCH 2/2] note MPLBACKEND in changelog --- docs/changelog.rst | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/docs/changelog.rst b/docs/changelog.rst index ed938d558..42a142e49 100644 --- a/docs/changelog.rst +++ b/docs/changelog.rst @@ -1,6 +1,21 @@ Changes in IPython kernel ========================= +4.4 +--- + +4.4.0 +***** + +`4.4.0 on GitHub `__ + +- Use `MPLBACKEND`_ environment variable to tell matplotlib >= 1.5 use use the inline backend by default. + This is only done if MPLBACKEND is not already set and no backend has been explicitly loaded, + so setting ``MPLBACKEND=Qt4Agg`` or calling ``%matplotlib notebook`` or ``matplotlib.use('Agg')`` + will take precedence. + +.. _MPLBACKEND: http://matplotlib.org/devel/coding_guide.html?highlight=mplbackend#developing-a-new-backend + 4.3 ---