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

Skip to content

Commit db4afbb

Browse files
story645jklymaktimhoffm
committed
added offset section and restructured annotations tutorial
Co-authored-by: Jody Klymak <[email protected]> Co-authored-by: Tim Hoffmann <[email protected]>
1 parent a68f21f commit db4afbb

File tree

1 file changed

+98
-57
lines changed

1 file changed

+98
-57
lines changed

tutorials/text/annotations.py

Lines changed: 98 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -47,22 +47,45 @@
4747
# 'data' use the axes data coordinate system
4848
# ================== ========================================================
4949
#
50-
# For example to place the text coordinates in fractional axes
51-
# coordinates, one could do::
50+
# The following strings are also valid arguments for *textcoords*
5251
#
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-
# )
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+
# ================== ========================================================
5858
#
5959
# For physical coordinate systems (points or pixels) the origin is the
60-
# bottom-left of the figure or axes.
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`.
6164
#
62-
# Optionally, you can enable drawing of an arrow from the text to the annotated
63-
# point by giving a dictionary of arrow properties in the optional keyword
64-
# argument *arrowprops*.
65+
# .. _annotation_data:
6566
#
67+
# Annotating data
68+
# ~~~~~~~~~~~~~~~
69+
#
70+
# For example to place the text coordinates in fractional axes
71+
# coordinates, one could do ::
72+
73+
fig, ax = plt.subplots()
74+
ax.scatter(3, 1, s=20)
75+
ax.annotate('local max', xy=(3, 1), xycoords='data',
76+
xytext=(0.8, 0.95), textcoords='axes fraction',
77+
horizontalalignment='right', verticalalignment='top')
78+
79+
###################################################################
80+
#
81+
# .. _annotation_with_arrow:
82+
#
83+
# Annotating with arrows
84+
# ~~~~~~~~~~~~~~~~~~~~~~
85+
#
86+
# You can enable drawing of an arrow from the text to the annotated point
87+
# by giving a dictionary of arrow properties in the optional keyword
88+
# argument *arrowprops*.
6689
#
6790
# ==================== =====================================================
6891
# *arrowprops* key description
@@ -77,10 +100,9 @@
77100
# e.g., ``facecolor``
78101
# ==================== =====================================================
79102
#
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
103+
# In the example below, the *xy* point is in the data coordinate system
104+
# since *xycoords* defaults to 'data'. For a polar axes, this is in
105+
# (theta, radius) space. The text in this example is placed in the
84106
# fractional figure coordinate system. :class:`matplotlib.text.Text`
85107
# keyword arguments like *horizontalalignment*, *verticalalignment* and
86108
# *fontsize* are passed from `~matplotlib.axes.Axes.annotate` to the
@@ -98,14 +120,34 @@
98120
# Do not proceed unless you have already read :ref:`annotations-tutorial`,
99121
# :func:`~matplotlib.pyplot.text` and :func:`~matplotlib.pyplot.annotate`!
100122
#
123+
# .. _annotations-offset-text:
124+
#
125+
# Placing text annotations relative to data
126+
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
127+
# Annotations can be positioned at a relative offset to the *xy* input to
128+
# annotation by setting the *textcoords* kwarg to 'offset points' or
129+
# 'offset pixels'.
130+
131+
fig, ax = plt.subplots()
132+
x = [1, 3, 5, 7, 9]
133+
y = [2, 4, 6, 8, 10]
134+
annotations = ["A", "B", "C", "D", "E"]
135+
ax.scatter(x, y, s=20)
136+
137+
for xi, yi, text in zip(x, y, annotations):
138+
ax.annotate(text, xy=(xi, yi), xycoords='data',
139+
xytext=(1.5, 1.5), textcoords='offset points')
140+
141+
###############################################################################
142+
# The annotations are offset 1.5 points (1.5*1/72 inches) from the *xy* values.
101143
#
102144
# .. _plotting-guide-annotation:
103145
#
104-
# Advanced Annotations
146+
# Advanced annotation
105147
# --------------------
106148
#
107-
# Annotating with Text with Box
108-
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
149+
# Annotating with text in box
150+
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~
109151
#
110152
# Let's start with a simple example.
111153
#
@@ -116,9 +158,9 @@
116158
# `~.Axes.text` takes a *bbox* keyword argument, which draws a box around the
117159
# text::
118160
#
119-
# t = ax.text(
120-
# 0, 0, "Direction", ha="center", va="center", rotation=45, size=15,
121-
# bbox=dict(boxstyle="rarrow,pad=0.3", fc="cyan", ec="b", lw=2))
161+
# t = ax.text(0, 0, "Direction", ha="center", va="center", rotation=45,
162+
# size=15, bbox=dict(boxstyle="rarrow,pad=0.3", fc="cyan", ec="b", lw=2)
163+
# )
122164
#
123165
# The patch object associated with the text can be accessed by::
124166
#
@@ -157,8 +199,8 @@
157199
#
158200
# bb.set_boxstyle("rarrow,pad=0.6")
159201
#
160-
# Annotating with Arrow
161-
# ~~~~~~~~~~~~~~~~~~~~~
202+
# Customizing annotation arrow
203+
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
162204
#
163205
# `~.Axes.annotate` draws an arrow connecting two points in an Axes::
164206
#
@@ -359,8 +401,39 @@
359401
# Note that unlike the legend, the ``bbox_transform`` is set
360402
# to IdentityTransform by default.
361403
#
362-
# Coordinate systems for Annotations
363-
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
404+
# Define custom box style
405+
# ~~~~~~~~~~~~~~~~~~~~~~
406+
#
407+
# You can use a custom box style. The value for the ``boxstyle`` can be a
408+
# callable object in the following forms. ::
409+
#
410+
# def __call__(self, x0, y0, width, height, mutation_size,
411+
# aspect_ratio=1.):
412+
# '''
413+
# Given the location and size of the box, return the path of
414+
# the box around it.
415+
#
416+
# - *x0*, *y0*, *width*, *height* : location and size of the box
417+
# - *mutation_size* : a reference scale for the mutation.
418+
# - *aspect_ratio* : aspect-ratio for the mutation.
419+
# '''
420+
# path = ...
421+
# return path
422+
#
423+
# Here is a complete example.
424+
#
425+
# .. figure:: ../../gallery/userdemo/images/sphx_glr_custom_boxstyle01_001.png
426+
# :target: ../../gallery/userdemo/custom_boxstyle01.html
427+
# :align: center
428+
#
429+
# Similarly, you can define a custom ConnectionStyle and a custom ArrowStyle.
430+
# See the source code of ``lib/matplotlib/patches.py`` and check
431+
# how each style class is defined.
432+
#
433+
# .. _annotating_coordinate_systems:
434+
#
435+
# Coordinate systems for annotations
436+
# ----------------------------------
364437
#
365438
# Matplotlib Annotations support several types of coordinates. Some are
366439
# described in :ref:`annotations-tutorial`; more advanced options are
@@ -462,9 +535,6 @@
462535
# and is also necessary if using :doc:`constrained_layout
463536
# </tutorials/intermediate/constrainedlayout_guide>` for positioning the axes.
464537
#
465-
# Advanced Topics
466-
# ---------------
467-
#
468538
# Zoom effect between Axes
469539
# ~~~~~~~~~~~~~~~~~~~~~~~~
470540
#
@@ -475,32 +545,3 @@
475545
# .. figure:: ../../gallery/subplots_axes_and_figures/images/sphx_glr_axes_zoom_effect_001.png
476546
# :target: ../../gallery/subplots_axes_and_figures/axes_zoom_effect.html
477547
# :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

Comments
 (0)