Version 3.0.2
matplotlib
Fork me on GitHub

Table of Contents

Related Topics

textpath

matplotlib.textpath

class matplotlib.textpath.TextPath(xy, s, size=None, prop=None, _interpolation_steps=1, usetex=False, *kl, **kwargs)[source]

Bases: matplotlib.path.Path

Create a path from the text.

Create a path from the text. Note that it simply is a path, not an artist. You need to use the PathPatch (or other artists) to draw this path onto the canvas.

Parameters:
xy : tuple or array of two float values

Position of the text. For no offset, use xy=(0, 0).

s : str

The text to convert to a path.

size : float, optional

Font size in points. Defaults to the size specified via the font properties prop.

prop : matplotlib.font_manager.FontProperties, optional

Font property. If not provided, will use a default FontProperties with parameters from the rcParams.

_interpolation_steps : integer, optional

(Currently ignored)

usetex : bool, optional

Whether to use tex rendering. Defaults to False.

Examples

The following creates a path from the string "ABC" with Helvetica font face; and another path from the latex fraction 1/2:

from matplotlib.textpath import TextPath
from matplotlib.font_manager import FontProperties

fp = FontProperties(family="Helvetica", style="italic")
path1 = TextPath((12,12), "ABC", size=12, prop=fp)
path2 = TextPath((0,0), r"$\frac{1}{2}$", size=12, usetex=True)

Also see Demo Text Path.

codes

Return the codes

get_size()[source]

get the size of the text

is_math_text(s)[source]

Returns True if the given string s contains any mathtext.

set_size(size)[source]

set the size of the text

text_get_vertices_codes(prop, s, usetex)[source]

convert the string s to vertices and codes using the provided font property prop. Mostly copied from backend_svg.py.

vertices

Return the cached path after updating it if necessary.

class matplotlib.textpath.TextToPath[source]

Bases: object

A class that convert a given text to a path using ttf fonts.

DPI = 72
FONT_SCALE = 100.0
get_glyphs_mathtext(prop, s, glyph_map=None, return_new_glyphs_only=False)[source]

convert the string s to vertices and codes by parsing it with mathtext.

get_glyphs_tex(prop, s, glyph_map=None, return_new_glyphs_only=False)[source]

convert the string s to vertices and codes using matplotlib's usetex mode.

get_glyphs_with_font(font, s, glyph_map=None, return_new_glyphs_only=False)[source]

Convert string s to vertices and codes using the provided ttf font.

get_texmanager()[source]

return the matplotlib.texmanager.TexManager instance

get_text_path(prop, s, ismath=False, usetex=False)[source]

Convert text s to path (a tuple of vertices and codes for matplotlib.path.Path).

Parameters:
prop : matplotlib.font_manager.FontProperties instance

The font properties for the text.

s : str

The text to be converted.

usetex : bool, optional

Whether to use tex rendering. Defaults to False.

ismath : bool, optional

If True, use mathtext parser. Effective only if usetex == False.

Returns:
verts, codes : tuple of lists

verts is a list of numpy arrays containing the x and y coordinates of the vertices. codes is a list of path codes.

Examples

Create a list of vertices and codes from a text, and create a Path from those:

from matplotlib.path import Path
from matplotlib.textpath import TextToPath
from matplotlib.font_manager import FontProperties

fp = FontProperties(family="Humor Sans", style="italic")
verts, codes = TextToPath().get_text_path(fp, "ABC")
path = Path(verts, codes, closed=False)

Also see TextPath for a more direct way to create a path from a text.

get_text_width_height_descent(s, prop, ismath)[source]
glyph_to_path(font, currx=0.0)[source]

convert the ft2font glyph to vertices and codes.

tex_font_map

Deprecated since version 3.0: The tex_font_map function was deprecated in Matplotlib 3.0 and will be removed in 3.2.