matplotlib.mathtext
A module for parsing a subset of the TeX math syntax and rendering it to a
Matplotlib backend.
For a tutorial of its usage, see Writing mathematical expressions. This
document is primarily concerned with implementation details.
The module uses pyparsing to parse the TeX expression.
The Bakoma distribution of the TeX Computer Modern fonts, and STIX
fonts are supported. There is experimental support for using
arbitrary fonts, but results may vary without proper tweaking and
metrics for those fonts.
-
class matplotlib.mathtext.MathTextParser(output)
Bases:
object
Create a MathTextParser for the given backend output.
- Parameters:
- output : {"path", "agg"}
Whether to return a VectorParse ("path") or a
RasterParse ("agg", or its synonym "macosx").
-
parse(s, dpi=72, prop=None, *, antialiased=None)
Parse the given math expression s at the given dpi. If prop is
provided, it is a FontProperties object specifying the "default"
font to use in the math expression, used for all non-math text.
The results are cached, so multiple calls to parse
with the same expression should be fast.
Depending on the output type, this returns either a VectorParse or
a RasterParse.
-
class matplotlib.mathtext.RasterParse(ox, oy, width, height, depth, image)
Bases: NamedTuple
The namedtuple type returned by MathTextParser("agg").parse(...).
- Attributes:
- ox, oy : float
The offsets are always zero.
- width, height, depth : float
The global metrics.
- image : 2D array of uint8
A raster image.
Create new instance of RasterParse(ox, oy, width, height, depth, image)
-
depth
Alias for field number 4
-
height
Alias for field number 3
-
image
Alias for field number 5
-
ox
Alias for field number 0
-
oy
Alias for field number 1
-
width
Alias for field number 2
-
class matplotlib.mathtext.VectorParse(width, height, depth, glyphs, rects)
Bases: NamedTuple
The namedtuple type returned by MathTextParser("path").parse(...).
- Attributes:
- width, height, depth : float
The global metrics.
- glyphs : list
The glyphs including their positions.
- rect : list
The list of rectangles.
Create new instance of VectorParse(width, height, depth, glyphs, rects)
-
depth
Alias for field number 2
-
glyphs
Alias for field number 3
-
height
Alias for field number 1
-
rects
Alias for field number 4
-
width
Alias for field number 0
-
matplotlib.mathtext.get_unicode_index(symbol)
Return the integer index (from the Unicode table) of symbol.
- Parameters:
- symbol : str
A single (Unicode) character, a TeX command (e.g. r'pi') or a Type1
symbol name (e.g. 'phi').
-
matplotlib.mathtext.math_to_image(s, filename_or_obj, prop=None, dpi=None, format=None, *, color=None)
Given a math expression, renders it in a closely-clipped bounding
box to an image file.
- Parameters:
- s : str
A math expression. The math portion must be enclosed in dollar signs.
- filename_or_obj : str or path-like or file-like
Where to write the image data.
- prop :
FontProperties, optional The size and style of the text.
- dpi : float, optional
The output dpi. If not set, the dpi is determined as for
Figure.savefig.
- format : str, optional
The output format, e.g., 'svg', 'pdf', 'ps' or 'png'. If not set, the
format is determined as for Figure.savefig.
- color : str, optional
Foreground color, defaults to rcParams["text.color"] (default: 'black').