Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 23797b9

Browse filesBrowse files
committed
DOC: add infor to transforms tutorial about fig.dpi_scale_trans
1 parent 4d1ca36 commit 23797b9
Copy full SHA for 23797b9

File tree

Expand file treeCollapse file tree

2 files changed

+108
-38
lines changed
Filter options
Expand file treeCollapse file tree

2 files changed

+108
-38
lines changed

‎lib/matplotlib/axes/_base.py

Copy file name to clipboardExpand all lines: lib/matplotlib/axes/_base.py
+4Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1811,6 +1811,10 @@ def add_artist(self, a):
18111811
to manually update the dataLim if the artist is to be included in
18121812
autoscaling.
18131813
1814+
If no ``transform`` has been specified when creating the artist (e.g.
1815+
``artist.get_transform()==None``) then the transform is set to
1816+
``ax.transData``.
1817+
18141818
Returns the artist.
18151819
"""
18161820
a.axes = self

‎tutorials/advanced/transforms_tutorial.py

Copy file name to clipboardExpand all lines: tutorials/advanced/transforms_tutorial.py
+104-38Lines changed: 104 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -18,43 +18,59 @@
1818
``ax`` is a :class:`~matplotlib.axes.Axes` instance, and ``fig`` is a
1919
:class:`~matplotlib.figure.Figure` instance.
2020
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+
+----------------+-----------------------------+-----------------------------------+
4653
4754
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
5158
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.
5360
This is particularly useful when processing events from the user
5461
interface, which typically occur in display space, and you want to
5562
know where the mouse click or key-press occurred in your data
5663
coordinate system.
5764
65+
Note that specifying objects in ``display`` coordinates will change their
66+
location if the ``dpi`` of the figure changes. This can cause confusion when
67+
printing or changing screen resolution, because the object can change location
68+
and size. Therefore it is most common
69+
for artists placed in an axes or figure to have their transform set to
70+
something *other* than the ``IdentityTransform()``; the default when an artist
71+
is placed on an axes using ``~Axes.axes.add_artist`` is for the transform to be
72+
`ax.transData`.
73+
5874
.. _data-coords:
5975
6076
Data coordinates
@@ -71,6 +87,7 @@
7187

7288
import numpy as np
7389
import matplotlib.pyplot as plt
90+
import matplotlib.patches as mpatches
7491

7592
x = np.arange(0, 10, 0.005)
7693
y = np.exp(-x/2.) * np.sin(2*np.pi*x)
@@ -143,14 +160,12 @@
143160
(xdata, ydata), xytext=(-2*offset, offset), textcoords='offset points',
144161
bbox=bbox, arrowprops=arrowprops)
145162

146-
147163
disp = ax.annotate('display = (%.1f, %.1f)' % (xdisplay, ydisplay),
148164
(xdisplay, ydisplay), xytext=(0.5*offset, -offset),
149165
xycoords='figure pixels',
150166
textcoords='offset points',
151167
bbox=bbox, arrowprops=arrowprops)
152168

153-
154169
plt.show()
155170

156171
###############################################################################
@@ -229,14 +244,12 @@
229244
# move, but the circle will remain fixed because it is not in `data`
230245
# coordinates and will always remain at the center of the axes.
231246

232-
import matplotlib.patches as patches
233-
234247
fig = plt.figure()
235248
ax = fig.add_subplot(111)
236249
x, y = 10*np.random.rand(2, 1000)
237250
ax.plot(x, y, 'go') # plot some data in data coordinates
238251

239-
circ = patches.Circle((0.5, 0.5), 0.25, transform=ax.transAxes,
252+
circ = mpatches.Circle((0.5, 0.5), 0.25, transform=ax.transAxes,
240253
facecolor='yellow', alpha=0.5)
241254
ax.add_patch(circ)
242255
plt.show()
@@ -281,7 +294,7 @@
281294
# highlight the 1..2 stddev region with a span.
282295
# We want x to be in data coordinates and y to
283296
# span from 0..1 in axes coords
284-
rect = patches.Rectangle((1, 0), width=1, height=1,
297+
rect = mpatches.Rectangle((1, 0), width=1, height=1,
285298
transform=trans, color='yellow',
286299
alpha=0.5)
287300

@@ -303,6 +316,58 @@
303316
#
304317
# trans = ax.get_xaxis_transform()
305318
#
319+
# .. _transforms-fig-scale-dpi:
320+
#
321+
# Plotting in physical units
322+
# ==========================
323+
#
324+
# Sometimes we want an object to be a certain physical size on the plot.
325+
# Here we draw the same circle as above, but in physical units. If done
326+
# interactively, you can see that changing the size of the figure does
327+
# not change the offset of the circle from the lower-left corner,
328+
# does not change its size, and the circle remains a circle regardless of
329+
# the aspect ratio of the axes.
330+
331+
fig, ax = plt.subplots(figsize=(5, 4))
332+
x, y = 10*np.random.rand(2, 1000)
333+
ax.plot(x, y*10., 'go') # plot some data in data coordinates
334+
# add a circle in fixed-units
335+
circ = mpatches.Circle((2.5, 2), 1.0, transform=fig.dpi_scale_trans,
336+
facecolor='yellow', alpha=0.5)
337+
ax.add_patch(circ)
338+
plt.show()
339+
340+
fig, ax = plt.subplots(figsize=(7, 2))
341+
x, y = 10*np.random.rand(2, 1000)
342+
ax.plot(x, y*10., 'go') # plot some data in data coordinates
343+
# add a circle in fixed-units
344+
circ = mpatches.Circle((2.5, 2), 1.0, transform=fig.dpi_scale_trans,
345+
facecolor='yellow', alpha=0.5)
346+
ax.add_patch(circ)
347+
plt.show()
348+
349+
###############################################################################
350+
# Another use is putting a patch with a set physical dimmension around a
351+
# data point on the axes. We use ``inverted`` to decide where to put the
352+
# patch,
353+
# though in this case if the figure is resized the ellipse doesn't follow the
354+
# data point:
355+
fig, ax = plt.subplots()
356+
xdata, ydata = (0.2, 0.7), (0.5, 0.5)
357+
ax.plot(xdata, ydata, "o")
358+
ax.set_xlim((0, 1))
359+
# get the coordinates of the data point in inches from lower left corner:
360+
x, y = fig.dpi_scale_trans.inverted().transform(
361+
ax.transData.transform((xdata[0], ydata[0])))
362+
363+
# plot an ellipse around the point that is 150 x 130 points in diameter...
364+
circle = mpatches.Ellipse((x, y), 150/72, 130/72, angle=40, fill=None,
365+
transform=fig.dpi_scale_trans)
366+
# add to figure
367+
fig.artists.append(circle)
368+
plt.show()
369+
370+
###############################################################################
306371
# .. _offset-transforms-shadow:
307372
#
308373
# Using offset transforms to create a shadow effect
@@ -327,7 +392,9 @@
327392
#
328393
# where `xt` and `yt` are the translation offsets, and `scale_trans` is
329394
# a transformation which scales `xt` and `yt` at transformation time
330-
# before applying the offsets. A typical use case is to use the figure
395+
# before applying the offsets.
396+
#
397+
# A typical use case is to use the figure
331398
# ``fig.dpi_scale_trans`` transformation for the `scale_trans` argument,
332399
# to first scale `xt` and `yt` specified in points to `display` space
333400
# before doing the final offset. The dpi and inches offset is a
@@ -348,8 +415,7 @@
348415
# 1/72 inches, and by specifying your offsets in points, your figure
349416
# will look the same regardless of the dpi resolution it is saved in.
350417

351-
fig = plt.figure()
352-
ax = fig.add_subplot(111)
418+
fig, ax = plt.subplots()
353419

354420
# make a simple sine wave
355421
x = np.arange(0., 2., 0.01)

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.