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

Skip to content

Commit d779b8b

Browse files
authored
Merge pull request #12676 from ImportanceOfBeingErnest/document-textpath
Doc: document textpath module
2 parents 1f0ed58 + e9101f0 commit d779b8b

File tree

5 files changed

+94
-19
lines changed

5 files changed

+94
-19
lines changed

doc/api/api_overview.rst

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,8 @@ Further reading:
3838
- `matplotlib.axes.Axes` and `matplotlib.figure.Figure` for an overview of
3939
plotting functions.
4040
- Most of the :ref:`examples <examples-index>` use the object-oriented approach
41-
(except for the pyplot section).
41+
(except for the pyplot section)
42+
- The list of :doc:`matplotlib modules </api/index>`.
4243

4344

4445
The pylab API (disapproved)

doc/api/index.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ Modules
5555
style_api.rst
5656
table_api.rst
5757
text_api.rst
58+
textpath_api.rst
5859
ticker_api.rst
5960
tight_layout_api.rst
6061
transformations.rst

doc/api/textpath_api.rst

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
********
2+
textpath
3+
********
4+
5+
6+
:mod:`matplotlib.textpath`
7+
==========================
8+
9+
.. automodule:: matplotlib.textpath
10+
:members:
11+
:undoc-members:
12+
:show-inheritance:

examples/text_labels_and_annotations/demo_text_path.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@
33
Demo Text Path
44
==============
55
6+
Use a text as `Path`. The tool that allows for such conversion is a
7+
`~matplotlib.textpath.TextPath`. The resulting path can be employed
8+
e.g. as a clip path for an image.
69
"""
710

811
import matplotlib.pyplot as plt

lib/matplotlib/textpath.py

Lines changed: 76 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -106,22 +106,47 @@ def get_text_width_height_descent(self, s, prop, ismath):
106106

107107
def get_text_path(self, prop, s, ismath=False, usetex=False):
108108
"""
109-
convert text *s* to path (a tuple of vertices and codes for
109+
Convert text *s* to path (a tuple of vertices and codes for
110110
matplotlib.path.Path).
111111
112-
*prop*
113-
font property
112+
Parameters
113+
----------
114114
115-
*s*
116-
text to be converted
115+
prop : `matplotlib.font_manager.FontProperties` instance
116+
The font properties for the text.
117117
118-
*usetex*
119-
If True, use matplotlib usetex mode.
118+
s : str
119+
The text to be converted.
120120
121-
*ismath*
122-
If True, use mathtext parser. Effective only if usetex == False.
121+
usetex : bool, optional
122+
Whether to use tex rendering. Defaults to ``False``.
123123
124+
ismath : bool, optional
125+
If True, use mathtext parser. Effective only if
126+
``usetex == False``.
124127
128+
Returns
129+
-------
130+
131+
verts, codes : tuple of lists
132+
*verts* is a list of numpy arrays containing the x and y
133+
coordinates of the vertices. *codes* is a list of path codes.
134+
135+
Examples
136+
--------
137+
138+
Create a list of vertices and codes from a text, and create a `Path`
139+
from those::
140+
141+
from matplotlib.path import Path
142+
from matplotlib.textpath import TextToPath
143+
from matplotlib.font_manager import FontProperties
144+
145+
fp = FontProperties(family="Humor Sans", style="italic")
146+
verts, codes = TextToPath().get_text_path(fp, "ABC")
147+
path = Path(verts, codes, closed=False)
148+
149+
Also see `TextPath` for a more direct way to create a path from a text.
125150
"""
126151
if not usetex:
127152
if not ismath:
@@ -393,16 +418,49 @@ class TextPath(Path):
393418
def __init__(self, xy, s, size=None, prop=None,
394419
_interpolation_steps=1, usetex=False,
395420
*kl, **kwargs):
396-
"""
397-
Create a path from the text. No support for TeX yet. Note that
398-
it simply is a path, not an artist. You need to use the
399-
PathPatch (or other artists) to draw this path onto the
400-
canvas.
421+
r"""
422+
Create a path from the text. Note that it simply is a path,
423+
not an artist. You need to use the `~.PathPatch` (or other artists)
424+
to draw this path onto the canvas.
425+
426+
Parameters
427+
----------
428+
429+
xy : tuple or array of two float values
430+
Position of the text. For no offset, use ``xy=(0, 0)``.
431+
432+
s : str
433+
The text to convert to a path.
434+
435+
size : float, optional
436+
Font size in points. Defaults to the size specified via the font
437+
properties *prop*.
438+
439+
prop : `matplotlib.font_manager.FontProperties`, optional
440+
Font property. If not provided, will use a default
441+
``FontProperties`` with parameters from the
442+
:ref:`rcParams <matplotlib-rcparams>`.
443+
444+
_interpolation_steps : integer, optional
445+
(Currently ignored)
446+
447+
usetex : bool, optional
448+
Whether to use tex rendering. Defaults to ``False``.
449+
450+
Examples
451+
--------
452+
453+
The following creates a path from the string "ABC" with Helvetica
454+
font face; and another path from the latex fraction 1/2::
455+
456+
from matplotlib.textpath import TextPath
457+
from matplotlib.font_manager import FontProperties
458+
459+
fp = FontProperties(family="Helvetica", style="italic")
460+
path1 = TextPath((12,12), "ABC", size=12, prop=fp)
461+
path2 = TextPath((0,0), r"$\frac{1}{2}$", size=12, usetex=True)
401462
402-
xy : position of the text.
403-
s : text
404-
size : font size
405-
prop : font property
463+
Also see :doc:`/gallery/text_labels_and_annotations/demo_text_path`.
406464
"""
407465

408466
if prop is None:

0 commit comments

Comments
 (0)