|
4 | 4 | Annotating Axes |
5 | 5 | **************** |
6 | 6 |
|
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 | + |
9 | 12 |
|
10 | 13 |
|
11 | 14 | Annotating with Text with Box |
@@ -182,31 +185,6 @@ lower-left corner and (1,1) means top-right. |
182 | 185 | .. plot:: users/plotting/examples/annotate_simple04.py |
183 | 186 |
|
184 | 187 |
|
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 | | - |
210 | 188 | Placing Artist at the anchored location of the Axes |
211 | 189 | =================================================== |
212 | 190 |
|
@@ -282,6 +260,111 @@ legend (as a matter of fact, this is how the legend is created). |
282 | 260 | Note that unlike the legend, the ``bbox_transform`` is set |
283 | 261 | to IdentityTransform by default. |
284 | 262 |
|
| 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 | + |
285 | 368 | Advanced Topics |
286 | 369 | *************** |
287 | 370 |
|
|
0 commit comments