summaryrefslogtreecommitdiffstats
path: root/contrib/python/matplotlib-inline/matplotlib_inline
diff options
context:
space:
mode:
Diffstat (limited to 'contrib/python/matplotlib-inline/matplotlib_inline')
-rw-r--r--contrib/python/matplotlib-inline/matplotlib_inline/__init__.py8
-rw-r--r--contrib/python/matplotlib-inline/matplotlib_inline/backend_inline.py85
-rw-r--r--contrib/python/matplotlib-inline/matplotlib_inline/config.py41
-rw-r--r--contrib/python/matplotlib-inline/matplotlib_inline/py.typed0
4 files changed, 87 insertions, 47 deletions
diff --git a/contrib/python/matplotlib-inline/matplotlib_inline/__init__.py b/contrib/python/matplotlib-inline/matplotlib_inline/__init__.py
index dce942f8991..d275175ecd9 100644
--- a/contrib/python/matplotlib-inline/matplotlib_inline/__init__.py
+++ b/contrib/python/matplotlib-inline/matplotlib_inline/__init__.py
@@ -1,2 +1,8 @@
from . import backend_inline, config # noqa
-__version__ = "0.1.7" # noqa
+
+__version__ = "0.2.1"
+
+# we can't ''.join(...) otherwise finding the version number at build time requires
+# import which introduces IPython and matplotlib at build time, and thus circular
+# dependencies.
+version_info = tuple(int(s) for s in __version__.split(".")[:3])
diff --git a/contrib/python/matplotlib-inline/matplotlib_inline/backend_inline.py b/contrib/python/matplotlib-inline/matplotlib_inline/backend_inline.py
index 05554b0d223..a798c562a7f 100644
--- a/contrib/python/matplotlib-inline/matplotlib_inline/backend_inline.py
+++ b/contrib/python/matplotlib-inline/matplotlib_inline/backend_inline.py
@@ -4,17 +4,16 @@
# Distributed under the terms of the BSD 3-Clause License.
import matplotlib
+from IPython.core.getipython import get_ipython
+from IPython.core.interactiveshell import InteractiveShell
+from IPython.core.pylabtools import select_figure_formats
+from IPython.display import display
from matplotlib import colors
+from matplotlib._pylab_helpers import Gcf
from matplotlib.backends import backend_agg
from matplotlib.backends.backend_agg import FigureCanvasAgg
-from matplotlib._pylab_helpers import Gcf
from matplotlib.figure import Figure
-from IPython.core.interactiveshell import InteractiveShell
-from IPython.core.getipython import get_ipython
-from IPython.core.pylabtools import select_figure_formats
-from IPython.display import display
-
from .config import InlineBackend
@@ -43,10 +42,11 @@ def new_figure_manager_given_figure(num, figure):
# https://github.com/ipython/ipython/issues/1612
# https://github.com/matplotlib/matplotlib/issues/835
- if not hasattr(figure, 'show'):
+ if not hasattr(figure, "show"):
# Queue up `figure` for display
figure.show = lambda *a: display(
- figure, metadata=_fetch_figure_metadata(figure))
+ figure, metadata=_fetch_figure_metadata(figure)
+ )
# If matplotlib was manually set to non-interactive mode, this function
# should be a no-op (otherwise we'll generate duplicate plots, since a user
@@ -89,20 +89,20 @@ def show(close=None, block=None):
for figure_manager in Gcf.get_all_fig_managers():
display(
figure_manager.canvas.figure,
- metadata=_fetch_figure_metadata(figure_manager.canvas.figure)
+ metadata=_fetch_figure_metadata(figure_manager.canvas.figure),
)
finally:
show._to_draw = []
# only call close('all') if any to close
# close triggers gc.collect, which can be slow
if close and Gcf.get_all_fig_managers():
- matplotlib.pyplot.close('all')
+ matplotlib.pyplot.close("all")
# This flag will be reset by draw_if_interactive when called
-show._draw_called = False
+show._draw_called = False # type: ignore[attr-defined]
# list of figures to draw when flush_figures is called
-show._to_draw = []
+show._to_draw = [] # type: ignore[attr-defined]
def flush_figures():
@@ -176,8 +176,8 @@ def configure_inline_support(shell, backend):
if cfg not in shell.configurables:
shell.configurables.append(cfg)
- if backend in ('inline', 'module://matplotlib_inline.backend_inline'):
- shell.events.register('post_execute', flush_figures)
+ if backend in ("inline", "module://matplotlib_inline.backend_inline"):
+ shell.events.register("post_execute", flush_figures)
# Save rcParams that will be overwrittern
shell._saved_rcParams = {}
@@ -188,10 +188,10 @@ def configure_inline_support(shell, backend):
new_backend_name = "inline"
else:
try:
- shell.events.unregister('post_execute', flush_figures)
+ shell.events.unregister("post_execute", flush_figures)
except ValueError:
pass
- if hasattr(shell, '_saved_rcParams'):
+ if hasattr(shell, "_saved_rcParams"):
matplotlib.rcParams.update(shell._saved_rcParams)
del shell._saved_rcParams
new_backend_name = "other"
@@ -208,11 +208,18 @@ def configure_inline_support(shell, backend):
def _enable_matplotlib_integration():
"""Enable extra IPython matplotlib integration when we are loaded as the matplotlib backend."""
- from matplotlib import get_backend
ip = get_ipython()
- backend = get_backend()
- if ip and backend in ('inline', 'module://matplotlib_inline.backend_inline'):
+
+ import matplotlib
+
+ if matplotlib.__version_info__ >= (3, 10):
+ backend = matplotlib.get_backend(auto_select=False)
+ else:
+ backend = matplotlib.rcParams._get("backend")
+
+ if ip and backend in ("inline", "module://matplotlib_inline.backend_inline"):
from IPython.core.pylabtools import activate_matplotlib
+
try:
activate_matplotlib(backend)
configure_inline_support(ip, backend)
@@ -221,8 +228,9 @@ def _enable_matplotlib_integration():
def configure_once(*args):
activate_matplotlib(backend)
configure_inline_support(ip, backend)
- ip.events.unregister('post_run_cell', configure_once)
- ip.events.register('post_run_cell', configure_once)
+ ip.events.unregister("post_run_cell", configure_once)
+
+ ip.events.register("post_run_cell", configure_once)
_enable_matplotlib_integration()
@@ -233,13 +241,17 @@ def _fetch_figure_metadata(fig):
# determine if a background is needed for legibility
if _is_transparent(fig.get_facecolor()):
# the background is transparent
- ticksLight = _is_light([label.get_color()
- for axes in fig.axes
- for axis in (axes.xaxis, axes.yaxis)
- for label in axis.get_ticklabels()])
+ ticksLight = _is_light(
+ [
+ label.get_color()
+ for axes in fig.axes
+ for axis in (axes.xaxis, axes.yaxis)
+ for label in axis.get_ticklabels()
+ ]
+ )
if ticksLight.size and (ticksLight == ticksLight[0]).all():
# there are one or more tick labels, all with the same lightness
- return {'needs_background': 'dark' if ticksLight[0] else 'light'}
+ return {"needs_background": "dark" if ticksLight[0] else "light"}
return None
@@ -249,13 +261,13 @@ def _is_light(color):
opposed to dark). Based on ITU BT.601 luminance formula (see
https://stackoverflow.com/a/596241)."""
rgbaArr = colors.to_rgba_array(color)
- return rgbaArr[:, :3].dot((.299, .587, .114)) > .5
+ return rgbaArr[:, :3].dot((0.299, 0.587, 0.114)) > 0.5
def _is_transparent(color):
"""Determine transparency from alpha."""
rgba = colors.to_rgba(color)
- return rgba[3] < .5
+ return rgba[3] < 0.5
def set_matplotlib_formats(*formats, **kwargs):
@@ -263,12 +275,21 @@ def set_matplotlib_formats(*formats, **kwargs):
For example, this enables PNG and JPEG output with a JPEG quality of 90%::
- In [1]: set_matplotlib_formats('png', 'jpeg', quality=90)
+ In [1]: set_matplotlib_formats('png', 'jpeg',
+ pil_kwargs={'quality': 90})
+
+ To set this in your notebook by `%config` magic::
+
+ In [1]: %config InlineBackend.figure_formats = {'png', 'jpeg'}
+ %config InlineBackend.print_figure_kwargs = \\
+ {'pil_kwargs': {'quality' : 90}}
To set this in your config files use the following::
c.InlineBackend.figure_formats = {'png', 'jpeg'}
- c.InlineBackend.print_figure_kwargs.update({'quality' : 90})
+ c.InlineBackend.print_figure_kwargs.update({
+ 'pil_kwargs': {'quality' : 90}
+ })
Parameters
----------
@@ -276,6 +297,10 @@ def set_matplotlib_formats(*formats, **kwargs):
One or more figure formats to enable: 'png', 'retina', 'jpeg', 'svg', 'pdf'.
**kwargs
Keyword args will be relayed to ``figure.canvas.print_figure``.
+
+ In addition, see the docstrings of `plt.savefig()`,
+ `matplotlib.figure.Figure.savefig()`, `PIL.Image.Image.save()` and
+ :ref:`Pillow Image file formats <handbook/image-file-formats>`.
"""
# build kwargs, starting with InlineBackend config
cfg = InlineBackend.instance()
diff --git a/contrib/python/matplotlib-inline/matplotlib_inline/config.py b/contrib/python/matplotlib-inline/matplotlib_inline/config.py
index 8718babad92..ce9be032fbd 100644
--- a/contrib/python/matplotlib-inline/matplotlib_inline/config.py
+++ b/contrib/python/matplotlib-inline/matplotlib_inline/config.py
@@ -6,10 +6,8 @@ This module does not import anything from matplotlib.
# Copyright (c) IPython Development Team.
# Distributed under the terms of the BSD 3-Clause License.
+from traitlets import Bool, Dict, Instance, Set, TraitError, Unicode
from traitlets.config.configurable import SingletonConfigurable
-from traitlets import (
- Dict, Instance, Set, Bool, TraitError, Unicode
-)
# Configurable for inline backend options
@@ -18,6 +16,7 @@ def pil_available():
out = False
try:
from PIL import Image # noqa
+
out = True
except ImportError:
pass
@@ -44,37 +43,45 @@ class InlineBackend(InlineBackendConfig):
the box, but third-party tools may use it to manage rc data. To change
personal defaults for matplotlib, use matplotlib's configuration
tools, or customize this class in your `ipython_config.py` file for
- IPython/Jupyter-specific usage.""").tag(config=True)
+ IPython/Jupyter-specific usage.""",
+ ).tag(config=True)
figure_formats = Set(
- {'png'},
+ {"png"},
help="""A set of figure formats to enable: 'png',
- 'retina', 'jpeg', 'svg', 'pdf'.""").tag(config=True)
+ 'retina', 'jpeg', 'svg', 'pdf'.""",
+ ).tag(config=True)
def _update_figure_formatters(self):
if self.shell is not None:
from IPython.core.pylabtools import select_figure_formats
- select_figure_formats(self.shell, self.figure_formats, **self.print_figure_kwargs)
+
+ select_figure_formats(
+ self.shell, self.figure_formats, **self.print_figure_kwargs
+ )
def _figure_formats_changed(self, name, old, new):
- if 'jpg' in new or 'jpeg' in new:
+ if "jpg" in new or "jpeg" in new:
if not pil_available():
raise TraitError("Requires PIL/Pillow for JPG figures")
self._update_figure_formatters()
- figure_format = Unicode(help="""The figure format to enable (deprecated
- use `figure_formats` instead)""").tag(config=True)
+ figure_format = Unicode(
+ help="""The figure format to enable (deprecated
+ use `figure_formats` instead)"""
+ ).tag(config=True)
def _figure_format_changed(self, name, old, new):
if new:
self.figure_formats = {new}
print_figure_kwargs = Dict(
- {'bbox_inches': 'tight'},
+ {"bbox_inches": "tight"},
help="""Extra kwargs to be passed to fig.canvas.print_figure.
- Logical examples include: bbox_inches, quality (for jpeg figures), etc.
- """
+ Logical examples include: bbox_inches, pil_kwargs, etc. In addition,
+ see the docstrings of `set_matplotlib_formats`.
+ """,
).tag(config=True)
_print_figure_kwargs_changed = _update_figure_formatters
@@ -94,7 +101,9 @@ class InlineBackend(InlineBackendConfig):
iterative editing of figures, and behaves most consistently with
other matplotlib backends, but figure barriers between cells must
be explicit.
- """).tag(config=True)
+ """,
+ ).tag(config=True)
- shell = Instance('IPython.core.interactiveshell.InteractiveShellABC',
- allow_none=True)
+ shell = Instance(
+ "IPython.core.interactiveshell.InteractiveShellABC", allow_none=True
+ )
diff --git a/contrib/python/matplotlib-inline/matplotlib_inline/py.typed b/contrib/python/matplotlib-inline/matplotlib_inline/py.typed
new file mode 100644
index 00000000000..e69de29bb2d
--- /dev/null
+++ b/contrib/python/matplotlib-inline/matplotlib_inline/py.typed