5
5
resize_colorbar_horz ,
6
6
_parse_shift_shrink )
7
7
8
-
8
+ import matplotlib .pyplot as plt
9
+ import numpy as np
9
10
10
11
11
12
def test_parse_shift_shrink ():
@@ -32,3 +33,200 @@ def test_parse_shift_shrink():
32
33
33
34
with raises (AssertionError ):
34
35
_parse_shift_shrink (0 , 1.1 )
36
+
37
+
38
+ def _easy_cbar_vert (** kwargs ):
39
+
40
+ f , ax = plt .subplots ()
41
+
42
+ f .subplots_adjust (left = 0 , bottom = 0 , right = 0.8 , top = 1 )
43
+
44
+ # simplest 'mappable'
45
+ h = ax .pcolormesh ([[0 , 1 ]])
46
+
47
+ # create colorbar
48
+ cbax = f .add_axes ([0 , 0 , 0.1 , 0.1 ])
49
+
50
+ cbar = plt .colorbar (h , orientation = 'vertical' , cax = cbax )
51
+
52
+ func = resize_colorbar_vert (cbax , ax , ** kwargs )
53
+ f .canvas .mpl_connect ('draw_event' , func )
54
+
55
+ f .canvas .draw ()
56
+
57
+ return f , cbar
58
+
59
+
60
+ def test_resize_colorbar_vert ():
61
+
62
+
63
+ # test pad=0, size=0.2
64
+ f , cbar = _easy_cbar_vert (size = 0.2 , pad = 0 )
65
+
66
+ pos = cbar .ax .get_position ()
67
+ res = [pos .x0 , pos .y0 , pos .width , pos .height ]
68
+ exp = [0.8 , 0 , 0.2 , 1. ]
69
+
70
+ np .testing .assert_allclose (res , exp )
71
+
72
+ # -----------------------------------------------------------
73
+
74
+ f .subplots_adjust (left = 0 , bottom = 0.1 , right = 0.8 , top = 0.9 )
75
+ f .canvas .draw ()
76
+
77
+ pos = cbar .ax .get_position ()
78
+ res = [pos .x0 , pos .y0 , pos .width , pos .height ]
79
+ exp = [0.8 , 0.1 , 0.2 , 0.8 ]
80
+
81
+ np .testing .assert_allclose (res , exp )
82
+
83
+ # ===========================================================
84
+
85
+ # pad=0.05, size=0.1
86
+
87
+ f , cbar = _easy_cbar_vert (size = 0.1 , pad = 0.05 )
88
+
89
+ pos = cbar .ax .get_position ()
90
+ res = [pos .x0 , pos .y0 , pos .width , pos .height ]
91
+ exp = [0.85 , 0 , 0.1 , 1. ]
92
+
93
+ np .testing .assert_allclose (res , exp )
94
+
95
+ # ===========================================================
96
+
97
+ # shift='symmetric', shrink=0.1
98
+ # --> colorbar is 10 % smaller, and centered
99
+
100
+ f , cbar = _easy_cbar_vert (size = 0.2 , pad = 0. , shrink = 0.1 )
101
+
102
+ pos = cbar .ax .get_position ()
103
+ res = [pos .x0 , pos .y0 , pos .width , pos .height ]
104
+ exp = [0.8 , 0.05 , 0.2 , 0.9 ]
105
+
106
+ np .testing .assert_allclose (res , exp )
107
+
108
+ # ===========================================================
109
+
110
+ # shift=0., shrink=0.1
111
+ # --> colorbar is 10 % smaller, and aligned with the bottom
112
+
113
+ f , cbar = _easy_cbar_vert (size = 0.2 , pad = 0. , shrink = 0.1 , shift = 0. )
114
+
115
+ pos = cbar .ax .get_position ()
116
+ res = [pos .x0 , pos .y0 , pos .width , pos .height ]
117
+ exp = [0.8 , 0.0 , 0.2 , 0.9 ]
118
+
119
+ np .testing .assert_allclose (res , exp )
120
+
121
+ # ===========================================================
122
+
123
+ # shift=0.1, shrink=None
124
+ # --> colorbar is 10 % smaller, and aligned with the top
125
+
126
+ f , cbar = _easy_cbar_vert (size = 0.2 , pad = 0. , shrink = None , shift = 0.1 )
127
+
128
+ pos = cbar .ax .get_position ()
129
+ res = [pos .x0 , pos .y0 , pos .width , pos .height ]
130
+ exp = [0.8 , 0.1 , 0.2 , 0.9 ]
131
+
132
+ np .testing .assert_allclose (res , exp )
133
+
134
+ # =============================================================================
135
+ # =============================================================================
136
+
137
+ def _easy_cbar_horz (** kwargs ):
138
+
139
+ f , ax = plt .subplots ()
140
+
141
+ f .subplots_adjust (left = 0 , bottom = 0.2 , right = 1 , top = 1 )
142
+
143
+ # simplest 'mappable'
144
+ h = ax .pcolormesh ([[0 , 1 ]])
145
+
146
+ # create colorbar
147
+ cbax = f .add_axes ([0 , 0 , 0.1 , 0.1 ])
148
+
149
+ cbar = plt .colorbar (h , orientation = 'horizontal' , cax = cbax )
150
+
151
+ func = resize_colorbar_horz (cbax , ax , ** kwargs )
152
+ f .canvas .mpl_connect ('draw_event' , func )
153
+
154
+ f .canvas .draw ()
155
+
156
+ return f , cbar
157
+
158
+
159
+ def test_resize_colorbar_horz ():
160
+
161
+
162
+ # test pad=0, size=0.2
163
+ f , cbar = _easy_cbar_horz (size = 0.2 , pad = 0 )
164
+
165
+ pos = cbar .ax .get_position ()
166
+ res = [pos .x0 , pos .y0 , pos .width , pos .height ]
167
+ exp = [0 , 0 , 1 , 0.2 ]
168
+
169
+ # adding atol because else 5e-17 != 0
170
+ np .testing .assert_allclose (res , exp , atol = 1e-08 )
171
+
172
+ # -----------------------------------------------------------
173
+
174
+ f .subplots_adjust (left = 0.1 , bottom = 0.2 , right = 0.9 , top = 1 )
175
+ f .canvas .draw ()
176
+
177
+ pos = cbar .ax .get_position ()
178
+ res = [pos .x0 , pos .y0 , pos .width , pos .height ]
179
+ exp = [0.1 , 0 , 0.8 , 0.2 ]
180
+
181
+ np .testing .assert_allclose (res , exp , atol = 1e-08 )
182
+
183
+ # ===========================================================
184
+
185
+ # pad=0.05, size=0.1
186
+
187
+ f , cbar = _easy_cbar_horz (size = 0.1 , pad = 0.05 )
188
+
189
+ pos = cbar .ax .get_position ()
190
+ res = [pos .x0 , pos .y0 , pos .width , pos .height ]
191
+ exp = [0.0 , 0.05 , 1 , 0.1 ]
192
+
193
+ np .testing .assert_allclose (res , exp , atol = 1e-08 )
194
+
195
+ # ===========================================================
196
+
197
+ # shift='symmetric', shrink=0.1
198
+ # --> colorbar is 10 % smaller, and centered
199
+
200
+ f , cbar = _easy_cbar_horz (size = 0.2 , pad = 0. , shrink = 0.1 )
201
+
202
+ pos = cbar .ax .get_position ()
203
+ res = [pos .x0 , pos .y0 , pos .width , pos .height ]
204
+ exp = [0.05 , 0 , 0.9 , 0.2 ]
205
+
206
+ np .testing .assert_allclose (res , exp , atol = 1e-08 )
207
+
208
+ # ===========================================================
209
+
210
+ # shift=0., shrink=0.1
211
+ # --> colorbar is 10 % smaller, and aligned with lhs
212
+
213
+ f , cbar = _easy_cbar_horz (size = 0.2 , pad = 0. , shrink = 0.1 , shift = 0. )
214
+
215
+ pos = cbar .ax .get_position ()
216
+ res = [pos .x0 , pos .y0 , pos .width , pos .height ]
217
+ exp = [0.0 , 0 , 0.9 , 0.2 ]
218
+
219
+ np .testing .assert_allclose (res , exp , atol = 1e-08 )
220
+
221
+ # ===========================================================
222
+
223
+ # shift=0.1, shrink=None
224
+ # --> colorbar is 10 % smaller, and aligned with rhs
225
+
226
+ f , cbar = _easy_cbar_horz (size = 0.2 , pad = 0. , shrink = None , shift = 0.1 )
227
+
228
+ pos = cbar .ax .get_position ()
229
+ res = [pos .x0 , pos .y0 , pos .width , pos .height ]
230
+ exp = [0.1 , 0 , 0.9 , 0.2 ]
231
+
232
+ np .testing .assert_allclose (res , exp , atol = 1e-08 )
0 commit comments