1
1
"""
2
2
For the backends that supports draw_image with optional affine
3
3
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.
5
7
"""
6
8
7
9
import numpy as np
8
- import matplotlib .cm as cm
9
10
import matplotlib .mlab as mlab
10
11
import matplotlib .pyplot as plt
11
12
import matplotlib .transforms as mtransforms
@@ -21,6 +22,25 @@ def get_image():
21
22
return Z
22
23
23
24
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
+
24
44
if 1 :
25
45
26
46
fig ,ax1 = plt .subplots (1 ,1 )
@@ -29,16 +49,15 @@ def get_image():
29
49
origin = 'lower' ,
30
50
extent = [- 2 ,4 ,- 3 ,2 ],clip_on = True )
31
51
32
- #image rotation
52
+ #Image rotation
33
53
trans_data2 = mtransforms .Affine2D ().rotate_deg (30 )+ ax1 .transData
34
54
im1 .set_transform (trans_data2 )
35
55
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 )
42
61
43
62
ax1 .set_xlim (- 3 ,5 )
44
63
ax1 .set_ylim (- 4 ,4 )