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

Skip to content

Commit 207fa99

Browse files
committed
update annotation guide
svn path=/trunk/matplotlib/; revision=8163
1 parent 1650927 commit 207fa99

6 files changed

Lines changed: 171 additions & 32 deletions

File tree

doc/users/annotations_guide.rst

Lines changed: 110 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,11 @@
44
Annotating Axes
55
****************
66

7-
Do not proceed unless you already have read
8-
:func:`~matplotlib.pyplot.text` and :func:`~matplotlib.pyplot.annotate`!
7+
Do not proceed unless you already have read :ref:`annotations-tutorial`,
8+
:func:`~matplotlib.pyplot.text` and
9+
:func:`~matplotlib.pyplot.annotate`!
10+
11+
912

1013

1114
Annotating with Text with Box
@@ -182,31 +185,6 @@ lower-left corner and (1,1) means top-right.
182185
.. plot:: users/plotting/examples/annotate_simple04.py
183186

184187

185-
Using ConnectorPatch
186-
====================
187-
188-
The ConnectorPatch is like an annotation without a text. While the
189-
annotate function is recommended in most of situation, the
190-
ConnectorPatch is useful when you want to connect points in different
191-
axes. ::
192-
193-
from matplotlib.patches import ConnectionPatch
194-
xy = (0.2, 0.2)
195-
con = ConnectionPatch(xyA=xy, xyB=xy, coordsA="data", coordsB="data",
196-
axesA=ax1, axesB=ax2)
197-
ax2.add_artist(con)
198-
199-
The above code connects point xy in data coordinate of ``ax1`` to
200-
point xy int data coordinate of ``ax2``. Here is a simple example.
201-
202-
.. plot:: users/plotting/examples/connect_simple01.py
203-
204-
205-
While the ConnectorPatch instance can be added to any axes, but you
206-
may want it to be added to the axes in the latter (?) of the axes
207-
drawing order to prevent overlap (?) by other axes.
208-
209-
210188
Placing Artist at the anchored location of the Axes
211189
===================================================
212190

@@ -282,6 +260,111 @@ legend (as a matter of fact, this is how the legend is created).
282260
Note that unlike the legend, the ``bbox_transform`` is set
283261
to IdentityTransform by default.
284262

263+
Using Complex Coordinate with Annotation
264+
========================================
265+
266+
The Annotation in matplotlib support several types of coordinate as
267+
described in :ref:`annotations-tutorial`. For an advanced user who wants
268+
more control, it supports a few other options.
269+
270+
1. :class:`~matplotlib.transforms.Transform` instance. For example, ::
271+
272+
ax.annotate("Test", xy=(0.5, 0.5), xycoords=ax.transAxes)
273+
274+
is identical to ::
275+
276+
ax.annotate("Test", xy=(0.5, 0.5), xycoords="axes fraction")
277+
278+
With this, you can annotate a point in other axes. ::
279+
280+
ax1, ax2 = subplot(121), subplot(122)
281+
ax2.annotate("Test", xy=(0.5, 0.5), xycoords=ax1.transData,
282+
xytext=(0.5, 0.5), textcoords=ax2.transData,
283+
arrowprops=dict(arrowstyle="->"))
284+
285+
2. :class:`~matplotlib.artist.Artist` instance. The xy value (or
286+
xytext) is interpreted as a fractional coordinate of the bbox
287+
(return value of *get_window_extent*) of the artist. ::
288+
289+
an1 = ax.annotate("Test 1", xy=(0.5, 0.5), xycoords="data",
290+
va="center", ha="center",
291+
bbox=dict(boxstyle="round", fc="w"))
292+
an2 = ax.annotate("Test 2", xy=(1, 0.5), xycoords=an1, # (1,0.5) of the an1's bbox
293+
xytext=(30,0), textcoords="offset points",
294+
va="center", ha="left",
295+
bbox=dict(boxstyle="round", fc="w"),
296+
arrowprops=dict(arrowstyle="->"))
297+
298+
.. plot:: users/plotting/examples/annotate_simple_coord01.py
299+
300+
Note that it is your responsibility that the extent of the
301+
coordinate artist (*an1* in above example) is determined before *an2*
302+
gets drawn. In most cases, it means that an2 needs to be drawn
303+
later than *an1*.
304+
305+
306+
3. A callable object that returns an instance of either
307+
:class:`~matplotlib.transforms.BboxBase` or
308+
:class:`~matplotlib.transforms.Transform`. If a transform is
309+
returned, it is same as 1 and if bbox is returned, it is same
310+
as 2. The callable object should take a single argument of
311+
renderer instance. For example, following two commands give
312+
identical results ::
313+
an2 = ax.annotate("Test 2", xy=(1, 0.5), xycoords=an1,
314+
xytext=(30,0), textcoords="offset points")
315+
an2 = ax.annotate("Test 2", xy=(1, 0.5), xycoords=an1.get_window_extent,
316+
xytext=(30,0), textcoords="offset points")
317+
318+
319+
4. A tuple of two coordinate specification. The first item is for
320+
x-coordinate and the second is for y-coordinate. For example, ::
321+
322+
annotate("Test", xy=(0.5, 1), xycoords=("data", "axes fraction"))
323+
324+
0.5 is in data coordinate, and 1 is in normalized axes coordinate.
325+
You may use an atist or transform as with a tuple. For example,
326+
327+
.. plot:: users/plotting/examples/annotate_simple_coord02.py
328+
:include-source:
329+
330+
331+
5. Sometimes, you want your annotation with some "offset points", but
332+
not from the annotated point but from other
333+
point. :class:`~matplotlib.text.OffsetFrom` is a helper class for such
334+
case.
335+
336+
.. plot:: users/plotting/examples/annotate_simple_coord03.py
337+
:include-source:
338+
339+
You may take a look at this example :ref:`pylab_examples-annotation_demo3`.
340+
341+
Using ConnectorPatch
342+
====================
343+
344+
The ConnectorPatch is like an annotation without a text. While the
345+
annotate function is recommended in most of situation, the
346+
ConnectorPatch is useful when you want to connect points in different
347+
axes. ::
348+
349+
from matplotlib.patches import ConnectionPatch
350+
xy = (0.2, 0.2)
351+
con = ConnectionPatch(xyA=xy, xyB=xy, coordsA="data", coordsB="data",
352+
axesA=ax1, axesB=ax2)
353+
ax2.add_artist(con)
354+
355+
The above code connects point xy in data coordinate of ``ax1`` to
356+
point xy int data coordinate of ``ax2``. Here is a simple example.
357+
358+
.. plot:: users/plotting/examples/connect_simple01.py
359+
360+
361+
While the ConnectorPatch instance can be added to any axes, but you
362+
may want it to be added to the axes in the latter (?) of the axes
363+
drawing order to prevent overlap (?) by other axes.
364+
365+
366+
367+
285368
Advanced Topics
286369
***************
287370

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
2+
import matplotlib.pyplot as plt
3+
4+
plt.figure(figsize=(3,2))
5+
ax=plt.subplot(111)
6+
an1 = ax.annotate("Test 1", xy=(0.5, 0.5), xycoords="data",
7+
va="center", ha="center",
8+
bbox=dict(boxstyle="round", fc="w"))
9+
an2 = ax.annotate("Test 2", xy=(1, 0.5), xycoords=an1,
10+
xytext=(30,0), textcoords="offset points",
11+
va="center", ha="left",
12+
bbox=dict(boxstyle="round", fc="w"),
13+
arrowprops=dict(arrowstyle="->"))
14+
plt.show()
15+
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
2+
import matplotlib.pyplot as plt
3+
4+
plt.figure(figsize=(3,2))
5+
ax=plt.axes([0.1, 0.1, 0.8, 0.7])
6+
an1 = ax.annotate("Test 1", xy=(0.5, 0.5), xycoords="data",
7+
va="center", ha="center",
8+
bbox=dict(boxstyle="round", fc="w"))
9+
10+
an2 = ax.annotate("Test 2", xy=(0.5, 1.), xycoords=an1,
11+
xytext=(0.5,1.1), textcoords=(an1, "axes fraction"),
12+
va="bottom", ha="center",
13+
bbox=dict(boxstyle="round", fc="w"),
14+
arrowprops=dict(arrowstyle="->"))
15+
plt.show()
16+
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
2+
import matplotlib.pyplot as plt
3+
4+
plt.figure(figsize=(3,2))
5+
ax=plt.axes([0.1, 0.1, 0.8, 0.7])
6+
an1 = ax.annotate("Test 1", xy=(0.5, 0.5), xycoords="data",
7+
va="center", ha="center",
8+
bbox=dict(boxstyle="round", fc="w"))
9+
10+
from matplotlib.text import OffsetFrom
11+
offset_from = OffsetFrom(an1, (0.5, 0))
12+
an2 = ax.annotate("Test 2", xy=(0.1, 0.1), xycoords="data",
13+
xytext=(0, -10), textcoords=offset_from,
14+
# xytext is offset points from "xy=(0.5, 0), xycoords=an1"
15+
va="top", ha="center",
16+
bbox=dict(boxstyle="round", fc="w"),
17+
arrowprops=dict(arrowstyle="->"))
18+
plt.show()
19+

examples/pylab_examples/annotation_demo3.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -76,17 +76,16 @@
7676

7777
from matplotlib.text import OffsetFrom
7878

79-
ax2.annotate('xy=(0.5, 0)\nxycoords="bbox fraction"\nxybbox=artist',
80-
xy=(0.5, 0.), xycoords=t.get_window_extent,
79+
ax2.annotate('xy=(0.5, 0)\nxycoords=artist',
80+
xy=(0.5, 0.), xycoords=t,
8181
xytext=(0, -20), textcoords='offset points',
8282
ha="center", va="top",
8383
bbox=bbox_args,
8484
arrowprops=arrow_args
8585
)
8686

87-
ax2.annotate('xy=(0.8, 0.5)\nxycoords="bbox"\nxybbox=ax1.transData',
87+
ax2.annotate('xy=(0.8, 0.5)\nxycoords=ax1.transData',
8888
xy=(0.8, 0.5), xycoords=ax1.transData,
89-
#xytext=(0, 0), textcoords='data',
9089
xytext=(10, 10), textcoords=OffsetFrom(ax2.bbox, (0, 0), "points"),
9190
ha="left", va="bottom",
9291
bbox=bbox_args,

lib/matplotlib/text.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1423,7 +1423,9 @@ def __call__(self, renderer):
14231423
x, y = l+w*xf, b+h*yf
14241424
elif isinstance(self._artist, Transform):
14251425
x, y = self._artist.transform_point(self._ref_coord)
1426-
1426+
else:
1427+
raise RuntimeError("unknown type")
1428+
14271429
sc = self._get_scale(renderer)
14281430
tr = Affine2D().scale(sc, sc).translate(x, y)
14291431

@@ -1780,6 +1782,11 @@ def __init__(self, s, xy,
17801782
# 5 points below the top border
17811783
xy=(10,-5), xycoords='axes points'
17821784
1785+
You may use an instance of
1786+
:class:`~matplotlib.transforms.Transform` or
1787+
:class:`~matplotlib.artist.Artist`. See
1788+
:ref:`plotting-guide-annotation` for more details.
1789+
17831790
17841791
The *annotation_clip* attribute contols the visibility of the
17851792
annotation when it goes outside the axes area. If True, the

0 commit comments

Comments
 (0)