20
20
from matplotlib .gridspec import GridSpec
21
21
22
22
23
+ def index_to_coordinate (index , extent , origin ):
24
+ """Return the pixel center of an index."""
25
+ left , right , bottom , top = extent
26
+
27
+ hshift = 0.5 * np .sign (right - left )
28
+ left , right = left + hshift , right - hshift
29
+ vshift = 0.5 * np .sign (top - bottom )
30
+ bottom , top = bottom + vshift , top - vshift
31
+
32
+ if origin == 'upper' :
33
+ bottom , top = top , bottom
34
+
35
+ return {
36
+ "[0, 0]" : (left , bottom ),
37
+ "[N', 0]" : (left , top ),
38
+ "[0, M']" : (right , bottom ),
39
+ "[N', M']" : (right , top ),
40
+ }[index ]
41
+
42
+
43
+ def get_index_label_pos (index , extent , origin , inverted_xindex ):
44
+ """
45
+ Return the desired position and horizontal alignment of an index label.
46
+ """
47
+ if extent is None :
48
+ extent = lookup_extent (origin )
49
+ left , right , bottom , top = extent
50
+ x , y = index_to_coordinate (index , extent , origin )
51
+
52
+ is_x0 = index [- 2 :] == "0]"
53
+ halign = 'left' if is_x0 ^ inverted_xindex else 'right'
54
+ hshift = 0.5 * np .sign (left - right )
55
+ x += hshift * (1 if is_x0 else - 1 )
56
+ return x , y , halign
57
+
58
+
59
+ def get_color (index , data , cmap ):
60
+ """Return the data color of an index."""
61
+ val = {
62
+ "[0, 0]" : data [0 , 0 ],
63
+ "[0, M']" : data [0 , - 1 ],
64
+ "[N', 0]" : data [- 1 , 0 ],
65
+ "[N', M']" : data [- 1 , - 1 ],
66
+ }[index ]
67
+ return cmap (val / data .max ())
68
+
69
+
70
+ def lookup_extent (origin ):
71
+ """Return extent for label positioning when not given explicitly."""
72
+ if origin == 'lower' :
73
+ return (- 0.5 , 6.5 , - 0.5 , 5.5 )
74
+ else :
75
+ return (- 0.5 , 6.5 , 5.5 , - 0.5 )
76
+
77
+
78
+ def set_extent_None_text (ax ):
79
+ ax .text (3 , 2.5 , 'equals\n extent=None' , size = 'large' ,
80
+ ha = 'center' , va = 'center' , color = 'w' )
81
+
82
+
23
83
def generate_imshow_demo_grid (extents , auto_limits ):
24
84
N = len (extents )
25
85
fig = plt .figure (tight_layout = True )
@@ -37,18 +97,6 @@ def generate_imshow_demo_grid(extents, auto_limits):
37
97
38
98
im = ax .imshow (d , origin = origin , extent = extent )
39
99
left , right , bottom , top = im .get_extent ()
40
- arrow_style = {'arrowprops' : {'arrowstyle' : '-|>' ,
41
- 'shrinkA' : 0 ,
42
- 'color' : '0.5' ,
43
- 'linewidth' : 3 }}
44
- ax .annotate ('' ,
45
- (left , bottom + 2 * np .sign (top - bottom )),
46
- (left , bottom ),
47
- ** arrow_style )
48
- ax .annotate ('' ,
49
- (left + 2 * np .sign (right - left ), bottom ),
50
- (left , bottom ),
51
- ** arrow_style )
52
100
53
101
if auto_limits or top > bottom :
54
102
upper_string , lower_string = 'top' , 'bottom'
@@ -57,8 +105,10 @@ def generate_imshow_demo_grid(extents, auto_limits):
57
105
58
106
if auto_limits or left < right :
59
107
port_string , starboard_string = 'left' , 'right'
108
+ inverted_xindex = False
60
109
else :
61
110
port_string , starboard_string = 'right' , 'left'
111
+ inverted_xindex = True
62
112
63
113
bbox_kwargs = {'fc' : 'w' , 'alpha' : .75 , 'boxstyle' : "round4" }
64
114
ann_kwargs = {'xycoords' : 'axes fraction' ,
@@ -78,6 +128,13 @@ def generate_imshow_demo_grid(extents, auto_limits):
78
128
79
129
ax .set_title ('origin: {origin}' .format (origin = origin ))
80
130
131
+ for index in ["[0, 0]" , "[0, M']" , "[N', 0]" , "[N', M']" ]:
132
+ tx , ty , halign = get_index_label_pos (index , extent , origin ,
133
+ inverted_xindex )
134
+ facecolor = get_color (index , d , im .get_cmap ())
135
+ ax .text (tx , ty , index , color = 'white' , ha = halign , va = 'center' ,
136
+ bbox = {'boxstyle' : 'square' , 'facecolor' : facecolor })
137
+
81
138
if not auto_limits :
82
139
ax .set_xlim (- 1 , 7 )
83
140
ax .set_ylim (- 1 , 6 )
@@ -89,7 +146,7 @@ def generate_imshow_demo_grid(extents, auto_limits):
89
146
'xy' : (1 , .5 )}
90
147
if extent is None :
91
148
ax .annotate ('None' , ** text_kwargs )
92
- ax .set_title ('` extent=` ' )
149
+ ax .set_title ('extent=' )
93
150
else :
94
151
left , right , bottom , top = extent
95
152
text = ('left: {left:0.1f}\n right: {right:0.1f}\n ' +
@@ -98,6 +155,7 @@ def generate_imshow_demo_grid(extents, auto_limits):
98
155
99
156
ax .annotate (text , ** text_kwargs )
100
157
ax .axis ('off' )
158
+ return columns
101
159
102
160
103
161
extents = (None ,
@@ -106,6 +164,8 @@ def generate_imshow_demo_grid(extents, auto_limits):
106
164
(6.5 , - 0.5 , - 0.5 , 5.5 ),
107
165
(6.5 , - 0.5 , 5.5 , - 0.5 ))
108
166
167
+
168
+
109
169
###############################################################################
110
170
#
111
171
#
@@ -153,7 +213,9 @@ def generate_imshow_demo_grid(extents, auto_limits):
153
213
# may invert the axis so they do not increase in the 'natural' direction.
154
214
#
155
215
156
- generate_imshow_demo_grid (extents [1 :], auto_limits = True )
216
+ columns = generate_imshow_demo_grid (extents [1 :], auto_limits = True )
217
+ set_extent_None_text (columns ['upper' ][1 ])
218
+ set_extent_None_text (columns ['lower' ][0 ])
157
219
158
220
159
221
###############################################################################
@@ -170,4 +232,6 @@ def generate_imshow_demo_grid(extents, auto_limits):
170
232
# - The 'left-right' and 'top-bottom' sense of the image is uncoupled from
171
233
# the orientation on the screen.
172
234
173
- generate_imshow_demo_grid (extents , auto_limits = False )
235
+ columns = generate_imshow_demo_grid (extents , auto_limits = False )
236
+ set_extent_None_text (columns ['upper' ][2 ])
237
+ set_extent_None_text (columns ['lower' ][1 ])
0 commit comments