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

Skip to content

Commit 524e4f7

Browse files
committed
added offset section and restructured annotations tutorial
Co-authored-by: @jklymak
1 parent a68f21f commit 524e4f7

1 file changed

Lines changed: 92 additions & 50 deletions

File tree

tutorials/text/annotations.py

Lines changed: 92 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
###############################################################################
1414
# .. _annotations-tutorial:
1515
#
16-
# Basic annotation
16+
# Basic Annotation
1717
# ----------------
1818
#
1919
# The uses of the basic :func:`~matplotlib.pyplot.text` will place text
@@ -47,23 +47,47 @@
4747
# 'data' use the axes data coordinate system
4848
# ================== ========================================================
4949
#
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+
#
5070
# For example to place the text coordinates in fractional axes
5171
# 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+
###################################################################
5281
#
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:
5883
#
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+
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
6186
#
6287
# Optionally, you can enable drawing of an arrow from the text to the annotated
6388
# point by giving a dictionary of arrow properties in the optional keyword
6489
# argument *arrowprops*.
6590
#
66-
#
6791
# ==================== =====================================================
6892
# *arrowprops* key description
6993
# ==================== =====================================================
@@ -77,10 +101,9 @@
77101
# e.g., ``facecolor``
78102
# ==================== =====================================================
79103
#
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
84107
# fractional figure coordinate system. :class:`matplotlib.text.Text`
85108
# keyword arguments like *horizontalalignment*, *verticalalignment* and
86109
# *fontsize* are passed from `~matplotlib.axes.Axes.annotate` to the
@@ -98,13 +121,33 @@
98121
# Do not proceed unless you have already read :ref:`annotations-tutorial`,
99122
# :func:`~matplotlib.pyplot.text` and :func:`~matplotlib.pyplot.annotate`!
100123
#
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.
101144
#
102145
# .. _plotting-guide-annotation:
103146
#
104-
# Advanced Annotations
147+
# Advanced Annotation
105148
# --------------------
106149
#
107-
# Annotating with Text with Box
150+
# Annotating with Text in Box
108151
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
109152
#
110153
# Let's start with a simple example.
@@ -157,8 +200,8 @@
157200
#
158201
# bb.set_boxstyle("rarrow,pad=0.6")
159202
#
160-
# Annotating with Arrow
161-
# ~~~~~~~~~~~~~~~~~~~~~
203+
# Customizing Annotation Arrow
204+
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
162205
#
163206
# `~.Axes.annotate` draws an arrow connecting two points in an Axes::
164207
#
@@ -359,8 +402,39 @@
359402
# Note that unlike the legend, the ``bbox_transform`` is set
360403
# to IdentityTransform by default.
361404
#
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+
#
362436
# Coordinate systems for Annotations
363-
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
437+
# ----------------------------------
364438
#
365439
# Matplotlib Annotations support several types of coordinates. Some are
366440
# described in :ref:`annotations-tutorial`; more advanced options are
@@ -462,9 +536,6 @@
462536
# and is also necessary if using :doc:`constrained_layout
463537
# </tutorials/intermediate/constrainedlayout_guide>` for positioning the axes.
464538
#
465-
# Advanced Topics
466-
# ---------------
467-
#
468539
# Zoom effect between Axes
469540
# ~~~~~~~~~~~~~~~~~~~~~~~~
470541
#
@@ -475,32 +546,3 @@
475546
# .. figure:: ../../gallery/subplots_axes_and_figures/images/sphx_glr_axes_zoom_effect_001.png
476547
# :target: ../../gallery/subplots_axes_and_figures/axes_zoom_effect.html
477548
# :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)