|
118 | 118 |
|
119 | 119 | Annotate Text Arrow |
120 | 120 |
|
| 121 | +`~.Axes.text` takes a *bbox* keyword argument, which draws a box around the |
| 122 | +text:: |
121 | 123 |
|
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)) |
131 | 127 |
|
132 | 128 | The patch object associated with the text can be accessed by:: |
133 | 129 |
|
134 | 130 | bb = t.get_bbox_patch() |
135 | 131 |
|
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:: |
140 | 135 |
|
141 | 136 | bb.set_boxstyle("rarrow", pad=0.6) |
142 | 137 |
|
|
164 | 159 |
|
165 | 160 | Fancybox Demo |
166 | 161 |
|
167 | | -
|
168 | 162 | Note that the attribute arguments can be specified within the style |
169 | 163 | name with separating comma (this form can be used as "boxstyle" value |
170 | 164 | of bbox argument when initializing the text instance) :: |
171 | 165 |
|
172 | 166 | bb.set_boxstyle("rarrow,pad=0.6") |
173 | 167 |
|
174 | | -
|
175 | 168 | Annotating with Arrow |
176 | 169 | ~~~~~~~~~~~~~~~~~~~~~ |
177 | 170 |
|
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:: |
181 | 172 |
|
182 | 173 | ax.annotate("Annotation", |
183 | 174 | xy=(x1, y1), xycoords='data', |
|
188 | 179 | with the text at *xytext* given in *textcoords*. Often, the |
189 | 180 | annotated point is specified in the *data* coordinate and the annotating |
190 | 181 | text in *offset points*. |
191 | | -See :func:`~matplotlib.pyplot.annotate` for available coordinate systems. |
| 182 | +See `~.Axes.annotate` for available coordinate systems. |
192 | 183 |
|
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 |
194 | 185 | specifying the *arrowprops* argument. To draw only an arrow, use |
195 | 186 | empty string as the first argument. :: |
196 | 187 |
|
|
208 | 199 |
|
209 | 200 | Annotate Simple01 |
210 | 201 |
|
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: |
224 | 203 |
|
| 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. |
225 | 211 |
|
226 | 212 | .. figure:: ../../gallery/userdemo/images/sphx_glr_annotate_explain_001.png |
227 | 213 | :target: ../../gallery/userdemo/annotate_explain.html |
|
250 | 236 | be used when the connecting path is a quadratic spline. |
251 | 237 |
|
252 | 238 | 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 |
254 | 240 | well defined, it may be changed in the future). |
255 | 241 |
|
256 | 242 | .. figure:: ../../gallery/userdemo/images/sphx_glr_connectionstyle_demo_001.png |
|
429 | 415 | described in :ref:`annotations-tutorial`. For an advanced user who wants |
430 | 416 | more control, it supports a few other options. |
431 | 417 |
|
432 | | - 1. :class:`~matplotlib.transforms.Transform` instance. For example, :: |
| 418 | +1. A `.Transform` instance. For example, :: |
433 | 419 |
|
434 | | - ax.annotate("Test", xy=(0.5, 0.5), xycoords=ax.transAxes) |
| 420 | + ax.annotate("Test", xy=(0.5, 0.5), xycoords=ax.transAxes) |
435 | 421 |
|
436 | | - is identical to :: |
| 422 | + is identical to :: |
437 | 423 |
|
438 | | - ax.annotate("Test", xy=(0.5, 0.5), xycoords="axes fraction") |
| 424 | + ax.annotate("Test", xy=(0.5, 0.5), xycoords="axes fraction") |
439 | 425 |
|
440 | | - With this, you can annotate a point in other axes. :: |
| 426 | + This allows annotating a point in another axes:: |
441 | 427 |
|
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="->")) |
446 | 432 |
|
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:: |
450 | 436 |
|
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="->")) |
459 | 445 |
|
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 |
464 | 450 |
|
465 | | - Annotation with Simple Coordinates |
| 451 | + Annotation with Simple Coordinates |
466 | 452 |
|
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*. |
471 | 456 |
|
| 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, :: |
472 | 460 |
|
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") |
480 | 463 |
|
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:: |
485 | 465 |
|
| 466 | + an2 = ax.annotate("Test 2", xy=(1, 0.5), xycoords=an1.get_window_extent, |
| 467 | + xytext=(30, 0), textcoords="offset points") |
486 | 468 |
|
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. :: |
489 | 471 |
|
490 | | - annotate("Test", xy=(0.5, 1), xycoords=("data", "axes fraction")) |
| 472 | + annotate("Test", xy=(0.5, 1), xycoords=("data", "axes fraction")) |
491 | 473 |
|
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, |
494 | 477 |
|
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 |
499 | 482 |
|
500 | | - Annotation with Simple Coordinates 2 |
| 483 | + Annotation with Simple Coordinates 2 |
501 | 484 |
|
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. |
505 | 488 |
|
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 |
510 | 493 |
|
511 | | - Annotation with Simple Coordinates 3 |
| 494 | + Annotation with Simple Coordinates 3 |
512 | 495 |
|
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`. |
515 | 498 |
|
516 | 499 | Using ConnectionPatch |
517 | 500 | ~~~~~~~~~~~~~~~~~~~~~ |
|
522 | 505 |
|
523 | 506 | from matplotlib.patches import ConnectionPatch |
524 | 507 | 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) |
527 | 510 | ax2.add_artist(con) |
528 | 511 |
|
529 | 512 | The above code connects point *xy* in the data coordinates of ``ax1`` to |
|
536 | 519 |
|
537 | 520 | Connect Simple01 |
538 | 521 |
|
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. |
544 | 525 |
|
545 | 526 | Advanced Topics |
546 | 527 | --------------- |
547 | 528 |
|
548 | 529 | Zoom effect between Axes |
549 | 530 | ~~~~~~~~~~~~~~~~~~~~~~~~ |
550 | 531 |
|
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. |
556 | 535 |
|
557 | 536 | .. figure:: ../../gallery/subplots_axes_and_figures/images/sphx_glr_axes_zoom_effect_001.png |
558 | 537 | :target: ../../gallery/subplots_axes_and_figures/axes_zoom_effect.html |
|
561 | 540 |
|
562 | 541 | Axes Zoom Effect |
563 | 542 |
|
564 | | -
|
565 | 543 | Define Custom BoxStyle |
566 | 544 | ~~~~~~~~~~~~~~~~~~~~~~ |
567 | 545 |
|
|
0 commit comments