-
-
Notifications
You must be signed in to change notification settings - Fork 8.1k
Description
Summary
I propose to move the Matplotlib backend mapping that is currently in IPython into Matplotlib, and extend it to support backends registering themselves via entry_points. This was originally discussed in #19482 and Tom summarised and proposed a solution in #19482 (comment).
Proposed fix
Background
IPython supports the use of the %matplotlib magic in two ways:
%matplotlib <backend_name>to use the named Matplotlib backend.%matplotlib --listto list available backends.
IPython contains hard-coded information about which backends exist, and which GUI interactive framework they support. The current information consists of backends that are built-into mpl itself (e.g. qt, notebook, webagg) and those in external libraries (ipympl and inline) which are Jupyter-based.
Problems
- This is unnecessarily difficult to maintain as new backends require changes in IPython.
- There is no support in IPython for backends specified using
"module://whatever.backend.module"
Proposal
- Keep the same
%matplotlibmagic functionality in IPython but defer responsibility for the backend naming and interactive framework identification to Matplotlib, with a version check of Matplotlib to fallback to the existing behaviour if necessary. - Add a backend registry to Matplotlib. Whether this is literally a
BackendRegistrysingleton or not is an implementation detail. - The registry will allow use of backends for both IPython and
matplotlib.usein three different categories:
a. Backends that are built into the Matplotlib code base.
b. Backends that can be dynamically loaded using"module://backend.name"such as MplCairo.
c. Backends that register themselves using anentry_point. Initially this will beipymplandmatplotlib-inline. - Need to handle error situations of multiple backends registering under the same name, or registering with the same name as a built-in backend.
- Matplotlib needs a new
list_backendsfunction for IPython’s%matplotlib --list. This will include all backends in items 3a and 3c above, as well as any“module://…”backends (3b) that have already been loaded. The latter is so that if you use%matplotlib module://…in IPython a subsequent%matplotlib –listwill include the backend you are using. - The
list_backendsfunction may as well be public within Matplotlib as it is a feature that is requested every so often. It will have to be clearly stated that this contains “backends that mpl is aware of” rather than an exhaustive list.
This isn’t the full set of functionality listed at #19482 (comment) as it does not include use of "m://…" form for example.
Changes will be required in the following projects:
- Matplotlib = new registry, looking up entry points, docs.
- Matplotlib-inline = add entry point.
- Ipympl = add entry point.
- IPython = call Matplotlib backend registry for backend info, docs.
Changes 1-3 will need to be published in releases before 4 can be merged and released.