8000 Merge pull request #16647 from anntzer/anntut · matplotlib/matplotlib@25b80c3 · GitHub
[go: up one dir, main page]

Skip to content

Commit 25b80c3

Browse files
authored
Merge pull request #16647 from anntzer/anntut
Update annotation tutorial.
2 parents cfd5463 + e779850 commit 25b80c3

File tree

1 file changed

+85
-107
lines changed

1 file changed

+85
-107
lines changed

tutorials/text/annotations.py

Lines changed: 85 additions & 107 deletions
Original file line numberDiff line numberDiff line change
@@ -118,25 +118,20 @@
118118
119119
Annotate Text Arrow
120120
121+
`~.Axes.text` takes a *bbox* keyword argument, which draws a box around the
122+
text::
121123
122-
The :func:`~matplotlib.pyplot.text` function in the pyplot module (or
123-
`~.axes.Axes.text` method of the Axes class) takes *bbox* keyword argument, and
124-
when given, a box around the text is drawn. ::
125-
126-
bbox_props = dict(boxstyle="rarrow,pad=0.3", fc="cyan", ec="b", lw=2)
127-
t = ax.text(0, 0, "Direction", ha="center", va="center", rotation=45,
128-
size=15,
129-
bbox=bbox_props)
130-
124+
t = ax.text(
125+
0, 0, "Direction", ha="center", va="center", rotation=45, size=15,
126+
bbox=dict(boxstyle="rarrow,pad=0.3", fc="cyan", ec="b", lw=2))
131127
132128
The patch object associated with the text can be accessed by::
133129
134130
bb = t.get_bbox_patch()
135131
136-
The return value is an instance of FancyBboxPatch and the patch
137-
properties like facecolor, edgewidth, etc. can be accessed and
138-
modified as usual. To change the shape of the box, use the
139-
`~.FancyBboxPatch.set_boxstyle` method. ::
132+
The return value is a `.FancyBboxPatch`; patch properties
133+
(facecolor, edgewidth, etc.) can be accessed and modified as usual.
134+
`.FancyBboxPatch.set_boxstyle` sets the box shape::
140135
141136
bb.set_boxstyle("rarrow", pad=0.6)
142137
@@ -164,20 +159,16 @@
164159
165160
Fancybox Demo
166161
167-
168162
Note that the attribute arguments can be specified within the style
169163
name with separating comma (this form can be used as "boxstyle" value
170164
of bbox argument when initializing the text instance) ::
171165
172166
bb.set_boxstyle("rarrow,pad=0.6")
173167
174-
175168
Annotating with Arrow
176169
~~~~~~~~~~~~~~~~~~~~~
177170
178-
The :func:`~matplotlib.pyplot.annotate` function in the pyplot module
179-
(or `~.axes.Axes.annotate` method of the Axes class) is used to draw an arrow
180-
connecting two points on the plot. ::
171+
`~.Axes.annotate` draws an arrow connecting two points in an axes::
181172
182173
ax.annotate("Annotation",
183174
xy=(x1, y1), xycoords='data',
@@ -188,9 +179,9 @@
188179
with the text at *xytext* given in *textcoords*. Often, the
189180
annotated point is specified in the *data* coordinate and the annotating
190181
text in *offset points*.
191-
See :func:`~matplotlib.pyplot.annotate` for available coordinate systems.
182+
See `~.Axes.annotate` for available coordinate systems.
192183
193-
An arrow connecting two points (*xy* & *xytext*) can be optionally drawn by
184+
An arrow connecting *xy* to *xytext* can be optionally drawn by
194185
specifying the *arrowprops* argument. To draw only an arrow, use
195186
empty string as the first argument. ::
196187
@@ -208,20 +199,15 @@
208199
209200
Annotate Simple01
210201
211-
The arrow drawing takes a few steps.
212-
213-
1. a connecting path between two points are created. This is
214-
controlled by ``connectionstyle`` key value.
215-
216-
2. If patch object is given (*patchA* & *patchB*), the path is clipped to
217-
avoid the patch.
218-
219-
3. The path is further shrunk by given amount of pixels (*shrinkA*
220-
& *shrinkB*)
221-
222-
4. The path is transmuted to arrow patch, which is controlled by the
223-
``arrowstyle`` key value.
202+
The arrow is drawn as follows:
224203
204+
1. A path connecting the two points is created, as specified by the
205+
*connectionstyle* parameter.
206+
2. The path is clipped to avoid patches *patchA* and *patchB*, if these are
207+
set.
208+
3. The path is further shrunk by *shrinkA* and *shrinkB* (in pixels).
209+
4. The path is transmuted to an arrow patch, as specified by the *arrowstyle*
210+
parameter.
225211
226212
.. figure:: ../../gallery/userdemo/images/sphx_glr_annotate_explain_001.png
227213
:target: ../../gallery/userdemo/annotate_explain.html
@@ -250,7 +236,7 @@
250236
be used when the connecting path is a quadratic spline.
251237
252238
The behavior of each connection style is (limitedly) demonstrated in the
253-
example below. (Warning : The behavior of the ``bar`` style is currently not
239+
example below. (Warning: The behavior of the ``bar`` style is currently not
254240
well defined, it may be changed in the future).
255241
256242
.. figure:: ../../gallery/userdemo/images/sphx_glr_connectionstyle_demo_001.png
@@ -429,89 +415,86 @@
429415
described in :ref:`annotations-tutorial`. For an advanced user who wants
430416
more control, it supports a few other options.
431417
432-
1. :class:`~matplotlib.transforms.Transform` instance. For example, ::
418+
1. A `.Transform` instance. For example, ::
433419
434-
ax.annotate("Test", xy=(0.5, 0.5), xycoords=ax.transAxes)
420+
ax.annotate("Test", xy=(0.5, 0.5), xycoords=ax.transAxes)
435421
436-
is identical to ::
422+
is identical to ::
437423
438-
ax.annotate("Test", xy=(0.5, 0.5), xycoords="axes fraction")
424+
ax.annotate("Test", xy=(0.5, 0.5), xycoords="axes fraction")
439425
440-
With this, you can annotate a point in other axes. ::
426+
This allows annotating a point in another axes::
441427
442-
ax1, ax2 = subplot(121), subplot(122)
443-
ax2.annotate("Test", xy=(0.5, 0.5), xycoords=ax1.transData,
444-
xytext=(0.5, 0.5), textcoords=ax2.transData,
445-
arrowprops=dict(arrowstyle="->"))
428+
ax1, ax2 = subplot(121), subplot(122)
429+
ax2.annotate("Test", xy=(0.5, 0.5), xycoords=ax1.transData,
430+
xytext=(0.5, 0.5), textcoords=ax2.transData,
431+
arrowprops=dict(arrowstyle="->"))
446432
447-
2. :class:`~matplotlib.artist.Artist` instance. The *xy* value (or
448-
*xytext*) is interpreted as a fractional coordinate of the bbox
449-
(return value of *get_window_extent*) of the artist. ::
433+
2. An `.Artist` instance. The *xy* value (or *xytext*) is interpreted as a
434+
fractional coordinate of the bbox (return value of *get_window_extent*) of
435+
the artist::
450436
451-
an1 = ax.annotate("Test 1", xy=(0.5, 0.5), xycoords="data",
452-
va="center", ha="center",
453-
bbox=dict(boxstyle="round", fc="w"))
454-
an2 = ax.annotate("Test 2", xy=(1, 0.5), xycoords=an1, # (1, 0.5) of the an1's bbox
455-
xytext=(30, 0), textcoords="offset points",
456-
va="center", ha="left",
457-
bbox=dict(boxstyle="round", fc="w"),
458-
arrowprops=dict(arrowstyle="->"))
437+
an1 = ax.annotate("Test 1", xy=(0.5, 0.5), xycoords="data",
438+
va="center", ha="center",
439+
bbox=dict(boxstyle="round", fc="w"))
440+
an2 = ax.annotate("Test 2", xy=(1, 0.5), xycoords=an1, # (1, 0.5) of the an1's bbox
441+
xytext=(30, 0), textcoords="offset points",
442+
va="center", ha="left",
443+
bbox=dict(boxstyle="round", fc="w"),
444+
arrowprops=dict(arrowstyle="->"))
459445
460-
.. figure:: ../../gallery/userdemo/images/sphx_glr_annotate_simple_coord01_001.png
461-
:target: ../../gallery/userdemo/annotate_simple_coord01.html
462-
:align: center
463-
:scale: 50
446+
.. figure:: ../../gallery/userdemo/images/sphx_glr_annotate_simple_coord01_001.png
447+
:target: ../../gallery/userdemo/annotate_simple_coord01.html
448+
:align: center
449+
:scale: 50
464450
465-
Annotation with Simple Coordinates
451+
Annotation with Simple Coordinates
466452
467-
Note that it is your responsibility that the extent of the
468-
coordinate artist (*an1* in above example) is determined before *an2*
469-
gets drawn. In most cases, it means that *an2* needs to be drawn
470-
later than *an1*.
453+
Note that you must ensure that the extent of the coordinate artist (*an1* in
454+
above example) is determined before *an2* gets drawn. Usually, this means
455+
that *an2* needs to be drawn after *an1*.
471456
457+
3. A callable object that takes the renderer instance as single argument, and
458+
returns either a `.Transform` or a `.BboxBase`. The return value is then
459+
handled as in (1), for transforms, or in (2), for bboxes. For example, ::
472460
473-
3. A callable object that returns an instance of either
474-
:class:`~matplotlib.transforms.BboxBase` or
475-
:class:`~matplotlib.transforms.Transform`. If a transform is
476-
returned, it is the same as 1 and if a bbox is returned, it is the same
477-
as 2. The callable object should take a single argument of the
478-
renderer instance. For example, the following two commands give
479-
identical results ::
461+
an2 = ax.annotate("Test 2", xy=(1, 0.5), xycoords=an1,
462+
xytext=(30, 0), textcoords="offset points")
480463
481-
an2 = ax.annotate("Test 2", xy=(1, 0.5), xycoords=an1,
482-
xytext=(30, 0), textcoords="offset points")
483-
an2 = ax.annotate("Test 2", xy=(1, 0.5), xycoords=an1.get_window_extent,
484-
xytext=(30, 0), textcoords="offset points")
464+
is identical to::
485465
466+
an2 = ax.annotate("Test 2", xy=(1, 0.5), xycoords=an1.get_window_extent,
467+
xytext=(30, 0), textcoords="offset points")
486468
487-
4. A tuple of two coordinate specifications. The first item is for the
488-
x-coordinate and the second is for the y-coordinate. For example, ::
469+
4. A pair of coordinate specifications -- the first for the x-coordinate, and
470+
the second is for the y-coordinate; e.g. ::
489471
490-
annotate("Test", xy=(0.5, 1), xycoords=("data", "axes fraction"))
472+
annotate("Test", xy=(0.5, 1), xycoords=("data", "axes fraction"))
491473
492-
0.5 is in data coordinates, and 1 is in normalized axes coordinates.
493-
You may use an artist or transform as with a tuple. For example,
474+
Here, 0.5 is in data coordinates, and 1 is in normalized axes coordinates.
475+
Each of the coordinate specifications can also be an artist or a transform.
476+
For example,
494477
495-
.. figure:: ../../gallery/userdemo/images/sphx_glr_annotate_simple_coord02_001.png
496-
:target: ../../gallery/userdemo/annotate_simple_coord02.html
497-
:align: center
498-
:scale: 50
478+
.. figure:: ../../gallery/userdemo/images/sphx_glr_annotate_simple_coord02_001.png
479+
:target: ../../gallery/userdemo/annotate_simple_coord02.html
480+
:align: center
481+
:scale: 50
499482
500-
Annotation with Simple Coordinates 2
483+
Annotation with Simple Coordinates 2
501484
502-
5. Sometimes, you want your annotation with some "offset points", not from the
503-
annotated point but from some other point.
504-
:class:`~matplotlib.text.OffsetFrom` is a helper class for such cases.
485+
5. Sometimes, you want your annotation with some "offset points", not from the
486+
annotated point but from some other point. `.text.OffsetFrom` is a helper
487+
for such cases.
505488
506-
.. figure:: ../../gallery/userdemo/images/sphx_glr_annotate_simple_coord03_001.png
507-
:target: ../../gallery/userdemo/annotate_simple_coord03.html
508-
:align: center
509-
:scale: 50
489+
.. figure:: ../../gallery/userdemo/images/sphx_glr_annotate_simple_coord03_001.png
490+
:target: ../../gallery/userdemo/annotate_simple_coord03.html
491+
:align: center
492+
:scale: 50
510493
511-
Annotation with Simple Coordinates 3
494+
Annotation with Simple Coordinates 3
512495
513-
You may take a look at this example
514-
:doc:`/gallery/text_labels_and_annotations/annotation_demo`.
496+
You may take a look at this example
497+
:doc:`/gallery/text_labels_and_annotations/annotation_demo`.
515498
516499
Using ConnectionPatch
517500
~~~~~~~~~~~~~~~~~~~~~
@@ -522,8 +505,8 @@
522505
523506
from matplotlib.patches import ConnectionPatch
524507
xy = (0.2, 0.2)
525-
con = ConnectionPatch(xyA=xy, xyB=xy, coordsA="data", coordsB="data",
526-
axesA=ax1, axesB=ax2)
508+
con = ConnectionPatch(xyA=xy, coordsA=ax1.transData,
509+
xyB=xy, coordsB=ax2.transData)
527510
ax2.add_artist(con)
528511
529512
The above code connects point *xy* in the data coordinates of ``ax1`` to
@@ -536,23 +519,19 @@
536519
537520
Connect Simple01
538521
539-
540-
While the ConnectionPatch instance can be added to any axes, you may want to add
541-
it to the axes that is latest in drawing order to prevent overlap by other
542-
axes.
543-
522+
While the ConnectionPatch instance can be added to any axes, you may want to
523+
add it to the axes that is drawn last, to prevent it from being covered by
524+
other axes.
544525
545526
Advanced Topics
546527
---------------
547528
548529
Zoom effect between Axes
549530
~~~~~~~~~~~~~~~~~~~~~~~~
550531
551-
``mpl_toolkits.axes_grid1.inset_locator`` defines some patch classes useful
552-
for interconnecting two axes. Understanding the code requires some
553-
knowledge of how mpl's transform works. But, utilizing it will be
554-
straight forward.
555-
532+
``mpl_toolkits.axes_grid1.inset_locator`` defines some patch classes useful for
533+
interconnecting two axes. Understanding the code requires some knowledge of
534+
Matplotlib's transform system.
556535
557536
.. figure:: ../../gallery/subplots_axes_and_figures/images/sphx_glr_axes_zoom_effect_001.png
558537
:target: ../../gallery/subplots_axes_and_figures/axes_zoom_effect.html
@@ -561,7 +540,6 @@
561540
562541
Axes Zoom Effect
563542
564-
565543
Define Custom BoxStyle
566544
~~~~~~~~~~~~~~~~~~~~~~
567545

0 commit comments

Comments
 (0)
0