|
18 | 18 | ``ax`` is a :class:`~matplotlib.axes.Axes` instance, and ``fig`` is a
|
19 | 19 | :class:`~matplotlib.figure.Figure` instance.
|
20 | 20 |
|
21 |
| -+-----------+-----------------------------+-----------------------------------+ |
22 |
| -|Coordinates|Transformation object |Description | |
23 |
| -+-----------+-----------------------------+-----------------------------------+ |
24 |
| -|"data" |``ax.transData`` |The coordinate system for the data,| |
25 |
| -| | |controlled by xlim and ylim. | |
26 |
| -+-----------+-----------------------------+-----------------------------------+ |
27 |
| -|"axes" |``ax.transAxes`` |The coordinate system of the | |
28 |
| -| | |`~matplotlib.axes.Axes`; (0, 0) | |
29 |
| -| | |is bottom left of the axes, and | |
30 |
| -| | |(1, 1) is top right of the axes. | |
31 |
| -+-----------+-----------------------------+-----------------------------------+ |
32 |
| -|"figure" |``fig.transFigure`` |The coordinate system of the | |
33 |
| -| | |`.Figure`; (0, 0) is bottom left | |
34 |
| -| | |of the figure, and (1, 1) is top | |
35 |
| -| | |right of the figure. | |
36 |
| -+-----------+-----------------------------+-----------------------------------+ |
37 |
| -|"display" |``None``, or |The pixel coordinate system of the | |
38 |
| -| |``IdentityTransform()`` |display; (0, 0) is bottom left of | |
39 |
| -| | |the display, and (width, height) is| |
40 |
| -| | |top right of the display in pixels.| |
41 |
| -+-----------+-----------------------------+-----------------------------------+ |
42 |
| -|"xaxis", |``ax.get_xaxis_transform()``,|Blended coordinate systems; use | |
43 |
| -|"yaxis" |``ax.get_yaxis_transform()`` |data coordinates on one of the axis| |
44 |
| -| | |and axes coordinates on the other. | |
45 |
| -+-----------+-----------------------------+-----------------------------------+ |
| 21 | ++----------------+-----------------------------+-----------------------------------+ |
| 22 | +|Coordinates |Transformation object |Description | |
| 23 | ++----------------+-----------------------------+-----------------------------------+ |
| 24 | +|"data" |``ax.transData`` |The coordinate system for the data,| |
| 25 | +| | |controlled by xlim and ylim. | |
| 26 | ++----------------+-----------------------------+-----------------------------------+ |
| 27 | +|"axes" |``ax.transAxes`` |The coordinate system of the | |
| 28 | +| | |`~matplotlib.axes.Axes`; (0, 0) | |
| 29 | +| | |is bottom left of the axes, and | |
| 30 | +| | |(1, 1) is top right of the axes. | |
| 31 | ++----------------+-----------------------------+-----------------------------------+ |
| 32 | +|"figure" |``fig.transFigure`` |The coordinate system of the | |
| 33 | +| | |`.Figure`; (0, 0) is bottom left | |
| 34 | +| | |of the figure, and (1, 1) is top | |
| 35 | +| | |right of the figure. | |
| 36 | ++----------------+-----------------------------+-----------------------------------+ |
| 37 | +|"figure-inches" |``fig.dpi_scale_trans`` |The coordinate system of the | |
| 38 | +| | |`.Figure` in inches; (0, 0) is | |
| 39 | +| | |bottom left of the figure, and | |
| 40 | +| | |(width, height) is the top right | |
| 41 | +| | |of the figure ininches. | |
| 42 | ++----------------+-----------------------------+-----------------------------------+ |
| 43 | +|"display" |``None``, or |The pixel coordinate system of the | |
| 44 | +| |``IdentityTransform()`` |display window; (0, 0) is bottom | |
| 45 | +| | |left of the window, and (width, | |
| 46 | +| | |height) is top right of the | |
| 47 | +| | |display window in pixels. | |
| 48 | ++----------------+-----------------------------+-----------------------------------+ |
| 49 | +|"xaxis", |``ax.get_xaxis_transform()``,|Blended coordinate systems; use | |
| 50 | +|"yaxis" |``ax.get_yaxis_transform()`` |data coordinates on one of the axis| |
| 51 | +| | |and axes coordinates on the other. | |
| 52 | ++----------------+-----------------------------+-----------------------------------+ |
46 | 53 |
|
47 | 54 | All of the transformation objects in the table above take inputs in
|
48 |
| -their coordinate system, and transform the input to the `display` |
49 |
| -coordinate system. That is why the `display` coordinate system has |
50 |
| -`None` for the `Transformation Object` column -- it already is in |
| 55 | +their coordinate system, and transform the input to the ``display`` |
| 56 | +coordinate system. That is why the ``display`` coordinate system has |
| 57 | +``None`` for the ``Transformation Object`` column -- it already is in |
51 | 58 | display coordinates. The transformations also know how to invert
|
52 |
| -themselves, to go from `display` back to the native coordinate system. |
| 59 | +themselves, to go from ``display`` back to the native coordinate system. |
53 | 60 | This is particularly useful when processing events from the user
|
54 | 61 | interface, which typically occur in display space, and you want to
|
55 | 62 | know where the mouse click or key-press occurred in your data
|
|
71 | 78 |
|
72 | 79 | import numpy as np
|
73 | 80 | import matplotlib.pyplot as plt
|
| 81 | +import matplotlib.patches as mpatches |
74 | 82 |
|
75 | 83 | x = np.arange(0, 10, 0.005)
|
76 | 84 | y = np.exp(-x/2.) * np.sin(2*np.pi*x)
|
|
229 | 237 | # move, but the circle will remain fixed because it is not in `data`
|
230 | 238 | # coordinates and will always remain at the center of the axes.
|
231 | 239 |
|
232 |
| -import matplotlib.patches as patches |
233 |
| - |
234 | 240 | fig = plt.figure()
|
235 | 241 | ax = fig.add_subplot(111)
|
236 | 242 | x, y = 10*np.random.rand(2, 1000)
|
237 | 243 | ax.plot(x, y, 'go') # plot some data in data coordinates
|
238 | 244 |
|
239 |
| -circ = patches.Circle((0.5, 0.5), 0.25, transform=ax.transAxes, |
| 245 | +circ = mpatches.Circle((0.5, 0.5), 0.25, transform=ax.transAxes, |
240 | 246 | facecolor='yellow', alpha=0.5)
|
241 | 247 | ax.add_patch(circ)
|
242 | 248 | plt.show()
|
|
281 | 287 | # highlight the 1..2 stddev region with a span.
|
282 | 288 | # We want x to be in data coordinates and y to
|
283 | 289 | # span from 0..1 in axes coords
|
284 |
| -rect = patches.Rectangle((1, 0), width=1, height=1, |
| 290 | +rect = mpatches.Rectangle((1, 0), width=1, height=1, |
285 | 291 | transform=trans, color='yellow',
|
286 | 292 | alpha=0.5)
|
287 | 293 |
|
|
303 | 309 | #
|
304 | 310 | # trans = ax.get_xaxis_transform()
|
305 | 311 | #
|
| 312 | +# .. _transforms-fig-scale-dpi: |
| 313 | +# |
| 314 | +# Plotting in physical units |
| 315 | +# ========================== |
| 316 | +# |
| 317 | +# Sometimes we want an object to be a certain physical size on the plot, in |
| 318 | +# which case the ``fig.dpi_scale_trans`` should be used. The following |
| 319 | +# example draws a circle displaced one inch from the bottom-left corner that |
| 320 | +# is 0.5-inches in diameter. Note that if you run this code with an |
| 321 | +# interactive window and resize, the circle stays in the same spot. |
| 322 | + |
| 323 | +fig, ax = plt.subplots(figsize=(3, 3)) |
| 324 | +r = mpatches.Circle((1, 1), 1., transform=fig.dpi_scale_trans) |
| 325 | +fig.artists.append(r) |
| 326 | +plt.show() |
| 327 | + |
| 328 | +############################################################################### |
| 329 | +# Another use is putting a circle around a data point that is a certain size. |
| 330 | +# Though in this case if the figure is resized the circle doesn't follow the |
| 331 | +# data point. This is a good example of using ``inverted``: |
| 332 | +fig, ax = plt.subplots() |
| 333 | +xdata, ydata = (0.2, 0.7), (0.5, 0.5) |
| 334 | +ax.plot(xdata, ydata, "o") |
| 335 | +ax.set_xlim((0, 1)) |
| 336 | +# get the coordinates of the data point in inches from lower left corner: |
| 337 | +x, y = fig.dpi_scale_trans.inverted().transform( |
| 338 | + ax.transData.transform((xdata[0], ydata[0]))) |
| 339 | +# plot a circle around the point that is 50 points in diameter... |
| 340 | +circle = mpatches.Circle((x, y), 50/72, fill=None, |
| 341 | + transform=fig.dpi_scale_trans) |
| 342 | +# add to figure |
| 343 | +fig.artists.append(circle) |
| 344 | +plt.show() |
| 345 | + |
| 346 | +############################################################################### |
306 | 347 | # .. _offset-transforms-shadow:
|
307 | 348 | #
|
308 | 349 | # Using offset transforms to create a shadow effect
|
|
327 | 368 | #
|
328 | 369 | # where `xt` and `yt` are the translation offsets, and `scale_trans` is
|
329 | 370 | # a transformation which scales `xt` and `yt` at transformation time
|
330 |
| -# before applying the offsets. A typical use case is to use the figure |
| 371 | +# before applying the offsets. |
| 372 | +# |
| 373 | +# A typical use case is to use the figure |
331 | 374 | # ``fig.dpi_scale_trans`` transformation for the `scale_trans` argument,
|
332 | 375 | # to first scale `xt` and `yt` specified in points to `display` space
|
333 | 376 | # before doing the final offset. The dpi and inches offset is a
|
|
348 | 391 | # 1/72 inches, and by specifying your offsets in points, your figure
|
349 | 392 | # will look the same regardless of the dpi resolution it is saved in.
|
350 | 393 |
|
351 |
| -fig = plt.figure() |
352 |
| -ax = fig.add_subplot(111) |
| 394 | +fig, ax = plt.subplots() |
353 | 395 |
|
354 | 396 | # make a simple sine wave
|
355 | 397 | x = np.arange(0., 2., 0.01)
|
|
0 commit comments