Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Doc: document textpath module #12676

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion doc/api/api_overview.rst
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@ Further reading:
- `matplotlib.axes.Axes` and `matplotlib.figure.Figure` for an overview of
plotting functions.
- Most of the :ref:`examples <examples-index>` use the object-oriented approach
(except for the pyplot section).
(except for the pyplot section)
- The list of :doc:`matplotlib modules </api/index>`.


The pylab API (disapproved)
Expand Down
1 change: 1 addition & 0 deletions doc/api/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ Modules
style_api.rst
table_api.rst
text_api.rst
textpath_api.rst
ticker_api.rst
tight_layout_api.rst
transformations.rst
Expand Down
12 changes: 12 additions & 0 deletions doc/api/textpath_api.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
********
textpath
********


:mod:`matplotlib.textpath`
==========================

.. automodule:: matplotlib.textpath
:members:
:undoc-members:
:show-inheritance:
3 changes: 3 additions & 0 deletions examples/text_labels_and_annotations/demo_text_path.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
Demo Text Path
==============

Use a text as `Path`. The tool that allows for such conversion is a
`~matplotlib.textpath.TextPath`. The resulting path can be employed
e.g. as a clip path for an image.
"""

import matplotlib.pyplot as plt
Expand Down
94 changes: 76 additions & 18 deletions lib/matplotlib/textpath.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,22 +104,47 @@ def get_text_width_height_descent(self, s, prop, ismath):

def get_text_path(self, prop, s, ismath=False, usetex=False):
"""
convert text *s* to path (a tuple of vertices and codes for
Convert text *s* to path (a tuple of vertices and codes for
matplotlib.path.Path).

*prop*
font property
Parameters
----------

*s*
text to be converted
prop : `matplotlib.font_manager.FontProperties` instance
The font properties for the text.

*usetex*
If True, use matplotlib usetex mode.
s : str
The text to be converted.

*ismath*
If True, use mathtext parser. Effective only if usetex == False.
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
-------

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FYI, you don't need blank lines after the headers.

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.
"""
if not usetex:
if not ismath:
Expand Down Expand Up @@ -391,16 +416,49 @@ class TextPath(Path):
def __init__(self, xy, s, size=None, prop=None,
_interpolation_steps=1, usetex=False,
*kl, **kwargs):
"""
Create a path from the text. No support for TeX yet. 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.
r"""
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
:ref:`rcParams <matplotlib-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)

xy : position of the text.
s : text
size : font size
prop : font property
Also see :doc:`/gallery/text_labels_and_annotations/demo_text_path`.
"""

if prop is None:
Expand Down