You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.
Dismiss alert
<liclass="toctree-l2"><aclass="reference internal" href="../introductory/lifecycle.html">The Lifecycle of a Plot</a></li>
<liclass="toctree-l2"><aclass="reference internal" href="../introductory/customizing.html">Customizing Matplotlib with style sheets and rcParams</a></li>
<liclass="toctree-l2"><aclass="reference internal" href="pgf.html">Text rendering with XeLaTeX/LuaLaTeX via the <codeclass="docutils literal notranslate"><spanclass="pre">pgf</span></code> backend</a></li>
<liclass="toctree-l2"><aclass="reference internal" href="usetex.html">Text rendering with LaTeX</a></li>
<spanid="sphx-glr-tutorials-text-annotations-py"></span><h1><aclass="toc-backref" href="#id1" role="doc-backlink">Annotations</a><aclass="headerlink" href="#annotations" title="Permalink to this heading">#</a></h1>
<spanid="annotations-tutorial"></span><h2><aclass="toc-backref" href="#id2" role="doc-backlink">Basic annotation</a><aclass="headerlink" href="#basic-annotation" title="Permalink to this heading">#</a></h2>
<p>The uses of the basic <aclass="reference internal" href="../../api/_as_gen/matplotlib.pyplot.text.html#matplotlib.pyplot.text" title="matplotlib.pyplot.text"><codeclass="xref py py-func docutils literal notranslate"><spanclass="pre">text()</span></code></a> will place text
at an arbitrary position on the Axes. A common use case of text is to
keyword arguments like <em>horizontalalignment</em>, <em>verticalalignment</em> and
<em>fontsize</em> are passed from <aclass="reference internal" href="../../api/_as_gen/matplotlib.axes.Axes.annotate.html#matplotlib.axes.Axes.annotate" title="matplotlib.axes.Axes.annotate"><codeclass="xref py py-obj docutils literal notranslate"><spanclass="pre">annotate</span></code></a> to the
<p>For more on all the wild and wonderful things you can do with
annotations, including fancy arrows, see <aclass="reference internal" href="#plotting-guide-annotation"><spanclass="std std-ref">Advanced Annotations</span></a>
and <aclass="reference internal" href="../../gallery/text_labels_and_annotations/annotation_demo.html"><spanclass="doc">Annotating Plots</span></a>.</p>
<p>Do not proceed unless you have already read <aclass="reference internal" href="#annotations-tutorial"><spanclass="std std-ref">Basic annotation</span></a>,
<spanid="plotting-guide-annotation"></span><h2><aclass="toc-backref" href="#id3" role="doc-backlink">Advanced Annotations</a><aclass="headerlink" href="#advanced-annotations" title="Permalink to this heading">#</a></h2>
<sectionid="annotating-with-text-with-box">
<h3><aclass="toc-backref" href="#id4" role="doc-backlink">Annotating with Text with Box</a><aclass="headerlink" href="#annotating-with-text-with-box" title="Permalink to this heading">#</a></h3>
<p><aclass="reference internal" href="../../api/_as_gen/matplotlib.axes.Axes.text.html#matplotlib.axes.Axes.text" title="matplotlib.axes.Axes.text"><codeclass="xref py py-obj docutils literal notranslate"><spanclass="pre">text</span></code></a> takes a <em>bbox</em> keyword argument, which draws a box around the
<h3><aclass="toc-backref" href="#id5" role="doc-backlink">Annotating with Arrow</a><aclass="headerlink" href="#annotating-with-arrow" title="Permalink to this heading">#</a></h3>
<p><aclass="reference internal" href="../../api/_as_gen/matplotlib.axes.Axes.annotate.html#matplotlib.axes.Axes.annotate" title="matplotlib.axes.Axes.annotate"><codeclass="xref py py-obj docutils literal notranslate"><spanclass="pre">annotate</span></code></a> draws an arrow connecting two points in an Axes:</p>
<p>This annotates a point at <em>xy</em> in the given coordinate (<em>xycoords</em>)
with the text at <em>xytext</em> given in <em>textcoords</em>. Often, the
annotated point is specified in the <em>data</em> coordinate and the annotating
text in <em>offset points</em>.
See <aclass="reference internal" href="../../api/_as_gen/matplotlib.axes.Axes.annotate.html#matplotlib.axes.Axes.annotate" title="matplotlib.axes.Axes.annotate"><codeclass="xref py py-obj docutils literal notranslate"><spanclass="pre">annotate</span></code></a> for available coordinate systems.</p>
<p>An arrow connecting <em>xy</em> to <em>xytext</em> can be optionally drawn by
specifying the <em>arrowprops</em> argument. To draw only an arrow, use
<p>Note that "3" in <codeclass="docutils literal notranslate"><spanclass="pre">angle3</span></code> and <codeclass="docutils literal notranslate"><spanclass="pre">arc3</span></code> 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.</p>
<p>The behavior of each connection style is (limitedly) demonstrated in the
example below. (Warning: The behavior of the <codeclass="docutils literal notranslate"><spanclass="pre">bar</span></code> style is currently not
well defined, it may be changed in the future).</p>
<p>Some arrowstyles only work with connection styles that generate a
quadratic-spline segment. They are <codeclass="docutils literal notranslate"><spanclass="pre">fancy</span></code>, <codeclass="docutils literal notranslate"><spanclass="pre">simple</span></code>, and <codeclass="docutils literal notranslate"><spanclass="pre">wedge</span></code>.
For these arrow styles, you must use the "angle3" or "arc3" connection
style.</p>
<p>If the annotation string is given, the patchA is set to the bbox patch
<p>As with <aclass="reference internal" href="../../api/_as_gen/matplotlib.axes.Axes.text.html#matplotlib.axes.Axes.text" title="matplotlib.axes.Axes.text"><codeclass="xref py py-obj docutils literal notranslate"><spanclass="pre">text</span></code></a>, a box around the text can be drawn using the <em>bbox</em>
<h3><aclass="toc-backref" href="#id6" role="doc-backlink">Placing Artist at anchored Axes locations</a><aclass="headerlink" href="#placing-artist-at-anchored-axes-locations" title="Permalink to this heading">#</a></h3>
<p>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 <aclass="reference internal" href="../../api/offsetbox_api.html#matplotlib.offsetbox.OffsetBox" title="matplotlib.offsetbox.OffsetBox"><codeclass="xref py py-obj docutils literal notranslate"><spanclass="pre">OffsetBox</span></code></a> class. A few
predefined classes are available in <aclass="reference internal" href="../../api/offsetbox_api.html#module-matplotlib.offsetbox" title="matplotlib.offsetbox"><codeclass="xref py py-mod docutils literal notranslate"><spanclass="pre">matplotlib.offsetbox</span></code></a> and in
<imgsrc="../../_images/sphx_glr_annotations_001.png" srcset="../../_images/sphx_glr_annotations_001.png, ../../_images/sphx_glr_annotations_001_2_0x.png 2.0x" alt="annotations" class = "sphx-glr-single-img"/><p>The <em>loc</em> keyword has same meaning as in the legend command.</p>
<p>A simple application is when the size of the artist (or collection of
artists) is known in pixel size during the time of creation. For
example, If you want to draw a circle with fixed size of 20 pixel x 20
<imgsrc="../../_images/sphx_glr_annotations_002.png" srcset="../../_images/sphx_glr_annotations_002.png, ../../_images/sphx_glr_annotations_002_2_0x.png 2.0x" alt="annotations" class = "sphx-glr-single-img"/><p>Sometimes, you want your artists to scale with the data coordinate (or
coordinates other than canvas pixels). You can use
<imgsrc="../../_images/sphx_glr_annotations_003.png" srcset="../../_images/sphx_glr_annotations_003.png, ../../_images/sphx_glr_annotations_003_2_0x.png 2.0x" alt="annotations" class = "sphx-glr-single-img"/><p>As in the legend, the bbox_to_anchor argument can be set. Using the
HPacker and VPacker, you can have an arrangement(?) of artist as in the
legend (as a matter of fact, this is how the legend is created).</p>
<p>Note that unlike the legend, the <codeclass="docutils literal notranslate"><spanclass="pre">bbox_transform</span></code> is set
to IdentityTransform by default.</p>
</section>
<sectionid="coordinate-systems-for-annotations">
<h3><aclass="toc-backref" href="#id7" role="doc-backlink">Coordinate systems for Annotations</a><aclass="headerlink" href="#coordinate-systems-for-annotations" title="Permalink to this heading">#</a></h3>
<p>Matplotlib Annotations support several types of coordinates. Some are
described in <aclass="reference internal" href="#annotations-tutorial"><spanclass="std std-ref">Basic annotation</span></a>; more advanced options are</p>
<li><p>An <aclass="reference internal" href="../../api/artist_api.html#matplotlib.artist.Artist" title="matplotlib.artist.Artist"><codeclass="xref py py-obj docutils literal notranslate"><spanclass="pre">Artist</span></code></a> instance. The <em>xy</em> value (or <em>xytext</em>) is interpreted as a
fractional coordinate of the bbox (return value of <em>get_window_extent</em>) of
<spanclass="n">an2</span><spanclass="o">=</span><spanclass="n">ax</span><spanclass="o">.</span><spanclass="n">annotate</span><spanclass="p">(</span><spanclass="s2">"Test 2"</span><spanclass="p">,</span><spanclass="n">xy</span><spanclass="o">=</span><spanclass="p">(</span><spanclass="mi">1</span><spanclass="p">,</span><spanclass="mf">0.5</span><spanclass="p">),</span><spanclass="n">xycoords</span><spanclass="o">=</span><spanclass="n">an1</span><spanclass="p">,</span><spanclass="c1"># (1, 0.5) of the an1's bbox</span>
<p>Note that you must ensure that the extent of the coordinate artist (<em>an1</em> in
above example) is determined before <em>an2</em> gets drawn. Usually, this means
that <em>an2</em> needs to be drawn after <em>an1</em>.</p>
</li>
<li><p>A callable object that takes the renderer instance as single argument, and
returns either a <aclass="reference internal" href="../../api/transformations.html#matplotlib.transforms.Transform" title="matplotlib.transforms.Transform"><codeclass="xref py py-obj docutils literal notranslate"><spanclass="pre">Transform</span></code></a> or a <aclass="reference internal" href="../../api/transformations.html#matplotlib.transforms.BboxBase" title="matplotlib.transforms.BboxBase"><codeclass="xref py py-obj docutils literal notranslate"><spanclass="pre">BboxBase</span></code></a>. The return value is then
handled as in (1), for transforms, or in (2), for bboxes. For example,</p>
<li><p>Sometimes, you want your annotation with some "offset points", not from the
annotated point but from some other point. <aclass="reference internal" href="../../api/text_api.html#matplotlib.text.OffsetFrom" title="matplotlib.text.OffsetFrom"><codeclass="xref py py-obj docutils literal notranslate"><spanclass="pre">text.OffsetFrom</span></code></a> is a helper