|
13 | 13 | ############################################################################### |
14 | 14 | # .. _annotations-tutorial: |
15 | 15 | # |
16 | | -# Basic annotation |
| 16 | +# Basic Annotation |
17 | 17 | # ---------------- |
18 | 18 | # |
19 | 19 | # The uses of the basic :func:`~matplotlib.pyplot.text` will place text |
|
47 | 47 | # 'data' use the axes data coordinate system |
48 | 48 | # ================== ======================================================== |
49 | 49 | # |
| 50 | +# The following strings are also valid arguments for *textcoords* |
| 51 | +# |
| 52 | +# ================== ======================================================== |
| 53 | +# argument coordinate system |
| 54 | +# ================== ======================================================== |
| 55 | +# 'offset points' offset (in points) from the xy value |
| 56 | +# 'offset pixels' offset (in pixels) from the xy value |
| 57 | +# ================== ======================================================== |
| 58 | +# |
| 59 | +# For physical coordinate systems (points or pixels) the origin is the |
| 60 | +# bottom-left of the figure or axes. Points are |
| 61 | +# `typographic points <https://en.wikipedia.org/wiki/Point_(typography)>`_ |
| 62 | +# meaning that they are a physical unit measuring 1/72 of an inch. Points and |
| 63 | +# pixels are discussed in further detail in :ref:`transforms-fig-scale-dpi`. |
| 64 | +# |
| 65 | +# .. _annotation_basic: |
| 66 | +# |
| 67 | +# Annotating with Text |
| 68 | +# ~~~~~~~~~~~~~~~~~~~~ |
| 69 | +# |
50 | 70 | # For example to place the text coordinates in fractional axes |
51 | 71 | # coordinates, one could do:: |
| 72 | + |
| 73 | +fig, ax = plt.subplots() |
| 74 | +ax.plot(3, 1) |
| 75 | +ax.annotate('local max', xy=(3, 1), xycoords='data', |
| 76 | + xytext=(0.8, 0.95), textcoords='axes fraction', |
| 77 | + arrowprops=dict(facecolor='black', shrink=0.05), |
| 78 | + horizontalalignment='right', verticalalignment='top') |
| 79 | + |
| 80 | +################################################################### |
52 | 81 | # |
53 | | -# ax.annotate('local max', xy=(3, 1), xycoords='data', |
54 | | -# xytext=(0.8, 0.95), textcoords='axes fraction', |
55 | | -# arrowprops=dict(facecolor='black', shrink=0.05), |
56 | | -# horizontalalignment='right', verticalalignment='top', |
57 | | -# ) |
| 82 | +# .. _annotation_with_arrow: |
58 | 83 | # |
59 | | -# For physical coordinate systems (points or pixels) the origin is the |
60 | | -# bottom-left of the figure or axes. |
| 84 | +# Annotating With Arrow |
| 85 | +# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
61 | 86 | # |
62 | 87 | # Optionally, you can enable drawing of an arrow from the text to the annotated |
63 | 88 | # point by giving a dictionary of arrow properties in the optional keyword |
64 | 89 | # argument *arrowprops*. |
65 | 90 | # |
66 | | -# |
67 | 91 | # ==================== ===================================================== |
68 | 92 | # *arrowprops* key description |
69 | 93 | # ==================== ===================================================== |
|
77 | 101 | # e.g., ``facecolor`` |
78 | 102 | # ==================== ===================================================== |
79 | 103 | # |
80 | | -# |
81 | | -# In the example below, the *xy* point is in native coordinates |
82 | | -# (*xycoords* defaults to 'data'). For a polar axes, this is in |
83 | | -# (theta, radius) space. The text in this example is placed in the |
| 104 | +# In the example below, the *xy* point is in the default coordinate system for |
| 105 | +# the axes projection (*xycoords* defaults to 'data'). For a polar axes, this |
| 106 | +# is in (theta, radius) space. The text in this example is placed in the |
84 | 107 | # fractional figure coordinate system. :class:`matplotlib.text.Text` |
85 | 108 | # keyword arguments like *horizontalalignment*, *verticalalignment* and |
86 | 109 | # *fontsize* are passed from `~matplotlib.axes.Axes.annotate` to the |
|
98 | 121 | # Do not proceed unless you have already read :ref:`annotations-tutorial`, |
99 | 122 | # :func:`~matplotlib.pyplot.text` and :func:`~matplotlib.pyplot.annotate`! |
100 | 123 | # |
| 124 | +# .. _annotations-offset-text: |
| 125 | +# |
| 126 | +# Placing Text Annotations relative to data |
| 127 | +# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 128 | +# Annotations can be positioned at a relative offset to the *xy* input to |
| 129 | +# annotation by setting the *textcoords* kwarg to one of the offset options |
| 130 | +# (points or pixels). |
| 131 | + |
| 132 | +fig, ax = plt.subplots() |
| 133 | +x = [1, 3, 5, 7, 9] |
| 134 | +y = [2, 4, 6, 8, 10] |
| 135 | +annotations = ["A", "B", "C", "D", "E"] |
| 136 | +ax.scatter(x, y, s=20) |
| 137 | + |
| 138 | +for xi, yi, text in zip(x, y, annotations): |
| 139 | + ax.annotate(text, xy=(xi, yi), xycoords='data', |
| 140 | + xytext=(1.5, 1.5), textcoords='offset points') |
| 141 | + |
| 142 | +############################################################################### |
| 143 | +# The annotations are offset 1.5 points (1.5*1/72 inches) from the *xy* values. |
101 | 144 | # |
102 | 145 | # .. _plotting-guide-annotation: |
103 | 146 | # |
104 | | -# Advanced Annotations |
| 147 | +# Advanced Annotation |
105 | 148 | # -------------------- |
106 | 149 | # |
107 | | -# Annotating with Text with Box |
| 150 | +# Annotating with Text in Box |
108 | 151 | # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
109 | 152 | # |
110 | 153 | # Let's start with a simple example. |
|
157 | 200 | # |
158 | 201 | # bb.set_boxstyle("rarrow,pad=0.6") |
159 | 202 | # |
160 | | -# Annotating with Arrow |
161 | | -# ~~~~~~~~~~~~~~~~~~~~~ |
| 203 | +# Customizing Annotation Arrow |
| 204 | +# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
162 | 205 | # |
163 | 206 | # `~.Axes.annotate` draws an arrow connecting two points in an Axes:: |
164 | 207 | # |
|
359 | 402 | # Note that unlike the legend, the ``bbox_transform`` is set |
360 | 403 | # to IdentityTransform by default. |
361 | 404 | # |
| 405 | +# Define custom BoxStyle |
| 406 | +# ~~~~~~~~~~~~~~~~~~~~~~ |
| 407 | +# |
| 408 | +# You can use a custom box style. The value for the ``boxstyle`` can be a |
| 409 | +# callable object in the following forms.:: |
| 410 | +# |
| 411 | +# def __call__(self, x0, y0, width, height, mutation_size, |
| 412 | +# aspect_ratio=1.): |
| 413 | +# ''' |
| 414 | +# Given the location and size of the box, return the path of |
| 415 | +# the box around it. |
| 416 | +# |
| 417 | +# - *x0*, *y0*, *width*, *height* : location and size of the box |
| 418 | +# - *mutation_size* : a reference scale for the mutation. |
| 419 | +# - *aspect_ratio* : aspect-ratio for the mutation. |
| 420 | +# ''' |
| 421 | +# path = ... |
| 422 | +# return path |
| 423 | +# |
| 424 | +# Here is a complete example. |
| 425 | +# |
| 426 | +# .. figure:: ../../gallery/userdemo/images/sphx_glr_custom_boxstyle01_001.png |
| 427 | +# :target: ../../gallery/userdemo/custom_boxstyle01.html |
| 428 | +# :align: center |
| 429 | +# |
| 430 | +# Similarly, you can define a custom ConnectionStyle and a custom ArrowStyle. |
| 431 | +# See the source code of ``lib/matplotlib/patches.py`` and check |
| 432 | +# how each style class is defined. |
| 433 | +# |
| 434 | +# .. _annotating_coordinate systems: |
| 435 | +# |
362 | 436 | # Coordinate systems for Annotations |
363 | | -# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 437 | +# ---------------------------------- |
364 | 438 | # |
365 | 439 | # Matplotlib Annotations support several types of coordinates. Some are |
366 | 440 | # described in :ref:`annotations-tutorial`; more advanced options are |
|
462 | 536 | # and is also necessary if using :doc:`constrained_layout |
463 | 537 | # </tutorials/intermediate/constrainedlayout_guide>` for positioning the axes. |
464 | 538 | # |
465 | | -# Advanced Topics |
466 | | -# --------------- |
467 | | -# |
468 | 539 | # Zoom effect between Axes |
469 | 540 | # ~~~~~~~~~~~~~~~~~~~~~~~~ |
470 | 541 | # |
|
475 | 546 | # .. figure:: ../../gallery/subplots_axes_and_figures/images/sphx_glr_axes_zoom_effect_001.png |
476 | 547 | # :target: ../../gallery/subplots_axes_and_figures/axes_zoom_effect.html |
477 | 548 | # :align: center |
478 | | -# |
479 | | -# Define Custom BoxStyle |
480 | | -# ~~~~~~~~~~~~~~~~~~~~~~ |
481 | | -# |
482 | | -# You can use a custom box style. The value for the ``boxstyle`` can be a |
483 | | -# callable object in the following forms.:: |
484 | | -# |
485 | | -# def __call__(self, x0, y0, width, height, mutation_size, |
486 | | -# aspect_ratio=1.): |
487 | | -# ''' |
488 | | -# Given the location and size of the box, return the path of |
489 | | -# the box around it. |
490 | | -# |
491 | | -# - *x0*, *y0*, *width*, *height* : location and size of the box |
492 | | -# - *mutation_size* : a reference scale for the mutation. |
493 | | -# - *aspect_ratio* : aspect-ratio for the mutation. |
494 | | -# ''' |
495 | | -# path = ... |
496 | | -# return path |
497 | | -# |
498 | | -# Here is a complete example. |
499 | | -# |
500 | | -# .. figure:: ../../gallery/userdemo/images/sphx_glr_custom_boxstyle01_001.png |
501 | | -# :target: ../../gallery/userdemo/custom_boxstyle01.html |
502 | | -# :align: center |
503 | | -# |
504 | | -# Similarly, you can define a custom ConnectionStyle and a custom ArrowStyle. |
505 | | -# See the source code of ``lib/matplotlib/patches.py`` and check |
506 | | -# how each style class is defined. |
|
0 commit comments