"""
Abstract base classes define the primitives that renderers and
graphics contexts must implement to serve as a Matplotlib backend.
`RendererBase`
An abstract base class to handle drawing/rendering operations.
`FigureCanvasBase`
The abstraction layer that separates the `.Figure` from the backend
specific details like a user interface drawing area.
`GraphicsContextBase`
An abstract base class that provides color, line styles, etc.
`Event`
The base class for all of the Matplotlib event handling. Derived classes
such as `KeyEvent` and `MouseEvent` store the meta data like keys and
buttons pressed, x and y locations in pixel and `~.axes.Axes` coordinates.
`ShowBase`
The base class for the ``Show`` class of each interactive backend; the
'show' callable is then set to ``Show.__call__``.
`ToolContainerBase`
The base class for the Toolbar class of each interactive backend.
"""
from contextlib import contextmanager, suppress
from enum import Enum, IntEnum
import functools
import importlib
import inspect
import io
import logging
import os
import re
import sys
import time
import traceback
from weakref import WeakKeyDictionary
import numpy as np
import matplotlib as mpl
from matplotlib import (
backend_tools as tools, cbook, colors, textpath, tight_bbox,
transforms, widgets, get_backend, is_interactive, rcParams)
from matplotlib._pylab_helpers import Gcf
from matplotlib.backend_managers import ToolManager
from matplotlib.transforms import Affine2D
from matplotlib.path import Path
from matplotlib.cbook import _setattr_cm
_log = logging.getLogger(__name__)
_default_filetypes = {
'eps': 'Encapsulated Postscript',
'jpg': 'Joint Photographic Experts Group',
'jpeg': 'Joint Photographic Experts Group',
'pdf': 'Portable Document Format',
'pgf': 'PGF code for LaTeX',
'png': 'Portable Network Graphics',
'ps': 'Postscript',
'raw': 'Raw RGBA bitmap',
'rgba': 'Raw RGBA bitmap',
'svg': 'Scalable Vector Graphics',
'svgz': 'Scalable Vector Graphics',
'tif': 'Tagged Image File Format',
'tiff': 'Tagged Image File Format',
}
_default_backends = {
'eps': 'matplotlib.backends.backend_ps',
'jpg': 'matplotlib.backends.backend_agg',
'jpeg': 'matplotlib.backends.backend_agg',
'pdf': 'matplotlib.backends.backend_pdf',
'pgf': 'matplotlib.backends.backend_pgf',
'png': 'matplotlib.backends.backend_agg',
'ps': 'matplotlib.backends.backend_ps',
'raw': 'matplotlib.backends.backend_agg',
'rgba': 'matplotlib.backends.backend_agg',
'svg': 'matplotlib.backends.backend_svg',
'svgz': 'matplotlib.backends.backend_svg',
'tif': 'matplotlib.backends.backend_agg',
'tiff': 'matplotlib.backends.backend_agg',
}
[docs]def register_backend(format, backend, description=None):
"""
Register a backend for saving to a given file format.
Parameters
----------
format : str
File extension
backend : module string or canvas class
Backend for handling file output
description : str, default: ""
Description of the file type.
"""
if description is None:
description = ''
_default_backends[format] = backend
_default_filetypes[format] = description
[docs]def get_registered_canvas_class(format):
"""
Return the registered default canvas for given file format.
Handles deferred import of required backend.
"""
if format not in _default_backends:
return None
backend_class = _default_backends[format]
if isinstance(backend_class, str):
backend_class = importlib.import_module(backend_class).FigureCanvas
_default_backends[format] = backend_class
return backend_class
[docs]class RendererBase:
"""
An abstract base class to handle drawing/rendering operations.
The following methods must be implemented in the backend for full
functionality (though just implementing :meth:`draw_path` alone would
give a highly capable backend):
* :meth:`draw_path`
* :meth:`draw_image`
* :meth:`draw_gouraud_triangle`
The following methods *should* be implemented in the backend for
optimization reasons:
* :meth:`draw_text`
* :meth:`draw_markers`
* :meth:`draw_path_collection`
* :meth:`draw_quad_mesh`
"""
def __init__(self):
super().__init__()
self._texmanager = None
self._text2path = textpath.TextToPath()
[docs] def open_group(self, s, gid=None):
"""
Open a grouping element with label *s* and *gid* (if set) as id.
Only used by the SVG renderer.
"""
[docs] def close_group(self, s):
"""
Close a grouping element with label *s*.
Only used by the SVG renderer.
"""
[docs] def draw_path(self, gc, path, transform, rgbFace=None):
"""Draw a `~.path.Path` instance using the given affine transform."""
raise NotImplementedError
[docs] def draw_markers(self, gc, marker_path, marker_trans, path,
trans, rgbFace=None):
"""
Draw a marker at each of the vertices in path.
This includes all vertices, including control points on curves.
To avoid that behavior, those vertices should be removed before
calling this function.
This provides a fallback implementation of draw_markers that
makes multiple calls to :meth:`draw_path`. Some backends may
want to override this method in order to draw the marker only
once and reuse it multiple times.
Parameters
----------
gc : `.GraphicsContextBase`
The graphics context.
marker_trans : `matplotlib.transforms.Transform`
An affine transform applied to the marker.
trans : `matplotlib.transforms.Transform`
An affine transform applied to the path.
"""
for vertices, codes in path.iter_segments(trans, simplify=False):
if len(vertices):
x, y = vertices[-2:]
self.draw_path(gc, marker_path,
marker_trans +
transforms.Affine2D().translate(x, y),
rgbFace)
[docs] def draw_path_collection(self, gc, master_transform, paths, all_transforms,
offsets, offsetTrans, facecolors, edgecolors,
linewidths, linestyles, antialiaseds, urls,
offset_position):
"""
Draw a collection of paths selecting drawing properties from
the lists *facecolors*, *edgecolors*, *linewidths*,
*linestyles* and *antialiaseds*. *offsets* is a list of
offsets to apply to each of the paths. The offsets in
*offsets* are first transformed by *offsetTrans* before being
applied.
*offset_position* may be either "screen" or "data" depending on the
space that the offsets are in; "data" is deprecated.
This provides a fallback implementation of
:meth:`draw_path_collection` that makes multiple calls to
:meth:`draw_path`. Some backends may want to override this in
order to render each set of path data only once, and then
reference that path multiple times with the different offsets,
colors, styles etc. The generator methods
:meth:`_iter_collection_raw_paths` and
:meth:`_iter_collection` are provided to help with (and
standardize) the implementation across backends. It is highly
recommended to use those generators, so that changes to the
behavior of :meth:`draw_path_collection` can be made globally.
"""
path_ids = self._iter_collection_raw_paths(master_transform,
paths, all_transforms)
for xo, yo, path_id, gc0, rgbFace in self._iter_collection(
gc, master_transform, all_transforms, list(path_ids), offsets,
offsetTrans, facecolors, edgecolors, linewidths, linestyles,
antialiaseds, urls, offset_position):
path, transform = path_id
# Only apply another translation if we have an offset, else we
# resuse the inital transform.
if xo != 0 or yo != 0:
# The transformation can be used by multiple paths. Since
# translate is a inplace operation, we need to copy the
# transformation by .frozen() before applying the translation.
transform = transform.frozen()
transform.translate(xo, yo)
self.draw_path(gc0, path, transform, rgbFace)
[docs] def draw_quad_mesh(self, gc, master_transform, meshWidth, meshHeight,
coordinates, offsets, offsetTrans, facecolors,
antialiased, edgecolors):
"""
Fallback implementation of :meth:`draw_quad_mesh` that generates paths
and then calls :meth:`draw_path_collection`.
"""
from matplotlib.collections import QuadMesh
paths = QuadMesh.convert_mesh_to_paths(
meshWidth, meshHeight, coordinates)
if edgecolors is None:
edgecolors = facecolors
linewidths = np.array([gc.get_linewidth()], float)
return self.draw_path_collection(
gc, master_transform, paths, [], offsets, offsetTrans, facecolors,
edgecolors, linewidths, [], [antialiased], [None], 'screen')
[docs] def draw_gouraud_triangle(self, gc, points, colors, transform):
"""
Draw a Gouraud-shaded triangle.
Parameters
----------
gc : `.GraphicsContextBase`
The graphics context.
points : array-like, shape=(3, 2)
Array of (x, y) points for the triangle.
colors : array-like, shape=(3, 4)
RGBA colors for each point of the triangle.
transform : `matplotlib.transforms.Transform`
An affine transform to apply to the points.
"""
raise NotImplementedError
[docs] def draw_gouraud_triangles(self, gc, triangles_array, colors_array,
transform):
"""
Draw a series of Gouraud triangles.
Parameters
----------
points : array-like, shape=(N, 3, 2)
Array of *N* (x, y) points for the triangles.
colors : array-like, shape=(N, 3, 4)
Array of *N* RGBA colors for each point of the triangles.
transform : `matplotlib.transforms.Transform`
An affine transform to apply to the points.
"""
transform = transform.frozen()
for tri, col in zip(triangles_array, colors_array):
self.draw_gouraud_triangle(gc, tri, col, transform)
def _iter_collection_raw_paths(self, master_transform, paths,
all_transforms):
"""
Helper method (along with :meth:`_iter_collection`) to implement
:meth:`draw_path_collection` in a space-efficient manner.
This method yields all of the base path/transform
combinations, given a master transform, a list of paths and
list of transforms.
The arguments should be exactly what is passed in to
:meth:`draw_path_collection`.
The backend should take each yielded path and transform and
create an object that can be referenced (reused) later.
"""
Npaths = len(paths)
Ntransforms = len(all_transforms)
N = max(Npaths, Ntransforms)
if Npaths == 0:
return
transform = transforms.IdentityTransform()
for i in range(N):
path = paths[i % Npaths]
if Ntransforms:
transform = Affine2D(all_transforms[i % Ntransforms])
yield path, transform + master_transform
def _iter_collection_uses_per_path(self, paths, all_transforms,
offsets, facecolors, edgecolors):
"""
Compute how many times each raw path object returned by
_iter_collection_raw_paths would be used when calling
_iter_collection. This is intended for the backend to decide
on the tradeoff between using the paths in-line and storing
them once and reusing. Rounds up in case the number of uses
is not the same for every path.
"""
Npaths = len(paths)
if Npaths == 0 or len(facecolors) == len(edgecolors) == 0:
return 0
Npath_ids = max(Npaths, len(all_transforms))
N = max(Npath_ids, len(offsets))
return (N + Npath_ids - 1) // Npath_ids
def _iter_collection(self, gc, master_transform, all_transforms,
path_ids, offsets, offsetTrans, facecolors,
edgecolors, linewidths, linestyles,
antialiaseds, urls, offset_position):
"""
Helper method (along with :meth:`_iter_collection_raw_paths`) to
implement :meth:`draw_path_collection` in a space-efficient manner.
This method yields all of the path, offset and graphics
context combinations to draw the path collection. The caller
should already have looped over the results of
:meth:`_iter_collection_raw_paths` to draw this collection.
The arguments should be the same as that passed into
:meth:`draw_path_collection`, with the exception of
*path_ids*, which is a list of arbitrary objects that the
backend will use to reference one of the paths created in the
:meth:`_iter_collection_raw_paths` stage.
Each yielded result is of the form::
xo, yo, path_id, gc, rgbFace
where *xo*, *yo* is an offset; *path_id* is one of the elements of
*path_ids*; *gc* is a graphics context and *rgbFace* is a color to
use for filling the path.
"""
Ntransforms = len(all_transforms)
Npaths = len(path_ids)
Noffsets = len(offsets)
N = max(Npaths, Noffsets)
Nfacecolors = len(facecolors)
Nedgecolors = len(edgecolors)
Nlinewidths = len(linewidths)
Nlinestyles = len(linestyles)
Naa = len(antialiaseds)
Nurls = len(urls)
if offset_position == "data":
cbook.warn_deprecated(
"3.3", message="Support for offset_position='data' is "
"deprecated since %(since)s and will be removed %(removal)s.")
if (Nfacecolors == 0 and Nedgecolors == 0) or Npaths == 0:
return
if Noffsets:
toffsets = offsetTrans.transform(offsets)
gc0 = self.new_gc()
gc0.copy_properties(gc)
if Nfacecolors == 0:
rgbFace = None
if Nedgecolors == 0:
gc0.set_linewidth(0.0)
xo, yo = 0, 0
for i in range(N):
path_id = path_ids[i % Npaths]
if Noffsets:
xo, yo = toffsets[i % Noffsets]
if offset_position == 'data':
if Ntransforms:
transform = (
Affine2D(all_transforms[i % Ntransforms]) +
master_transform)
else:
transform = master_transform
(xo, yo), (xp, yp) = transform.transform(
[(xo, yo), (0, 0)])
xo = -(xp - xo)
yo = -(yp - yo)
if not (np.isfinite(xo) and np.isfinite(yo)):
continue
if Nfacecolors:
rgbFace = facecolors[i % Nfacecolors]
if Nedgecolors:
if Nlinewidths:
gc0.set_linewidth(linewidths[i % Nlinewidths])
if Nlinestyles:
gc0.set_dashes(*linestyles[i % Nlinestyles])
fg = edgecolors[i % Nedgecolors]
if len(fg) == 4:
if fg[3] == 0.0:
gc0.set_linewidth(0)
else:
gc0.set_foreground(fg)
else:
gc0.set_foreground(fg)
if rgbFace is not None and len(rgbFace) == 4:
if rgbFace[3] == 0:
rgbFace = None
gc0.set_antialiased(antialiaseds[i % Naa])
if Nurls:
gc0.set_url(urls[i % Nurls])
yield xo, yo, path_id, gc0, rgbFace
gc0.restore()
[docs] def get_image_magnification(self):
"""
Get the factor by which to magnify images passed to :meth:`draw_image`.
Allows a backend to have images at a different resolution to other
artists.
"""
return 1.0
[docs] def draw_image(self, gc, x, y, im, transform=None):
"""
Draw an RGBA image.
Parameters
----------
gc : `.GraphicsContextBase`
A graphics context with clipping information.
x : scalar
The distance in physical units (i.e., dots or pixels) from the left
hand side of the canvas.
y : scalar
The distance in physical units (i.e., dots or pixels) from the
bottom side of the canvas.
im : array-like, shape=(N, M, 4), dtype=np.uint8
An array of RGBA pixels.
transform : `matplotlib.transforms.Affine2DBase`
If and only if the concrete backend is written such that
:meth:`option_scale_image` returns ``True``, an affine
transformation (i.e., an `.Affine2DBase`) *may* be passed to
:meth:`draw_image`. The translation vector of the transformation
is given in physical units (i.e., dots or pixels). Note that
the transformation does not override *x* and *y*, and has to be
applied *before* translating the result by *x* and *y* (this can
be accomplished by adding *x* and *y* to the translation vector
defined by *transform*).
"""
raise NotImplementedError
[docs] def option_image_nocomposite(self):
"""
Return whether image composition by Matplotlib should be skipped.
Raster backends should usually return False (letting the C-level
rasterizer take care of image composition); vector backends should
usually return ``not rcParams["image.composite_image"]``.
"""
return False
[docs] def option_scale_image(self):
"""
Return whether arbitrary affine transformations in :meth:`draw_image`
are supported (True for most vector backends).
"""
return False
[docs] @cbook._delete_parameter("3.3", "ismath")
def draw_tex(self, gc, x, y, s, prop, angle, ismath='TeX!', mtext=None):
"""
"""
self._draw_text_as_path(gc, x, y, s, prop, angle, ismath="TeX")
[docs] def draw_text(self, gc, x, y, s, prop, angle, ismath=False, mtext=None):
"""
Draw the text instance.
Parameters
----------
gc : `.GraphicsContextBase`
The graphics context.
x : float
The x location of the text in display coords.
y : float
The y location of the text baseline in display coords.
s : str
The text string.
prop : `matplotlib.font_manager.FontProperties`
The font properties.
angle : float
The rotation angle in degrees anti-clockwise.
mtext : `matplotlib.text.Text`
The original text object to be rendered.
Notes
-----
**Note for backend implementers:**
When you are trying to determine if you have gotten your bounding box
right (which is what enables the text layout/alignment to work
properly), it helps to change the line in text.py::
if 0: bbox_artist(self, renderer)
to if 1, and then the actual bounding box will be plotted along with
your text.
"""
self._draw_text_as_path(gc, x, y, s, prop, angle, ismath)
def _get_text_path_transform(self, x, y, s, prop, angle, ismath):
"""
Return the text path and transform.
Parameters
----------
prop : `matplotlib.font_manager.FontProperties`
The font property.
s : str
The text to be converted.
ismath : bool or "TeX"
If True, use mathtext parser. If "TeX", use *usetex* mode.
"""
text2path = self._text2path
fontsize = self.points_to_pixels(prop.get_size_in_points())
verts, codes = text2path.get_text_path(prop, s, ismath=ismath)
path = Path(verts, codes)
angle = np.deg2rad(angle)
if self.flipy():
width, height = self.get_canvas_width_height()
transform = (Affine2D()
.scale(fontsize / text2path.FONT_SCALE)
.rotate(angle)
.translate(x, height - y))
else:
transform = (Affine2D()
.scale(fontsize / text2path.FONT_SCALE)
.rotate(angle)
.translate(x, y))
return path, transform
def _draw_text_as_path(self, gc, x, y, s, prop, angle, ismath):
"""
Draw the text by converting them to paths using textpath module.
Parameters
----------
prop : `matplotlib.font_manager.FontProperties`
The font property.
s : str
The text to be converted.
usetex : bool
Whether to use usetex mode.
ismath : bool or "TeX"
If True, use mathtext parser. If "TeX", use *usetex* mode.
"""
path, transform = self._get_text_path_transform(
x, y, s, prop, angle, ismath)
color = gc.get_rgb()
gc.set_linewidth(0.0)
self.draw_path(gc, path, transform, rgbFace=color)
[docs] def get_text_width_height_descent(self, s, prop, ismath):
"""
Get the width, height, and descent (offset from the bottom
to the baseline), in display coords, of the string *s* with
`.FontProperties` *prop*.
"""
if ismath == 'TeX':
# todo: handle props
texmanager = self._text2path.get_texmanager()
fontsize = prop.get_size_in_points()
w, h, d = texmanager.get_text_width_height_descent(
s, fontsize, renderer=self)
return w, h, d
dpi = self.points_to_pixels(72)
if ismath:
dims = self._text2path.mathtext_parser.parse(s, dpi, prop)
return dims[0:3] # return width, height, descent
flags = self._text2path._get_hinting_flag()
font = self._text2path._get_font(prop)
size = prop.get_size_in_points()
font.set_size(size, dpi)
# the width and height of unrotated string
font.set_text(s, 0.0, flags=flags)
w, h = font.get_width_height()
d = font.get_descent()
w /= 64.0 # convert from subpixels
h /= 64.0
d /= 64.0
return w, h, d
[docs] def flipy(self):
"""
Return whether y values increase from top to bottom.
Note that this only affects drawing of texts and images.
"""
return True
[docs] def get_canvas_width_height(self):
"""Return the canvas width and height in display coords."""
return 1, 1
[docs] def get_texmanager(self):
"""Return the `.TexManager` instance."""
if self._texmanager is None:
from matplotlib.texmanager import TexManager
self._texmanager = TexManager()
return self._texmanager
[docs] def new_gc(self):
"""Return an instance of a `.GraphicsContextBase`."""
return GraphicsContextBase()
[docs] def points_to_pixels(self, points):
"""
Convert points to display units.
You need to override this function (unless your backend
doesn't have a dpi, e.g., postscript or svg). Some imaging
systems assume some value for pixels per inch::
points to pixels = points * pixels_per_inch/72 * dpi/72
Parameters
----------
points : float or array-like
a float or a numpy array of float
Returns
-------
Points converted to pixels
"""
return points
[docs] def start_rasterizing(self):
"""
Switch to the raster renderer.
Used by `.MixedModeRenderer`.
"""
[docs] def stop_rasterizing(self):
"""
Switch back to the vector renderer and draw the contents of the raster
renderer as an image on the vector renderer.
Used by `.MixedModeRenderer`.
"""
[docs] def start_filter(self):
"""
Switch to a temporary renderer for image filtering effects.
Currently only supported by the agg renderer.
"""
[docs] def stop_filter(self, filter_func):
"""
Switch back to the original renderer. The contents of the temporary
renderer is processed with the *filter_func* and is drawn on the
original renderer as an image.
Currently only supported by the agg renderer.
"""
def _draw_disabled(self):
"""
Context manager to temporary disable drawing.
This is used for getting the drawn size of Artists. This lets us
run the draw process to update any Python state but does not pay the
cost of the draw_XYZ calls on the canvas.
"""
no_ops = {
meth_name: lambda *args, **kwargs: None
for meth_name in dir(RendererBase)
if (meth_name.startswith("draw_")
or meth_name in ["open_group", "close_group"])
}
return _setattr_cm(self, **no_ops)
[docs]class GraphicsContextBase:
"""An abstract base class that provides color, line styles, etc."""
def __init__(self):
self._alpha = 1.0
self._forced_alpha = False # if True, _alpha overrides A from RGBA
self._antialiased = 1 # use 0, 1 not True, False for extension code
self._capstyle = 'butt'
self._cliprect = None
self._clippath = None
self._dashes = 0, None
self._joinstyle = 'round'
self._linestyle = 'solid'
self._linewidth = 1
self._rgb = (0.0, 0.0, 0.0, 1.0)
self._hatch = None
self._hatch_color = colors.to_rgba(rcParams['hatch.color'])
self._hatch_linewidth = rcParams['hatch.linewidth']
self._url = None
self._gid = None
self._snap = None
self._sketch = None
[docs] def copy_properties(self, gc):
"""Copy properties from *gc* to self."""
self._alpha = gc._alpha
self._forced_alpha = gc._forced_alpha
self._antialiased = gc._antialiased
self._capstyle = gc._capstyle
self._cliprect = gc._cliprect
self._clippath = gc._clippath
self._dashes = gc._dashes
self._joinstyle = gc._joinstyle
self._linestyle = gc._linestyle
self._linewidth = gc._linewidth
self._rgb = gc._rgb
self._hatch = gc._hatch
self._hatch_color = gc._hatch_color
self._hatch_linewidth = gc._hatch_linewidth
self._url = gc._url
self._gid = gc._gid
self._snap = gc._snap
self._sketch = gc._sketch
[docs] def restore(self):
"""
Restore the graphics context from the stack - needed only
for backends that save graphics contexts on a stack.
"""
[docs] def get_alpha(self):
"""
Return the alpha value used for blending - not supported on all
backends.
"""
return self._alpha
[docs] def get_antialiased(self):
"""Return whether the object should try to do antialiased rendering."""
return self._antialiased
[docs] def get_capstyle(self):
"""
Return the capstyle as a string in ('butt', 'round', 'projecting').
"""
return self._capstyle
[docs] def get_clip_rectangle(self):
"""
Return the clip rectangle as a `~matplotlib.transforms.Bbox` instance.
"""
return self._cliprect
[docs] def get_clip_path(self):
"""
Return the clip path in the form (path, transform), where path
is a `~.path.Path` instance, and transform is
an affine transform to apply to the path before clipping.
"""
if self._clippath is not None:
return self._clippath.get_transformed_path_and_affine()
return None, None
[docs] def get_dashes(self):
"""
Return the dash style as an (offset, dash-list) pair.
The dash list is a even-length list that gives the ink on, ink off in
points. See p. 107 of to PostScript `blue book`_ for more info.
Default value is (None, None).
.. _blue book: https://www-cdf.fnal.gov/offline/PostScript/BLUEBOOK.PDF
"""
return self._dashes
[docs] def get_forced_alpha(self):
"""
Return whether the value given by get_alpha() should be used to
override any other alpha-channel values.
"""
return self._forced_alpha
[docs] def get_joinstyle(self):
"""Return the line join style as one of ('miter', 'round', 'bevel')."""
return self._joinstyle
[docs] def get_linewidth(self):
"""Return the line width in points."""
return self._linewidth
[docs] def get_rgb(self):
"""Return a tuple of three or four floats from 0-1."""
return self._rgb
[docs] def get_url(self):
"""Return a url if one is set, None otherwise."""
return self._url
[docs] def get_gid(self):
"""Return the object identifier if one is set, None otherwise."""
return self._gid
[docs] def get_snap(self):
"""
Return the snap setting, which can be:
* True: snap vertices to the nearest pixel center
* False: leave vertices as-is
* None: (auto) If the path contains only rectilinear line segments,
round to the nearest pixel center
"""
return self._snap
[docs] def set_alpha(self, alpha):
"""
Set the alpha value used for blending - not supported on all backends.
If ``alpha=None`` (the default), the alpha components of the
foreground and fill colors will be used to set their respective
transparencies (where applicable); otherwise, ``alpha`` will override
them.
"""
if alpha is not None:
self._alpha = alpha
self._forced_alpha = True
else:
self._alpha = 1.0
self._forced_alpha = False
self.set_foreground(self._rgb, isRGBA=True)
[docs] def set_antialiased(self, b):
"""Set whether object should be drawn with antialiased rendering."""
# Use ints to make life easier on extension code trying to read the gc.
self._antialiased = int(bool(b))
[docs] def set_capstyle(self, cs):
"""Set the capstyle to be one of ('butt', 'round', 'projecting')."""
cbook._check_in_list(['butt', 'round', 'projecting'], cs=cs)
self._capstyle = cs
[docs] def set_clip_rectangle(self, rectangle):
"""
Set the clip rectangle with sequence (left, bottom, width, height)
"""
self._cliprect = rectangle
[docs] def set_clip_path(self, path):
"""
Set the clip path and transformation.
Parameters
----------
path : `~matplotlib.transforms.TransformedPath` or None
"""
cbook._check_isinstance((transforms.TransformedPath, None), path=path)
self._clippath = path
[docs] def set_dashes(self, dash_offset, dash_list):
"""
Set the dash style for the gc.
Parameters
----------
dash_offset : float or None
The offset (usually 0).
dash_list : array-like or None
The on-off sequence as points.
Notes
-----
``(None, None)`` specifies a solid line.
See p. 107 of to PostScript `blue book`_ for more info.
.. _blue book: https://www-cdf.fnal.gov/offline/PostScript/BLUEBOOK.PDF
"""
if dash_list is not None:
dl = np.asarray(dash_list)
if np.any(dl < 0.0):
raise ValueError(
"All values in the dash list must be positive")
self._dashes = dash_offset, dash_list
[docs] def set_foreground(self, fg, isRGBA=False):
"""
Set the foreground color.
Parameters
----------
fg : color
isRGBA : bool
If *fg* is known to be an ``(r, g, b, a)`` tuple, *isRGBA* can be
set to True to improve performance.
"""
if self._forced_alpha and isRGBA:
self._rgb = fg[:3] + (self._alpha,)
elif self._forced_alpha:
self._rgb = colors.to_rgba(fg, self._alpha)
elif isRGBA:
self._rgb = fg
else:
self._rgb = colors.to_rgba(fg)
[docs] def set_joinstyle(self, js):
"""Set the join style to be one of ('miter', 'round', 'bevel')."""
cbook._check_in_list(['miter', 'round', 'bevel'], js=js)
self._joinstyle = js
[docs] def set_linewidth(self, w):
"""Set the linewidth in points."""
self._linewidth = float(w)
[docs] def set_url(self, url):
"""Set the url for links in compatible backends."""
self._url = url
[docs] def set_gid(self, id):
"""Set the id."""
self._gid = id
[docs] def set_snap(self, snap):
"""
Set the snap setting which may be:
* True: snap vertices to the nearest pixel center
* False: leave vertices as-is
* None: (auto) If the path contains only rectilinear line segments,
round to the nearest pixel center
"""
self._snap = snap
[docs] def set_hatch(self, hatch):
"""Set the hatch style (for fills)."""
self._hatch = hatch
[docs] def get_hatch(self):
"""Get the current hatch style."""
return self._hatch
[docs] def get_hatch_path(self, density=6.0):
"""Return a `.Path` for the current hatch."""
hatch = self.get_hatch()
if hatch is None:
return None
return Path.hatch(hatch, density)
[docs] def get_hatch_color(self):
"""Get the hatch color."""
return self._hatch_color
[docs] def set_hatch_color(self, hatch_color):
"""Set the hatch color."""
self._hatch_color = hatch_color
[docs] def get_hatch_linewidth(self):
"""Get the hatch linewidth."""
return self._hatch_linewidth
[docs] def get_sketch_params(self):
"""
Return the sketch parameters for the artist.
Returns
-------
tuple or `None`
A 3-tuple with the following elements:
* ``scale``: The amplitude of the wiggle perpendicular to the
source line.
* ``length``: The length of the wiggle along the line.
* ``randomness``: The scale factor by which the length is
shrunken or expanded.
May return `None` if no sketch parameters were set.
"""
return self._sketch
[docs] def set_sketch_params(self, scale=None, length=None, randomness=None):
"""
Set the sketch parameters.
Parameters
----------
scale : float, optional
The amplitude of the wiggle perpendicular to the source line, in
pixels. If scale is `None`, or not provided, no sketch filter will
be provided.
length : float, default: 128
The length of the wiggle along the line, in pixels.
randomness : float, default: 16
The scale factor by which the length is shrunken or expanded.
"""
self._sketch = (
None if scale is None
else (scale, length or 128., randomness or 16.))
[docs]class TimerBase:
"""
A base class for providing timer events, useful for things animations.
Backends need to implement a few specific methods in order to use their
own timing mechanisms so that the timer events are integrated into their
event loops.
Subclasses must override the following methods:
- ``_timer_start``: Backend-specific code for starting the timer.
- ``_timer_stop``: Backend-specific code for stopping the timer.
Subclasses may additionally override the following methods:
- ``_timer_set_single_shot``: Code for setting the timer to single shot
operating mode, if supported by the timer object. If not, the `Timer`
class itself will store the flag and the ``_on_timer`` method should be
overridden to support such behavior.
- ``_timer_set_interval``: Code for setting the interval on the timer, if
there is a method for doing so on the timer object.
- ``_on_timer``: The internal function that any timer object should call,
which will handle the task of running all callbacks that have been set.
"""
def __init__(self, interval=None, callbacks=None):
"""
Parameters
----------
interval : int, default: 1000ms
The time between timer events in milliseconds. Will be stored as
``timer.interval``.
callbacks : List[Tuple[callable, Tuple, Dict]]
List of (func, args, kwargs) tuples that will be called upon
timer events. This list is accessible as ``timer.callbacks`` and
can be manipulated directly, or the functions `add_callback` and
`remove_callback` can be used.
"""
self.callbacks = [] if callbacks is None else callbacks.copy()
# Set .interval and not ._interval to go through the property setter.
self.interval = 1000 if interval is None else interval
self.single_shot = False
def __del__(self):
"""Need to stop timer and possibly disconnect timer."""
self._timer_stop()
[docs] def start(self, interval=None):
"""
Start the timer object.
Parameters
----------
interval : int, optional
Timer interval in milliseconds; overrides a previously set interval
if provided.
"""
if interval is not None:
self.interval = interval
self._timer_start()
[docs] def stop(self):
"""Stop the timer."""
self._timer_stop()
def _timer_start(self):
pass
def _timer_stop(self):
pass
@property
def interval(self):
"""The time between timer events, in milliseconds."""
return self._interval
@interval.setter
def interval(self, interval):
# Force to int since none of the backends actually support fractional
# milliseconds, and some error or give warnings.
interval = int(interval)
self._interval = interval
self._timer_set_interval()
@property
def single_shot(self):
"""Whether this timer should stop after a single run."""
return self._single
@single_shot.setter
def single_shot(self, ss):
self._single = ss
self._timer_set_single_shot()
[docs] def add_callback(self, func, *args, **kwargs):
"""
Register *func* to be called by timer when the event fires. Any
additional arguments provided will be passed to *func*.
This function returns *func*, which makes it possible to use it as a
decorator.
"""
self.callbacks.append((func, args, kwargs))
return func
[docs] def remove_callback(self, func, *args, **kwargs):
"""
Remove *func* from list of callbacks.
*args* and *kwargs* are optional and used to distinguish between copies
of the same function registered to be called with different arguments.
This behavior is deprecated. In the future, ``*args, **kwargs`` won't
be considered anymore; to keep a specific callback removable by itself,
pass it to `add_callback` as a `functools.partial` object.
"""
if args or kwargs:
cbook.warn_deprecated(
"3.1", message="In a future version, Timer.remove_callback "
"will not take *args, **kwargs anymore, but remove all "
"callbacks where the callable matches; to keep a specific "
"callback removable by itself, pass it to add_callback as a "
"functools.partial object.")
self.callbacks.remove((func, args, kwargs))
else:
funcs = [c[0] for c in self.callbacks]
if func in funcs:
self.callbacks.pop(funcs.index(func))
def _timer_set_interval(self):
"""Used to set interval on underlying timer object."""
def _timer_set_single_shot(self):
"""Used to set single shot on underlying timer object."""
def _on_timer(self):
"""
Runs all function that have been registered as callbacks. Functions
can return False (or 0) if they should not be called any more. If there
are no callbacks, the timer is automatically stopped.
"""
for func, args, kwargs in self.callbacks:
ret = func(*args, **kwargs)
# docstring above explains why we use `if ret == 0` here,
# instead of `if not ret`.
# This will also catch `ret == False` as `False == 0`
# but does not annoy the linters
# https://docs.python.org/3/library/stdtypes.html#boolean-values
if ret == 0:
self.callbacks.remove((func, args, kwargs))
if len(self.callbacks) == 0:
self.stop()
[docs]class Event:
"""
A Matplotlib event. Attach additional attributes as defined in
:meth:`FigureCanvasBase.mpl_connect`. The following attributes
are defined and shown with their default values
Attributes
----------
name : str
The event name.
canvas : `FigureCanvasBase`
The backend-specific canvas instance generating the event.
guiEvent
The GUI event that triggered the Matplotlib event.
"""
def __init__(self, name, canvas, guiEvent=None):
self.name = name
self.canvas = canvas
self.guiEvent = guiEvent
[docs]class DrawEvent(Event):
"""
An event triggered by a draw operation on the canvas
In most backends callbacks subscribed to this callback will be
fired after the rendering is complete but before the screen is
updated. Any extra artists drawn to the canvas's renderer will
be reflected without an explicit call to ``blit``.
.. warning::
Calling ``canvas.draw`` and ``canvas.blit`` in these callbacks may
not be safe with all backends and may cause infinite recursion.
In addition to the `Event` attributes, the following event
attributes are defined:
Attributes
----------
renderer : `RendererBase`
The renderer for the draw event.
"""
def __init__(self, name, canvas, renderer):
Event.__init__(self, name, canvas)
self.renderer = renderer
[docs]class ResizeEvent(Event):
"""
An event triggered by a canvas resize
In addition to the `Event` attributes, the following event
attributes are defined:
Attributes
----------
width : int
Width of the canvas in pixels.
height : int
Height of the canvas in pixels.
"""
def __init__(self, name, canvas):
Event.__init__(self, name, canvas)
self.width, self.height = canvas.get_width_height()
[docs]class CloseEvent(Event):
"""An event triggered by a figure being closed."""
[docs]class LocationEvent(Event):
"""
An event that has a screen location.
The following additional attributes are defined and shown with
their default values.
In addition to the `Event` attributes, the following
event attributes are defined:
Attributes
----------
x : int
x position - pixels from left of canvas.
y : int
y position - pixels from bottom of canvas.
inaxes : `~.axes.Axes` or None
The `~.axes.Axes` instance over which the mouse is, if any.
xdata : float or None
x data coordinate of the mouse.
ydata : float or None
y data coordinate of the mouse.
"""
lastevent = None # the last event that was triggered before this one
def __init__(self, name, canvas, x, y, guiEvent=None):
"""
(*x*, *y*) in figure coords ((0, 0) = bottom left).
"""
Event.__init__(self, name, canvas, guiEvent=guiEvent)
# x position - pixels from left of canvas
self.x = int(x) if x is not None else x
# y position - pixels from right of canvas
self.y = int(y) if y is not None else y
self.inaxes = None # the Axes instance if mouse us over axes
self.xdata = None # x coord of mouse in data coords
self.ydata = None # y coord of mouse in data coords
if x is None or y is None:
# cannot check if event was in axes if no (x, y) info
self._update_enter_leave()
return
if self.canvas.mouse_grabber is None:
self.inaxes = self.canvas.inaxes((x, y))
else:
self.inaxes = self.canvas.mouse_grabber
if self.inaxes is not None:
try:
trans = self.inaxes.transData.inverted()
xdata, ydata = trans.transform((x, y))
except ValueError:
pass
else:
self.xdata = xdata
self.ydata = ydata
self._update_enter_leave()
def _update_enter_leave(self):
"""Process the figure/axes enter leave events."""
if LocationEvent.lastevent is not None:
last = LocationEvent.lastevent
if last.inaxes != self.inaxes:
# process axes enter/leave events
try:
if last.inaxes is not None:
last.canvas.callbacks.process('axes_leave_event', last)
except Exception:
pass
# See ticket 2901582.
# I think this is a valid exception to the rule
# against catching all exceptions; if anything goes
# wrong, we simply want to move on and process the
# current event.
if self.inaxes is not None:
self.canvas.callbacks.process('axes_enter_event', self)
else:
# process a figure enter event
if self.inaxes is not None:
self.canvas.callbacks.process('axes_enter_event', self)
LocationEvent.lastevent = self
[docs]class MouseEvent(LocationEvent):
"""
A mouse event ('button_press_event',
'button_release_event',
'scroll_event',
'motion_notify_event').
In addition to the `Event` and `LocationEvent`
attributes, the following attributes are defined:
Attributes
----------
button : None or `MouseButton` or {'up', 'down'}
The button pressed. 'up' and 'down' are used for scroll events.
Note that in the nbagg backend, both the middle and right clicks
return RIGHT since right clicking will bring up the context menu in
some browsers.
Note that LEFT and RIGHT actually refer to the "primary" and
"secondary" buttons, i.e. if the user inverts their left and right
buttons ("left-handed setting") then the LEFT button will be the one
physically on the right.
key : None or str
The key pressed when the mouse event triggered, e.g. 'shift'.
See `KeyEvent`.
.. warning::
This key is currently obtained from the last 'key_press_event' or
'key_release_event' that occurred within the canvas. Thus, if the
last change of keyboard state occurred while the canvas did not have
focus, this attribute will be wrong.
step : float
The number of scroll steps (positive for 'up', negative for 'down').
This applies only to 'scroll_event' and defaults to 0 otherwise.
dblclick : bool
Whether the event is a double-click. This applies only to
'button_press_event' and is False otherwise. In particular, it's
not used in 'button_release_event'.
Examples
--------
::
def on_press(event):
print('you pressed', event.button, event.xdata, event.ydata)
cid = fig.canvas.mpl_connect('button_press_event', on_press)
"""
def __init__(self, name, canvas, x, y, button=None, key=None,
step=0, dblclick=False, guiEvent=None):
"""
(*x*, *y*) in figure coords ((0, 0) = bottom left)
button pressed None, 1, 2, 3, 'up', 'down'
"""
if button in MouseButton.__members__.values():
button = MouseButton(button)
self.button = button
self.key = key
self.step = step
self.dblclick = dblclick
# super-init is deferred to the end because it calls back on
# 'axes_enter_event', which requires a fully initialized event.
LocationEvent.__init__(self, name, canvas, x, y, guiEvent=guiEvent)
def __str__(self):
return (f"{self.name}: "
f"xy=({self.x}, {self.y}) xydata=({self.xdata}, {self.ydata}) "
f"button={self.button} dblclick={self.dblclick} "
f"inaxes={self.inaxes}")
[docs]class PickEvent(Event):
"""
A pick event, fired when the user picks a location on the canvas
sufficiently close to an artist.
Attrs: all the `Event` attributes plus
Attributes
----------
mouseevent : `MouseEvent`
The mouse event that generated the pick.
artist : `matplotlib.artist.Artist`
The picked artist.
other
Additional attributes may be present depending on the type of the
picked object; e.g., a `~.Line2D` pick may define different extra
attributes than a `~.PatchCollection` pick.
Examples
--------
Bind a function ``on_pick()`` to pick events, that prints the coordinates
of the picked data point::
ax.plot(np.rand(100), 'o', picker=5) # 5 points tolerance
def on_pick(event):
line = event.artist
xdata, ydata = line.get_data()
ind = event.ind
print('on pick line:', np.array([xdata[ind], ydata[ind]]).T)
cid = fig.canvas.mpl_connect('pick_event', on_pick)
"""
def __init__(self, name, canvas, mouseevent, artist,
guiEvent=None, **kwargs):
Event.__init__(self, name, canvas, guiEvent)
self.mouseevent = mouseevent
self.artist = artist
self.__dict__.update(kwargs)
[docs]class KeyEvent(LocationEvent):
"""
A key event (key press, key release).
Attach additional attributes as defined in
:meth:`FigureCanvasBase.mpl_connect`.
In addition to the `Event` and `LocationEvent`
attributes, the following attributes are defined:
Attributes
----------
key : None or str
the key(s) pressed. Could be **None**, a single case sensitive ascii
character ("g", "G", "#", etc.), a special key
("control", "shift", "f1", "up", etc.) or a
combination of the above (e.g., "ctrl+alt+g", "ctrl+alt+G").
Notes
-----
Modifier keys will be prefixed to the pressed key and will be in the order
"ctrl", "alt", "super". The exception to this rule is when the pressed key
is itself a modifier key, therefore "ctrl+alt" and "alt+control" can both
be valid key values.
Examples
--------
::
def on_key(event):
print('you pressed', event.key, event.xdata, event.ydata)
cid = fig.canvas.mpl_connect('key_press_event', on_key)
"""
def __init__(self, name, canvas, key, x=0, y=0, guiEvent=None):
self.key = key
# super-init deferred to the end: callback errors if called before
LocationEvent.__init__(self, name, canvas, x, y, guiEvent=guiEvent)
def _get_renderer(figure, print_method=None):
"""
Get the renderer that would be used to save a `~.Figure`, and cache it on
the figure.
If you need a renderer without any active draw methods use
renderer._draw_disabled to temporary patch them out at your call site.
"""
# This is implemented by triggering a draw, then immediately jumping out of
# Figure.draw() by raising an exception.
class Done(Exception):
pass
def _draw(renderer): raise Done(renderer)
with cbook._setattr_cm(figure, draw=_draw):
orig_canvas = figure.canvas
if print_method is None:
fmt = figure.canvas.get_default_filetype()
# Even for a canvas' default output type, a canvas switch may be
# needed, e.g. for FigureCanvasBase.
print_method = getattr(
figure.canvas._get_output_canvas(None, fmt), f"print_{fmt}")
try:
print_method(io.BytesIO(), dpi=figure.dpi)
except Done as exc:
renderer, = figure._cachedRenderer, = exc.args
return renderer
else:
raise RuntimeError(f"{print_method} did not call Figure.draw, so "
f"no renderer is available")
finally:
figure.canvas = orig_canvas
def _is_non_interactive_terminal_ipython(ip):
"""
Return whether we are in a a terminal IPython, but non interactive.
When in _terminal_ IPython, ip.parent will have and `interact` attribute,
if this attribute is False we do not setup eventloop integration as the
user will _not_ interact with IPython. In all other case (ZMQKernel, or is
interactive), we do.
"""
return (hasattr(ip, 'parent')
and (ip.parent is not None)
and getattr(ip.parent, 'interact', None) is False)
def _check_savefig_extra_args(func=None, extra_kwargs=()):
"""
Decorator for the final print_* methods that accept keyword arguments.
If any unused keyword arguments are left, this decorator will warn about
them, and as part of the warning, will attempt to specify the function that
the user actually called, instead of the backend-specific method. If unable
to determine which function the user called, it will specify `.savefig`.
For compatibility across backends, this does not warn about keyword
arguments added by `FigureCanvasBase.print_figure` for use in a subset of
backends, because the user would not have added them directly.
"""
if func is None:
return functools.partial(_check_savefig_extra_args,
extra_kwargs=extra_kwargs)
old_sig = inspect.signature(func)
@functools.wraps(func)
def wrapper(*args, **kwargs):
name = 'savefig' # Reasonable default guess.
public_api = re.compile(r'^savefig|print_[A-Za-z0-9]+$')
seen_print_figure = False
for frame, line in traceback.walk_stack(None):
if frame is None:
# when called in embedded context may hit frame is None.
break
if re.match(r'\A(matplotlib|mpl_toolkits)(\Z|\.(?!tests\.))',
# Work around sphinx-gallery not setting __name__.
frame.f_globals.get('__name__', '')):
if public_api.match(frame.f_code.co_name):
name = frame.f_code.co_name
if name == 'print_figure':
seen_print_figure = True
else:
break
accepted_kwargs = {*old_sig.parameters, *extra_kwargs}
if seen_print_figure:
for kw in ['dpi', 'facecolor', 'edgecolor', 'orientation',
'bbox_inches_restore']:
# Ignore keyword arguments that are passed in by print_figure
# for the use of other renderers.
if kw not in accepted_kwargs:
kwargs.pop(kw, None)
for arg in list(kwargs):
if arg in accepted_kwargs:
continue
cbook.warn_deprecated(
'3.3', name=name,
message='%(name)s() got unexpected keyword argument "'
+ arg + '" which is no longer supported as of '
'%(since)s and will become an error '
'%(removal)s')
kwargs.pop(arg)
return func(*args, **kwargs)
return wrapper
[docs]def key_press_handler(event, canvas=None, toolbar=None):
"""
Implement the default Matplotlib key bindings for the canvas and toolbar
described at :ref:`key-event-handling`.
Parameters
----------
event : `KeyEvent`
A key press/release event.
canvas : `FigureCanvasBase`, default: ``event.canvas``
The backend-specific canvas instance. This parameter is kept for
back-compatibility, but, if set, should always be equal to
``event.canvas``.
toolbar : `NavigationToolbar2`, default: ``event.canvas.toolbar``
The navigation cursor toolbar. This parameter is kept for
back-compatibility, but, if set, should always be equal to
``event.canvas.toolbar``.
"""
# these bindings happen whether you are over an axes or not
if event.key is None:
return
if canvas is None:
canvas = event.canvas
if toolbar is None:
toolbar = canvas.toolbar
# Load key-mappings from rcParams.
fullscreen_keys = rcParams['keymap.fullscreen']
home_keys = rcParams['keymap.home']
back_keys = rcParams['keymap.back']
forward_keys = rcParams['keymap.forward']
pan_keys = rcParams['keymap.pan']
zoom_keys = rcParams['keymap.zoom']
save_keys = rcParams['keymap.save']
quit_keys = rcParams['keymap.quit']
quit_all_keys = rcParams['keymap.quit_all']
grid_keys = rcParams['keymap.grid']
grid_minor_keys = rcParams['keymap.grid_minor']
toggle_yscale_keys = rcParams['keymap.yscale']
toggle_xscale_keys = rcParams['keymap.xscale']
all_keys = dict.__getitem__(rcParams, 'keymap.all_axes')
# toggle fullscreen mode ('f', 'ctrl + f')
if event.key in fullscreen_keys:
try:
canvas.manager.full_screen_toggle()
except AttributeError:
pass
# quit the figure (default key 'ctrl+w')
if event.key in quit_keys:
Gcf.destroy_fig(canvas.figure)
if event.key in quit_all_keys:
Gcf.destroy_all()
if toolbar is not None:
# home or reset mnemonic (default key 'h', 'home' and 'r')
if event.key in home_keys:
toolbar.home()
# forward / backward keys to enable left handed quick navigation
# (default key for backward: 'left', 'backspace' and 'c')
elif event.key in back_keys:
toolbar.back()
# (default key for forward: 'right' and 'v')
elif event.key in forward_keys:
toolbar.forward()
# pan mnemonic (default key 'p')
elif event.key in pan_keys:
toolbar.pan()
toolbar._update_cursor(event)
# zoom mnemonic (default key 'o')
elif event.key in zoom_keys:
toolbar.zoom()
toolbar._update_cursor(event)
# saving current figure (default key 's')
elif event.key in save_keys:
toolbar.save_figure()
if event.inaxes is None:
return
# these bindings require the mouse to be over an axes to trigger
def _get_uniform_gridstate(ticks):
# Return True/False if all grid lines are on or off, None if they are
# not all in the same state.
if all(tick.gridline.get_visible() for tick in ticks):
return True
elif not any(tick.gridline.get_visible() for tick in ticks):
return False
else:
return None
ax = event.inaxes
# toggle major grids in current axes (default key 'g')
# Both here and below (for 'G'), we do nothing if *any* grid (major or
# minor, x or y) is not in a uniform state, to avoid messing up user
# customization.
if (event.key in grid_keys
# Exclude minor grids not in a uniform state.
and None not in [_get_uniform_gridstate(ax.xaxis.minorTicks),
_get_uniform_gridstate(ax.yaxis.minorTicks)]):
x_state = _get_uniform_gridstate(ax.xaxis.majorTicks)
y_state = _get_uniform_gridstate(ax.yaxis.majorTicks)
cycle = [(False, False), (True, False), (True, True), (False, True)]
try:
x_state, y_state = (
cycle[(cycle.index((x_state, y_state)) + 1) % len(cycle)])
except ValueError:
# Exclude major grids not in a uniform state.
pass
else:
# If turning major grids off, also turn minor grids off.
ax.grid(x_state, which="major" if x_state else "both", axis="x")
ax.grid(y_state, which="major" if y_state else "both", axis="y")
canvas.draw_idle()
# toggle major and minor grids in current axes (default key 'G')
if (event.key in grid_minor_keys
# Exclude major grids not in a uniform state.
and None not in [_get_uniform_gridstate(ax.xaxis.majorTicks),
_get_uniform_gridstate(ax.yaxis.majorTicks)]):
x_state = _get_uniform_gridstate(ax.xaxis.minorTicks)
y_state = _get_uniform_gridstate(ax.yaxis.minorTicks)
cycle = [(False, False), (True, False), (True, True), (False, True)]
try:
x_state, y_state = (
cycle[(cycle.index((x_state, y_state)) + 1) % len(cycle)])
except ValueError:
# Exclude minor grids not in a uniform state.
pass
else:
ax.grid(x_state, which="both", axis="x")
ax.grid(y_state, which="both", axis="y")
canvas.draw_idle()
# toggle scaling of y-axes between 'log and 'linear' (default key 'l')
elif event.key in toggle_yscale_keys:
scale = ax.get_yscale()
if scale == 'log':
ax.set_yscale('linear')
ax.figure.canvas.draw_idle()
elif scale == 'linear':
try:
ax.set_yscale('log')
except ValueError as exc:
_log.warning(str(exc))
ax.set_yscale('linear')
ax.figure.canvas.draw_idle()
# toggle scaling of x-axes between 'log and 'linear' (default key 'k')
elif event.key in toggle_xscale_keys:
scalex = ax.get_xscale()
if scalex == 'log':
ax.set_xscale('linear')
ax.figure.canvas.draw_idle()
elif scalex == 'linear':
try:
ax.set_xscale('log')
except ValueError as exc:
_log.warning(str(exc))
ax.set_xscale('linear')
ax.figure.canvas.draw_idle()
# enable nagivation for all axes that contain the event (default key 'a')
elif event.key in all_keys:
for a in canvas.figure.get_axes():
if (event.x is not None and event.y is not None
and a.in_axes(event)): # FIXME: Why only these?
cbook.warn_deprecated(
"3.3", message="Toggling axes navigation from the "
"keyboard is deprecated since %(since)s and will be "
"removed %(removal)s.")
a.set_navigate(True)
# enable navigation only for axes with this index (if such an axes exist,
# otherwise do nothing)
elif event.key.isdigit() and event.key != '0':
n = int(event.key) - 1
if n < len(canvas.figure.get_axes()):
for i, a in enumerate(canvas.figure.get_axes()):
if (event.x is not None and event.y is not None
and a.in_axes(event)): # FIXME: Why only these?
cbook.warn_deprecated(
"3.3", message="Toggling axes navigation from the "
"keyboard is deprecated since %(since)s and will be "
"removed %(removal)s.")
a.set_navigate(i == n)
[docs]class NonGuiException(Exception):
"""Raised when trying show a figure in a non-GUI backend."""
pass
cursors = tools.cursors
class _Mode(str, Enum):
NONE = ""
PAN = "pan/zoom"
ZOOM = "zoom rect"
def __str__(self):
return self.value
@property
def _navigate_mode(self):
return self.name if self is not _Mode.NONE else None
[docs]@cbook.deprecated("3.3")
class StatusbarBase:
"""Base class for the statusbar."""
def __init__(self, toolmanager):
self.toolmanager = toolmanager
self.toolmanager.toolmanager_connect('tool_message_event',
self._message_cbk)
def _message_cbk(self, event):
"""Capture the 'tool_message_event' and set the message."""
self.set_message(event.message)
[docs] def set_message(self, s):
"""
Display a message on toolbar or in status bar.
Parameters
----------
s : str
Message text.
"""
class _Backend:
# A backend can be defined by using the following pattern:
#
# @_Backend.export
# class FooBackend(_Backend):
# # override the attributes and methods documented below.
# `backend_version` may be overridden by the subclass.
backend_version = "unknown"
# The `FigureCanvas` class must be defined.
FigureCanvas = None
# For interactive backends, the `FigureManager` class must be overridden.
FigureManager = FigureManagerBase
# The following methods must be left as None for non-interactive backends.
# For interactive backends, `trigger_manager_draw` should be a function
# taking a manager as argument and triggering a canvas draw, and `mainloop`
# should be a function taking no argument and starting the backend main
# loop.
trigger_manager_draw = None
mainloop = None
# The following methods will be automatically defined and exported, but
# can be overridden.
@classmethod
def new_figure_manager(cls, num, *args, **kwargs):
"""Create a new figure manager instance."""
# This import needs to happen here due to circular imports.
from matplotlib.figure import Figure
fig_cls = kwargs.pop('FigureClass', Figure)
fig = fig_cls(*args, **kwargs)
return cls.new_figure_manager_given_figure(num, fig)
@classmethod
def new_figure_manager_given_figure(cls, num, figure):
"""Create a new figure manager instance for the given figure."""
canvas = cls.FigureCanvas(figure)
manager = cls.FigureManager(canvas, num)
return manager
@classmethod
def draw_if_interactive(cls):
if cls.trigger_manager_draw is not None and is_interactive():
manager = Gcf.get_active()
if manager:
cls.trigger_manager_draw(manager)
@classmethod
def show(cls, *, block=None):
"""
Show all figures.
`show` blocks by calling `mainloop` if *block* is ``True``, or if it
is ``None`` and we are neither in IPython's ``%pylab`` mode, nor in
`interactive` mode.
"""
managers = Gcf.get_all_fig_managers()
if not managers:
return
for manager in managers:
try:
manager.show() # Emits a warning for non-interactive backend.
except NonGuiException as exc:
cbook._warn_external(str(exc))
if cls.mainloop is None:
return
if block is None:
# Hack: Are we in IPython's pylab mode?
from matplotlib import pyplot
try:
# IPython versions >= 0.10 tack the _needmain attribute onto
# pyplot.show, and always set it to False, when in %pylab mode.
ipython_pylab = not pyplot.show._needmain
except AttributeError:
ipython_pylab = False
block = not ipython_pylab and not is_interactive()
# TODO: The above is a hack to get the WebAgg backend working with
# ipython's `%pylab` mode until proper integration is implemented.
if get_backend() == "WebAgg":
block = True
if block:
cls.mainloop()
# This method is the one actually exporting the required methods.
@staticmethod
def export(cls):
for name in [
"backend_version",
"FigureCanvas",
"FigureManager",
"new_figure_manager",
"new_figure_manager_given_figure",
"draw_if_interactive",
"show",
]:
setattr(sys.modules[cls.__module__], name, getattr(cls, name))
# For back-compatibility, generate a shim `Show` class.
class Show(ShowBase):
def mainloop(self):
return cls.mainloop()
setattr(sys.modules[cls.__module__], "Show", Show)
return cls
[docs]class ShowBase(_Backend):
"""
Simple base class to generate a ``show()`` function in backends.
Subclass must override ``mainloop()`` method.
"""
def __call__(self, block=None):
return self.show(block=block)