Skip to content

Commit ad0b2f9

Browse files
committed
Allow swapping the underlying rendering backend.
This patch implements backend_inline.set_rendering_backend, such that `set_rendering_backend("foo")` behaves as if calling `matplotlib.use("foo")`, which allows selecting an alternative rendering backend. In matplotlib itself, the only relevant alternative rendering backend is cairo, which is generally less feature-ful than agg, but there are also third-party backends, such as mplcairo (`set_rendering_backend("module://mplcairo.base")`) which provide e.g. improved text rendering for complex scripts (Arabic, Hebrew) and different compositing options.
1 parent 30bf01b commit ad0b2f9

File tree

1 file changed

+26
-9
lines changed

1 file changed

+26
-9
lines changed

matplotlib_inline/backend_inline.py

Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,43 @@
33
# Copyright (c) IPython Development Team.
44
# Distributed under the terms of the BSD 3-Clause License.
55

6+
import importlib
7+
68
import matplotlib
79
from IPython.core.getipython import get_ipython
810
from IPython.core.interactiveshell import InteractiveShell
911
from IPython.core.pylabtools import select_figure_formats
1012
from IPython.display import display
1113
from matplotlib import colors
1214
from matplotlib._pylab_helpers import Gcf
13-
from matplotlib.backends import backend_agg
14-
from matplotlib.backends.backend_agg import FigureCanvasAgg
1515
from matplotlib.figure import Figure
1616

1717
from .config import InlineBackend
1818

1919

20+
def set_rendering_backend(backend_name):
21+
"""
22+
Set the rendering backend.
23+
24+
Parameters
25+
----------
26+
backend_name : str
27+
A backend name, as would be passed to ``matplotlib.use()``. The
28+
backend should be non-interactive.
29+
"""
30+
global _backend_module, FigureCanvas
31+
_backend_module = importlib.import_module(
32+
backend_name[9:] if backend_name.startswith("module://")
33+
else f"matplotlib.backends.backend_{backend_name.lower()}")
34+
# Changes to matplotlib in version 1.2 requires a mpl backend to supply a
35+
# FigureCanvas. See https://github.com/matplotlib/matplotlib/pull/1125
36+
FigureCanvas = _backend_module.FigureCanvas
37+
38+
39+
_backend_module = FigureCanvas = None # Will be set by the call below.
40+
set_rendering_backend("agg") # The default rendering backend.
41+
42+
2043
def new_figure_manager(num, *args, FigureClass=Figure, **kwargs):
2144
"""
2245
Return a new figure manager for a new figure instance.
@@ -32,7 +55,7 @@ def new_figure_manager_given_figure(num, figure):
3255
3356
This function is part of the API expected by Matplotlib backends.
3457
"""
35-
manager = backend_agg.new_figure_manager_given_figure(num, figure)
58+
manager = _backend_module.new_figure_manager_given_figure(num, figure)
3659

3760
# Hack: matplotlib FigureManager objects in interacive backends (at least
3861
# in some of them) monkeypatch the figure object and add a .show() method
@@ -152,12 +175,6 @@ def flush_figures():
152175
show._draw_called = False
153176

154177

155-
# Changes to matplotlib in version 1.2 requires a mpl backend to supply a default
156-
# figurecanvas. This is set here to a Agg canvas
157-
# See https://github.com/matplotlib/matplotlib/pull/1125
158-
FigureCanvas = FigureCanvasAgg
159-
160-
161178
def configure_inline_support(shell, backend):
162179
"""Configure an IPython shell object for matplotlib use.
163180

0 commit comments

Comments
 (0)