2020from matplotlib .gridspec import GridSpec
2121
2222
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+
2383def generate_imshow_demo_grid (extents , auto_limits ):
2484 N = len (extents )
2585 fig = plt .figure (tight_layout = True )
@@ -37,18 +97,6 @@ def generate_imshow_demo_grid(extents, auto_limits):
3797
3898 im = ax .imshow (d , origin = origin , extent = extent )
3999 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 )
52100
53101 if auto_limits or top > bottom :
54102 upper_string , lower_string = 'top' , 'bottom'
@@ -57,8 +105,10 @@ def generate_imshow_demo_grid(extents, auto_limits):
57105
58106 if auto_limits or left < right :
59107 port_string , starboard_string = 'left' , 'right'
108+ inverted_xindex = False
60109 else :
61110 port_string , starboard_string = 'right' , 'left'
111+ inverted_xindex = True
62112
63113 bbox_kwargs = {'fc' : 'w' , 'alpha' : .75 , 'boxstyle' : "round4" }
64114 ann_kwargs = {'xycoords' : 'axes fraction' ,
@@ -78,6 +128,13 @@ def generate_imshow_demo_grid(extents, auto_limits):
78128
79129 ax .set_title ('origin: {origin}' .format (origin = origin ))
80130
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+
81138 if not auto_limits :
82139 ax .set_xlim (- 1 , 7 )
83140 ax .set_ylim (- 1 , 6 )
@@ -89,7 +146,7 @@ def generate_imshow_demo_grid(extents, auto_limits):
89146 'xy' : (1 , .5 )}
90147 if extent is None :
91148 ax .annotate ('None' , ** text_kwargs )
92- ax .set_title ('` extent=` ' )
149+ ax .set_title ('extent=' )
93150 else :
94151 left , right , bottom , top = extent
95152 text = ('left: {left:0.1f}\n right: {right:0.1f}\n ' +
@@ -98,6 +155,7 @@ def generate_imshow_demo_grid(extents, auto_limits):
98155
99156 ax .annotate (text , ** text_kwargs )
100157 ax .axis ('off' )
158+ return columns
101159
102160
103161extents = (None ,
@@ -106,6 +164,8 @@ def generate_imshow_demo_grid(extents, auto_limits):
106164 (6.5 , - 0.5 , - 0.5 , 5.5 ),
107165 (6.5 , - 0.5 , 5.5 , - 0.5 ))
108166
167+
168+
109169###############################################################################
110170#
111171#
@@ -153,7 +213,9 @@ def generate_imshow_demo_grid(extents, auto_limits):
153213# may invert the axis so they do not increase in the 'natural' direction.
154214#
155215
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 ])
157219
158220
159221###############################################################################
@@ -170,4 +232,6 @@ def generate_imshow_demo_grid(extents, auto_limits):
170232# - The 'left-right' and 'top-bottom' sense of the image is uncoupled from
171233# the orientation on the screen.
172234
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