Skip to content

Commit 2f263b5

Browse files
committed
Factorize plotting extent & add extent w/o affine transform
1 parent eaaa791 commit 2f263b5

File tree

1 file changed

+28
-9
lines changed

1 file changed

+28
-9
lines changed

examples/api/demo_affine_image.py

Lines changed: 28 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
"""
22
For the backends that supports draw_image with optional affine
33
transform (e.g., agg, ps backend), the image of the output should
4-
have its boundary matches the red dashed rectangle.
4+
have its boundary matches the red dashed rectangle. The extent of
5+
the image without affine transform is additionally displayed with
6+
a black solid rectangle.
57
"""
68

79
import numpy as np
8-
import matplotlib.cm as cm
910
import matplotlib.mlab as mlab
1011
import matplotlib.pyplot as plt
1112
import matplotlib.transforms as mtransforms
@@ -21,6 +22,25 @@ def get_image():
2122
return Z
2223

2324

25+
def plot_extent(im, rect_lw=1.5, ls="-", color="Black", transform=None):
26+
"""Draws a rectangle denoting the extent of an image `im` altered by a
27+
transform `transform`. Additional segment markers going through then
28+
origin are also plotted.
29+
30+
`rect_lw` is the linewidth parameter used to the rectangle.
31+
"""
32+
x1, x2, y1, y2 = im.get_extent()
33+
ax = im.axes
34+
if transform is None: # then no specific transform will be applied
35+
transform = ax.transData
36+
# Plot the extent rectangle
37+
ax.plot([x1, x2, x2, x1, x1], [y1, y1, y2, y2, y1], ls=ls, lw=rect_lw,
38+
color=color, transform=transform)
39+
# Plot the segments parallel to the rectangle sides & going through (0, 0)
40+
ax.plot([x1, x2], [0, 0], ls=ls, color=color, transform=transform)
41+
ax.plot([0, 0], [y1, y2], ls=ls, color=color, transform=transform)
42+
43+
2444
if 1:
2545

2646
fig, ax1 = plt.subplots(1, 1)
@@ -29,16 +49,15 @@ def get_image():
2949
origin='lower',
3050
extent=[-2, 4, -3, 2], clip_on=True)
3151

32-
# image rotation
52+
# Image rotation
3353
trans_data2 = mtransforms.Affine2D().rotate_deg(30) + ax1.transData
3454
im1.set_transform(trans_data2)
3555

36-
# display intended extent of the image
37-
x1, x2, y1, y2 = im1.get_extent()
38-
x3, y3 = x2, y1
39-
40-
ax1.plot([x1, x2, x2, x1, x1], [y1, y1, y2, y2, y1], "--r", lw=3,
41-
transform=trans_data2)
56+
# Plot the extent of the image:
57+
# 1/ With the affine transform.
58+
plot_extent(im1, ls="--", rect_lw=3, color="Red", transform=trans_data2)
59+
# 2/ Without the affine transform (see `plot_extent` defaults).
60+
plot_extent(im1)
4261

4362
ax1.set_xlim(-3, 5)
4463
ax1.set_ylim(-4, 4)

0 commit comments

Comments
 (0)