From b9c47cec6dfe3c4be101e677ed14d99abfc40b37 Mon Sep 17 00:00:00 2001 From: Antony Lee Date: Sun, 27 Jun 2021 18:56:47 +0200 Subject: [PATCH] More cleanup to annotations tutorial. Make most of the tutorial a comment, rather than a string. Remove non-informative plot titles. Move annotate_simple01 into the tutorial, and delete it as independent example. Remove the table of fancybox properties, and directly output them as part of the plot in fancybox_demo.py. In that same example, it is also nicer to use two column output, and simpler to just output one fancybox per subplot, rather than having to recompute the position of each fancybox in data coordinates. Don't alphabetically sort boxstyles/arrowstyles/etc., the source order being semantically more meaningful. --- .../shapes_and_collections/fancybox_demo.py | 23 +- examples/userdemo/annotate_simple01.py | 19 - lib/matplotlib/patches.py | 4 +- tutorials/text/annotations.py | 590 ++++++++---------- 4 files changed, 280 insertions(+), 356 deletions(-) delete mode 100644 examples/userdemo/annotate_simple01.py diff --git a/examples/shapes_and_collections/fancybox_demo.py b/examples/shapes_and_collections/fancybox_demo.py index 0d1588ad9e4a..135578ab601e 100644 --- a/examples/shapes_and_collections/fancybox_demo.py +++ b/examples/shapes_and_collections/fancybox_demo.py @@ -6,6 +6,8 @@ The following examples show how to plot boxes with different visual properties. """ +import inspect + import matplotlib.pyplot as plt import matplotlib.transforms as mtransforms import matplotlib.patches as mpatch @@ -15,17 +17,16 @@ # First we'll show some sample boxes with fancybox. styles = mpatch.BoxStyle.get_styles() -spacing = 1.2 - -figheight = (spacing * len(styles) + .5) -fig = plt.figure(figsize=(4 / 1.5, figheight / 1.5)) -fontsize = 0.3 * 72 - -for i, stylename in enumerate(sorted(styles)): - fig.text(0.5, (spacing * (len(styles) - i) - 0.5) / figheight, stylename, - ha="center", - size=fontsize, - bbox=dict(boxstyle=stylename, fc="w", ec="k")) +ncol = 2 +nrow = (len(styles) + 1) // ncol +axs = plt.figure(figsize=(3 * ncol, nrow)).subplots(nrow, ncol) +for ax in axs.flat: + ax.set_axis_off() +for ax, (stylename, stylecls) in zip(axs.T.flat, styles.items()): + ax.text(.5, .5, + f"{stylename}\n{inspect.signature(stylecls)}", + transform=ax.transAxes, ha="center", va="center", + bbox=dict(boxstyle=stylename, fc="w", ec="k")) ############################################################################### diff --git a/examples/userdemo/annotate_simple01.py b/examples/userdemo/annotate_simple01.py deleted file mode 100644 index 0376397d0cb7..000000000000 --- a/examples/userdemo/annotate_simple01.py +++ /dev/null @@ -1,19 +0,0 @@ -""" -================= -Annotate Simple01 -================= - -""" -import matplotlib.pyplot as plt - - -fig, ax = plt.subplots(figsize=(3, 3)) - -ax.annotate("", - xy=(0.2, 0.2), xycoords='data', - xytext=(0.8, 0.8), textcoords='data', - arrowprops=dict(arrowstyle="->", - connectionstyle="arc3"), - ) - -plt.show() diff --git a/lib/matplotlib/patches.py b/lib/matplotlib/patches.py index 7793c7808dd4..00df0943a57f 100644 --- a/lib/matplotlib/patches.py +++ b/lib/matplotlib/patches.py @@ -2138,7 +2138,7 @@ def _simpleprint_styles(_styles): {stylename: styleclass}, return a string rep of the list of keys. Used to update the documentation. """ - return "[{}]".format("|".join(map(" '{}' ".format, sorted(_styles)))) + return "[{}]".format("|".join(map(" '{}' ".format, _styles))) class _Style: @@ -2184,7 +2184,7 @@ def pprint_styles(cls): f'``{name}``', # [1:-1] drops the surrounding parentheses. str(inspect.signature(cls))[1:-1] or 'None') - for name, cls in sorted(cls._style_list.items())]] + for name, cls in cls._style_list.items()]] # Convert to rst table. col_len = [max(len(cell) for cell in column) for column in zip(*table)] table_formatstr = ' '.join('=' * cl for cl in col_len) diff --git a/tutorials/text/annotations.py b/tutorials/text/annotations.py index 42766260fccc..0b7909ddd7ee 100644 --- a/tutorials/text/annotations.py +++ b/tutorials/text/annotations.py @@ -1,4 +1,4 @@ -r""" +""" Annotations =========== @@ -7,322 +7,276 @@ .. contents:: Table of Contents :depth: 3 -.. _annotations-tutorial: - -Basic annotation ----------------- - -The uses of the basic :func:`~matplotlib.pyplot.text` will place text -at an arbitrary position on the Axes. A common use case of text is to -annotate some feature of the plot, and the -:func:`~matplotlib.axes.Axes.annotate` method provides helper functionality -to make annotations easy. In an annotation, there are two points to -consider: the location being annotated represented by the argument -*xy* and the location of the text *xytext*. Both of these -arguments are ``(x, y)`` tuples. - -.. figure:: ../../gallery/pyplots/images/sphx_glr_annotation_basic_001.png - :target: ../../gallery/pyplots/annotation_basic.html - :align: center - :scale: 50 - - Annotation Basic - -In this example, both the *xy* (arrow tip) and *xytext* locations -(text location) are in data coordinates. There are a variety of other -coordinate systems one can choose -- you can specify the coordinate -system of *xy* and *xytext* with one of the following strings for -*xycoords* and *textcoords* (default is 'data') - -================== ======================================================== -argument coordinate system -================== ======================================================== -'figure points' points from the lower left corner of the figure -'figure pixels' pixels from the lower left corner of the figure -'figure fraction' (0, 0) is lower left of figure and (1, 1) is upper right -'axes points' points from lower left corner of axes -'axes pixels' pixels from lower left corner of axes -'axes fraction' (0, 0) is lower left of axes and (1, 1) is upper right -'data' use the axes data coordinate system -================== ======================================================== - -For example to place the text coordinates in fractional axes -coordinates, one could do:: - - ax.annotate('local max', xy=(3, 1), xycoords='data', - xytext=(0.8, 0.95), textcoords='axes fraction', - arrowprops=dict(facecolor='black', shrink=0.05), - horizontalalignment='right', verticalalignment='top', - ) - -For physical coordinate systems (points or pixels) the origin is the -bottom-left of the figure or axes. - -Optionally, you can enable drawing of an arrow from the text to the annotated -point by giving a dictionary of arrow properties in the optional keyword -argument *arrowprops*. - - -==================== ===================================================== -*arrowprops* key description -==================== ===================================================== -width the width of the arrow in points -frac the fraction of the arrow length occupied by the head -headwidth the width of the base of the arrow head in points -shrink move the tip and base some percent away from - the annotated point and text - -\*\*kwargs any key for :class:`matplotlib.patches.Polygon`, - e.g., ``facecolor`` -==================== ===================================================== - - -In the example below, the *xy* point is in native coordinates -(*xycoords* defaults to 'data'). For a polar axes, this is in -(theta, radius) space. The text in this example is placed in the -fractional figure coordinate system. :class:`matplotlib.text.Text` -keyword arguments like *horizontalalignment*, *verticalalignment* and -*fontsize* are passed from `~matplotlib.axes.Axes.annotate` to the -``Text`` instance. - -.. figure:: ../../gallery/pyplots/images/sphx_glr_annotation_polar_001.png - :target: ../../gallery/pyplots/annotation_polar.html - :align: center - :scale: 50 - - Annotation Polar - -For more on all the wild and wonderful things you can do with -annotations, including fancy arrows, see :ref:`plotting-guide-annotation` -and :doc:`/gallery/text_labels_and_annotations/annotation_demo`. - - -Do not proceed unless you have already read :ref:`annotations-tutorial`, -:func:`~matplotlib.pyplot.text` and :func:`~matplotlib.pyplot.annotate`! - - -.. _plotting-guide-annotation: - -Advanced Annotations --------------------- - -Annotating with Text with Box -~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ - -Let's start with a simple example. - -.. figure:: ../../gallery/userdemo/images/sphx_glr_annotate_text_arrow_001.png - :target: ../../gallery/userdemo/annotate_text_arrow.html - :align: center - :scale: 50 - - Annotate Text Arrow - -`~.Axes.text` takes a *bbox* keyword argument, which draws a box around the -text:: - - t = ax.text( - 0, 0, "Direction", ha="center", va="center", rotation=45, size=15, - bbox=dict(boxstyle="rarrow,pad=0.3", fc="cyan", ec="b", lw=2)) - -The patch object associated with the text can be accessed by:: - - bb = t.get_bbox_patch() - -The return value is a `.FancyBboxPatch`; patch properties -(facecolor, edgewidth, etc.) can be accessed and modified as usual. -`.FancyBboxPatch.set_boxstyle` sets the box shape:: - - bb.set_boxstyle("rarrow", pad=0.6) - -The arguments are the name of the box style with its attributes as -keyword arguments. Currently, following box styles are implemented. - - ========== ============== ========================== - Class Name Attrs - ========== ============== ========================== - Circle ``circle`` pad=0.3 - DArrow ``darrow`` pad=0.3 - LArrow ``larrow`` pad=0.3 - RArrow ``rarrow`` pad=0.3 - Round ``round`` pad=0.3,rounding_size=None - Round4 ``round4`` pad=0.3,rounding_size=None - Roundtooth ``roundtooth`` pad=0.3,tooth_size=None - Sawtooth ``sawtooth`` pad=0.3,tooth_size=None - Square ``square`` pad=0.3 - ========== ============== ========================== - -.. figure:: ../../gallery/shapes_and_collections/images/sphx_glr_fancybox_demo_001.png - :target: ../../gallery/shapes_and_collections/fancybox_demo.html - :align: center - :scale: 50 - - Fancybox Demo - -Note that the attribute arguments can be specified within the style -name with separating comma (this form can be used as "boxstyle" value -of bbox argument when initializing the text instance) :: - - bb.set_boxstyle("rarrow,pad=0.6") - -Annotating with Arrow -~~~~~~~~~~~~~~~~~~~~~ - -`~.Axes.annotate` draws an arrow connecting two points in an axes:: - - ax.annotate("Annotation", - xy=(x1, y1), xycoords='data', - xytext=(x2, y2), textcoords='offset points', - ) - -This annotates a point at *xy* in the given coordinate (*xycoords*) -with the text at *xytext* given in *textcoords*. Often, the -annotated point is specified in the *data* coordinate and the annotating -text in *offset points*. -See `~.Axes.annotate` for available coordinate systems. - -An arrow connecting *xy* to *xytext* can be optionally drawn by -specifying the *arrowprops* argument. To draw only an arrow, use -empty string as the first argument. :: - - ax.annotate("", - xy=(0.2, 0.2), xycoords='data', - xytext=(0.8, 0.8), textcoords='data', - arrowprops=dict(arrowstyle="->", - connectionstyle="arc3"), - ) - -.. figure:: ../../gallery/userdemo/images/sphx_glr_annotate_simple01_001.png - :target: ../../gallery/userdemo/annotate_simple01.html - :align: center - :scale: 50 - - Annotate Simple01 - -The arrow is drawn as follows: - -1. A path connecting the two points is created, as specified by the - *connectionstyle* parameter. -2. The path is clipped to avoid patches *patchA* and *patchB*, if these are - set. -3. The path is further shrunk by *shrinkA* and *shrinkB* (in pixels). -4. The path is transmuted to an arrow patch, as specified by the *arrowstyle* - parameter. - -.. figure:: ../../gallery/userdemo/images/sphx_glr_annotate_explain_001.png - :target: ../../gallery/userdemo/annotate_explain.html - :align: center - :scale: 50 - - Annotate Explain - - -The creation of the connecting path between two points is controlled by -``connectionstyle`` key and the following styles are available. - - ========== ============================================= - Name Attrs - ========== ============================================= - ``angle`` angleA=90,angleB=0,rad=0.0 - ``angle3`` angleA=90,angleB=0 - ``arc`` angleA=0,angleB=0,armA=None,armB=None,rad=0.0 - ``arc3`` rad=0.0 - ``bar`` armA=0.0,armB=0.0,fraction=0.3,angle=None - ========== ============================================= - -Note that "3" in ``angle3`` and ``arc3`` is meant to indicate that the -resulting path is a quadratic spline segment (three control -points). As will be discussed below, some arrow style options can only -be used when the connecting path is a quadratic spline. - -The behavior of each connection style is (limitedly) demonstrated in the -example below. (Warning: The behavior of the ``bar`` style is currently not -well defined, it may be changed in the future). - -.. figure:: ../../gallery/userdemo/images/sphx_glr_connectionstyle_demo_001.png - :target: ../../gallery/userdemo/connectionstyle_demo.html - :align: center - :scale: 50 - - Connectionstyle Demo - - -The connecting path (after clipping and shrinking) is then mutated to -an arrow patch, according to the given ``arrowstyle``. - - ========== ============================================= - Name Attrs - ========== ============================================= - ``-`` None - ``->`` head_length=0.4,head_width=0.2 - ``-[`` widthB=1.0,lengthB=0.2,angleB=None - ``|-|`` widthA=1.0,widthB=1.0 - ``-|>`` head_length=0.4,head_width=0.2 - ``<-`` head_length=0.4,head_width=0.2 - ``<->`` head_length=0.4,head_width=0.2 - ``<|-`` head_length=0.4,head_width=0.2 - ``<|-|>`` head_length=0.4,head_width=0.2 - ``fancy`` head_length=0.4,head_width=0.4,tail_width=0.4 - ``simple`` head_length=0.5,head_width=0.5,tail_width=0.2 - ``wedge`` tail_width=0.3,shrink_factor=0.5 - ========== ============================================= - -.. figure:: ../../gallery/text_labels_and_annotations/images/sphx_glr_fancyarrow_demo_001.png - :target: ../../gallery/text_labels_and_annotations/fancyarrow_demo.html - :align: center - :scale: 50 - - Fancyarrow Demo - -Some arrowstyles only work with connection styles that generate a -quadratic-spline segment. They are ``fancy``, ``simple``, and ``wedge``. -For these arrow styles, you must use the "angle3" or "arc3" connection -style. - -If the annotation string is given, the patchA is set to the bbox patch -of the text by default. - -.. figure:: ../../gallery/userdemo/images/sphx_glr_annotate_simple02_001.png - :target: ../../gallery/userdemo/annotate_simple02.html - :align: center - :scale: 50 - - Annotate Simple02 - -As with `~.Axes.text`, a box around the text can be drawn using the *bbox* -argument. - -.. figure:: ../../gallery/userdemo/images/sphx_glr_annotate_simple03_001.png - :target: ../../gallery/userdemo/annotate_simple03.html - :align: center - :scale: 50 - - Annotate Simple03 - -By default, the starting point is set to the center of the text -extent. This can be adjusted with ``relpos`` key value. The values -are normalized to the extent of the text. For example, (0, 0) means -lower-left corner and (1, 1) means top-right. - -.. figure:: ../../gallery/userdemo/images/sphx_glr_annotate_simple04_001.png - :target: ../../gallery/userdemo/annotate_simple04.html - :align: center - :scale: 50 +.. redirect-from:: /gallery/userdemo/annotate_simple01 +""" - Annotate Simple04 +from matplotlib import pyplot as plt +############################################################################### +# .. _annotations-tutorial: +# +# Basic annotation +# ---------------- +# +# The uses of the basic :func:`~matplotlib.pyplot.text` will place text +# at an arbitrary position on the Axes. A common use case of text is to +# annotate some feature of the plot, and the +# :func:`~matplotlib.axes.Axes.annotate` method provides helper functionality +# to make annotations easy. In an annotation, there are two points to +# consider: the location being annotated represented by the argument +# *xy* and the location of the text *xytext*. Both of these +# arguments are ``(x, y)`` tuples. +# +# .. figure:: ../../gallery/pyplots/images/sphx_glr_annotation_basic_001.png +# :target: ../../gallery/pyplots/annotation_basic.html +# :align: center +# :scale: 50 +# +# In this example, both the *xy* (arrow tip) and *xytext* locations +# (text location) are in data coordinates. There are a variety of other +# coordinate systems one can choose -- you can specify the coordinate +# system of *xy* and *xytext* with one of the following strings for +# *xycoords* and *textcoords* (default is 'data') +# +# ================== ======================================================== +# argument coordinate system +# ================== ======================================================== +# 'figure points' points from the lower left corner of the figure +# 'figure pixels' pixels from the lower left corner of the figure +# 'figure fraction' (0, 0) is lower left of figure and (1, 1) is upper right +# 'axes points' points from lower left corner of axes +# 'axes pixels' pixels from lower left corner of axes +# 'axes fraction' (0, 0) is lower left of axes and (1, 1) is upper right +# 'data' use the axes data coordinate system +# ================== ======================================================== +# +# For example to place the text coordinates in fractional axes +# coordinates, one could do:: +# +# ax.annotate('local max', xy=(3, 1), xycoords='data', +# xytext=(0.8, 0.95), textcoords='axes fraction', +# arrowprops=dict(facecolor='black', shrink=0.05), +# horizontalalignment='right', verticalalignment='top', +# ) +# +# For physical coordinate systems (points or pixels) the origin is the +# bottom-left of the figure or axes. +# +# Optionally, you can enable drawing of an arrow from the text to the annotated +# point by giving a dictionary of arrow properties in the optional keyword +# argument *arrowprops*. +# +# ==================== ===================================================== +# *arrowprops* key description +# ==================== ===================================================== +# width the width of the arrow in points +# frac the fraction of the arrow length occupied by the head +# headwidth the width of the base of the arrow head in points +# shrink move the tip and base some percent away from +# the annotated point and text +# \*\*kwargs any key for :class:`matplotlib.patches.Polygon`, +# e.g., ``facecolor`` +# ==================== ===================================================== +# +# In the example below, the *xy* point is in native coordinates +# (*xycoords* defaults to 'data'). For a polar axes, this is in +# (theta, radius) space. The text in this example is placed in the +# fractional figure coordinate system. :class:`matplotlib.text.Text` +# keyword arguments like *horizontalalignment*, *verticalalignment* and +# *fontsize* are passed from `~matplotlib.axes.Axes.annotate` to the +# ``Text`` instance. +# +# .. figure:: ../../gallery/pyplots/images/sphx_glr_annotation_polar_001.png +# :target: ../../gallery/pyplots/annotation_polar.html +# :align: center +# :scale: 50 +# +# For more on all the wild and wonderful things you can do with +# annotations, including fancy arrows, see :ref:`plotting-guide-annotation` +# and :doc:`/gallery/text_labels_and_annotations/annotation_demo`. +# +# Do not proceed unless you have already read :ref:`annotations-tutorial`, +# :func:`~matplotlib.pyplot.text` and :func:`~matplotlib.pyplot.annotate`! +# +# .. _plotting-guide-annotation: +# +# Advanced Annotations +# -------------------- +# +# Annotating with Text with Box +# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +# +# Let's start with a simple example. +# +# .. figure:: ../../gallery/userdemo/images/sphx_glr_annotate_text_arrow_001.png +# :target: ../../gallery/userdemo/annotate_text_arrow.html +# :align: center +# :scale: 50 +# +# `~.Axes.text` takes a *bbox* keyword argument, which draws a box around the +# text:: +# +# t = ax.text( +# 0, 0, "Direction", ha="center", va="center", rotation=45, size=15, +# bbox=dict(boxstyle="rarrow,pad=0.3", fc="cyan", ec="b", lw=2)) +# +# The patch object associated with the text can be accessed by:: +# +# bb = t.get_bbox_patch() +# +# The return value is a `.FancyBboxPatch`; patch properties +# (facecolor, edgewidth, etc.) can be accessed and modified as usual. +# `.FancyBboxPatch.set_boxstyle` sets the box shape:: +# +# bb.set_boxstyle("rarrow", pad=0.6) +# +# The arguments are the name of the box style with its attributes as +# keyword arguments. Currently, the following box styles are implemented. +# +# .. figure:: ../../gallery/shapes_and_collections/images/sphx_glr_fancybox_demo_001.png +# :target: ../../gallery/shapes_and_collections/fancybox_demo.html +# :align: center +# +# Note that the attribute arguments can be specified within the style +# name with separating comma (this form can be used as "boxstyle" value +# of bbox argument when initializing the text instance) :: +# +# bb.set_boxstyle("rarrow,pad=0.6") +# +# Annotating with Arrow +# ~~~~~~~~~~~~~~~~~~~~~ +# +# `~.Axes.annotate` draws an arrow connecting two points in an axes:: +# +# ax.annotate("Annotation", +# xy=(x1, y1), xycoords='data', +# xytext=(x2, y2), textcoords='offset points', +# ) +# +# This annotates a point at *xy* in the given coordinate (*xycoords*) +# with the text at *xytext* given in *textcoords*. Often, the +# annotated point is specified in the *data* coordinate and the annotating +# text in *offset points*. +# See `~.Axes.annotate` for available coordinate systems. +# +# An arrow connecting *xy* to *xytext* can be optionally drawn by +# specifying the *arrowprops* argument. To draw only an arrow, use +# empty string as the first argument. -Placing Artist at the anchored location of the Axes -~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +fig, ax = plt.subplots(figsize=(3, 3)) +ax.annotate("", + xy=(0.2, 0.2), xycoords='data', + xytext=(0.8, 0.8), textcoords='data', + arrowprops=dict(arrowstyle="->", connectionstyle="arc3")) -There are classes of artists that can be placed at an anchored -location in the Axes. A common example is the legend. This type -of artist can be created by using the `.OffsetBox` class. A few -predefined classes are available in :mod:`matplotlib.offsetbox` and in -:mod:`mpl_toolkits.axes_grid1.anchored_artists`. -""" +############################################################################### +# The arrow is drawn as follows: +# +# 1. A path connecting the two points is created, as specified by the +# *connectionstyle* parameter. +# 2. The path is clipped to avoid patches *patchA* and *patchB*, if these are +# set. +# 3. The path is further shrunk by *shrinkA* and *shrinkB* (in pixels). +# 4. The path is transmuted to an arrow patch, as specified by the *arrowstyle* +# parameter. +# +# .. figure:: ../../gallery/userdemo/images/sphx_glr_annotate_explain_001.png +# :target: ../../gallery/userdemo/annotate_explain.html +# :align: center +# :scale: 50 +# +# The creation of the connecting path between two points is controlled by +# ``connectionstyle`` key and the following styles are available. +# +# ========== ============================================= +# Name Attrs +# ========== ============================================= +# ``angle`` angleA=90,angleB=0,rad=0.0 +# ``angle3`` angleA=90,angleB=0 +# ``arc`` angleA=0,angleB=0,armA=None,armB=None,rad=0.0 +# ``arc3`` rad=0.0 +# ``bar`` armA=0.0,armB=0.0,fraction=0.3,angle=None +# ========== ============================================= +# +# Note that "3" in ``angle3`` and ``arc3`` is meant to indicate that the +# resulting path is a quadratic spline segment (three control +# points). As will be discussed below, some arrow style options can only +# be used when the connecting path is a quadratic spline. +# +# The behavior of each connection style is (limitedly) demonstrated in the +# example below. (Warning: The behavior of the ``bar`` style is currently not +# well defined, it may be changed in the future). +# +# .. figure:: ../../gallery/userdemo/images/sphx_glr_connectionstyle_demo_001.png +# :target: ../../gallery/userdemo/connectionstyle_demo.html +# :align: center +# :scale: 50 +# +# The connecting path (after clipping and shrinking) is then mutated to +# an arrow patch, according to the given ``arrowstyle``. +# +# ========== ============================================= +# Name Attrs +# ========== ============================================= +# ``-`` None +# ``->`` head_length=0.4,head_width=0.2 +# ``-[`` widthB=1.0,lengthB=0.2,angleB=None +# ``|-|`` widthA=1.0,widthB=1.0 +# ``-|>`` head_length=0.4,head_width=0.2 +# ``<-`` head_length=0.4,head_width=0.2 +# ``<->`` head_length=0.4,head_width=0.2 +# ``<|-`` head_length=0.4,head_width=0.2 +# ``<|-|>`` head_length=0.4,head_width=0.2 +# ``fancy`` head_length=0.4,head_width=0.4,tail_width=0.4 +# ``simple`` head_length=0.5,head_width=0.5,tail_width=0.2 +# ``wedge`` tail_width=0.3,shrink_factor=0.5 +# ========== ============================================= +# +# .. figure:: ../../gallery/text_labels_and_annotations/images/sphx_glr_fancyarrow_demo_001.png +# :target: ../../gallery/text_labels_and_annotations/fancyarrow_demo.html +# :align: center +# :scale: 50 +# +# Some arrowstyles only work with connection styles that generate a +# quadratic-spline segment. They are ``fancy``, ``simple``, and ``wedge``. +# For these arrow styles, you must use the "angle3" or "arc3" connection +# style. +# +# If the annotation string is given, the patchA is set to the bbox patch +# of the text by default. +# +# .. figure:: ../../gallery/userdemo/images/sphx_glr_annotate_simple02_001.png +# :target: ../../gallery/userdemo/annotate_simple02.html +# :align: center +# :scale: 50 +# +# As with `~.Axes.text`, a box around the text can be drawn using the *bbox* +# argument. +# +# .. figure:: ../../gallery/userdemo/images/sphx_glr_annotate_simple03_001.png +# :target: ../../gallery/userdemo/annotate_simple03.html +# :align: center +# :scale: 50 +# +# By default, the starting point is set to the center of the text +# extent. This can be adjusted with ``relpos`` key value. The values +# are normalized to the extent of the text. For example, (0, 0) means +# lower-left corner and (1, 1) means top-right. +# +# .. figure:: ../../gallery/userdemo/images/sphx_glr_annotate_simple04_001.png +# :target: ../../gallery/userdemo/annotate_simple04.html +# :align: center +# :scale: 50 +# +# Placing Artist at the anchored location of the Axes +# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +# +# There are classes of artists that can be placed at an anchored +# location in the Axes. A common example is the legend. This type +# of artist can be created by using the `.OffsetBox` class. A few +# predefined classes are available in :mod:`matplotlib.offsetbox` and in +# :mod:`mpl_toolkits.axes_grid1.anchored_artists`. -from matplotlib import pyplot as plt from matplotlib.offsetbox import AnchoredText fig, ax = plt.subplots() @@ -436,8 +390,6 @@ # :align: center # :scale: 50 # -# Annotation with Simple Coordinates -# # Note that you must ensure that the extent of the coordinate artist (*an1* in # above example) is determined before *an2* gets drawn. Usually, this means # that *an2* needs to be drawn after *an1*. @@ -468,8 +420,6 @@ # :align: center # :scale: 50 # -# Annotation with Simple Coordinates 2 -# # 5. Sometimes, you want your annotation with some "offset points", not from the # annotated point but from some other point. `.text.OffsetFrom` is a helper # for such cases. @@ -479,8 +429,6 @@ # :align: center # :scale: 50 # -# Annotation with Simple Coordinates 3 -# # You may take a look at this example # :doc:`/gallery/text_labels_and_annotations/annotation_demo`. # @@ -505,8 +453,6 @@ # :align: center # :scale: 50 # -# Connect Simple01 -# # Here, we added the ConnectionPatch to the *figure* (with `~.Figure.add_artist`) # rather than to either axes: this ensures that it is drawn on top of both axes, # and is also necessary if using :doc:`constrained_layout @@ -527,8 +473,6 @@ # :align: center # :scale: 50 # -# Axes Zoom Effect -# # Define Custom BoxStyle # ~~~~~~~~~~~~~~~~~~~~~~ # @@ -555,8 +499,6 @@ # :align: center # :scale: 50 # -# Custom Boxstyle01 -# # Similarly, you can define a custom ConnectionStyle and a custom ArrowStyle. # See the source code of ``lib/matplotlib/patches.py`` and check # how each style class is defined.