Skip to content

Commit 2b4a17c

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 fbf0ab8 commit 2b4a17c

File tree

1 file changed

+26
-9
lines changed

1 file changed

+26
-9
lines changed

matplotlib_inline/backend_inline.py

+26-9
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@
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 matplotlib import colors
8-
from matplotlib.backends import backend_agg
9-
from matplotlib.backends.backend_agg import FigureCanvasAgg
1010
from matplotlib._pylab_helpers import Gcf
1111
from matplotlib.figure import Figure
1212

@@ -18,6 +18,29 @@
1818
from .config import InlineBackend
1919

2020

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

3861
# Hack: matplotlib FigureManager objects in interacive backends (at least
3962
# 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)