@@ -94,138 +94,136 @@ def test_location_event_position():
94
94
assert isinstance (event .y ,int )
95
95
96
96
97
- class ButtonTest ():
98
- def __init__ (self ,fig_test ,fig_ref ,button_call ,plots = None ,
97
+ def ButtonTest (fig_test ,fig_ref ,button_call ,plots = None ,
99
98
layout = 'none' ):
100
99
101
- """
102
- Class for testing the home, back and forwards buttons togheter with
103
- layout managers for several types of "subplots".
100
+ """
101
+ Function for testing the home, back and forwards buttons togheter with
102
+ layout managers for several types of "subplots".
104
103
105
- Parameters
106
- ----------
107
- fig_test, fig_ref: `.figure.figures`
108
- The figure to compare
104
+ Parameters
105
+ ----------
106
+ fig_test, fig_ref: `.figure.figures`
107
+ The figure to compare
109
108
110
- button_call: {'home', 'back'}
111
- Which buttons to test
109
+ button_call: {'home', 'back'}
110
+ Which buttons to test
112
111
113
- plots: {'subplots', 'subplots_gridspec', 'add_subplopt', 'add_axes'\
112
+ plots: {'subplots', 'subplots_gridspec', 'add_subplopt', 'add_axes'\
114
113
'axesgrid'}
115
- Which plot configuration to test.
116
-
117
- pause : float
118
- The pause time between events.
119
- """
120
- self .rows_cols = 1 ,2
121
- self .figs = fig_test ,fig_ref
122
- self .trans = None ,None
123
- self .layout = layout
124
- self .zoom0 = (0.5 ,0.5 ), (0.5 ,0.5 )
125
- self .zoom1 = (0.5 ,0.5 ), (0.6 ,0.54 )
126
- self .zoom2 = (0.52 ,0.52 ), (0.58 ,0.53 )
127
-
128
- self .set_layout (True )
129
- self .add_subplots (plots )
130
- self .add_suptitle ('Test button: {}, axes: {}, layout manager: {}' .
131
- format (button_call ,plots ,layout ))
132
-
133
- if button_call == 'home' :
134
- self .home_button ()
135
- elif button_call == 'back' :
136
- self .back_button ()
137
-
138
- # set layout managers to False so that the figures don't change
139
- # during saving.
140
- self .set_layout (False )
141
-
142
- def add_suptitle (self ,title ):
143
- for fig in self .figs :
144
- fig .suptitle (title )
145
-
146
- def set_layout (self ,bool_ ):
147
- for fig in self .figs :
148
- if self .layout == 'constrained' :
149
- fig .set_constrained_layout (bool_ )
150
- elif self .layout == 'tight' :
151
- arg = dict (rect = (0 ,0 ,1 ,0.95 ))if bool_ else bool_
152
- fig .set_tight_layout (arg )
153
-
154
- def add_subplots (self ,plots ):
155
- rows ,cols = self .rows_cols
156
- self .trans = []
157
- for fig in self .figs :
158
- if plots == 'subplots' :
159
- ax = fig .subplots (rows ,cols ,squeeze = False )[rows - 1 ,cols - 1 ]
160
- elif plots == 'subplots_gridspec' :
161
- ax = fig .subplots (rows ,cols ,squeeze = False ,
162
- gridspec_kw = {'left' :0.01 })[rows - 1 ,cols - 1 ]
163
- elif plots == 'add_subplot' :
164
- for i in range (1 ,rows * cols + 1 ):
165
- ax = fig .add_subplot (rows ,cols ,i )
166
- elif plots == 'add_axes' :
167
- width = (1 - 0.1 )/ cols - 0.1
168
- height = (1 - 0.1 )/ rows - 0.1
169
- for i in range (rows ):
170
- for j in range (cols ):
171
- x0 = j * (width + 0.1 )+ 0.1
172
- y0 = i * (height + 0.1 )+ 0.1
173
- ax = fig .add_axes ((x0 ,y0 ,width ,height ))
174
- elif plots == 'axesgrid' :
175
- ax = AxesGrid (fig ,111 ,nrows_ncols = (2 ,2 ),
176
- axes_pad = 0.1 )[- 1 ]
177
-
178
- self .trans .append (ax .transData .transform )
179
- self .draw ()
180
-
181
- def draw (self ):
182
- for fig in self .figs :
183
- fig .canvas .flush_events ()
184
-
185
- def home_button (self ):
186
- """Zoom twice and get back to home with the home button."""
187
-
188
- fig_test ,fig_ref = self .figs
189
- trans_test ,trans_ref = self .trans
190
- #No zoom happens but this is sometimes necessary to get equal results
191
- self .zoom_event (fig_ref ,trans_ref ,self .zoom0 )
192
- self .zoom_event (fig_test ,trans_test ,self .zoom1 )
193
- self .zoom_event (fig_test ,trans_test ,self .zoom2 )
194
-
195
- self .move (fig_test ,'home' )
196
-
197
- def back_button (self ):
198
- """
199
- Zoom once in the ref figure, zoom twice in the test figure and use
200
- the back button twice followed by the forward button
201
- """
202
- fig_test ,fig_ref = self .figs
203
- trans_test ,trans_ref = self .trans
204
-
205
- self .zoom_event (fig_ref ,trans_ref ,self .zoom1 )
206
- self .zoom_event (fig_test ,trans_test ,self .zoom1 )
207
- self .zoom_event (fig_test ,trans_test ,self .zoom2 )
208
-
209
- self .move (fig_test ,'back' )
210
- self .move (fig_test ,'back' )
211
- self .move (fig_test ,'forward' )
212
-
213
- def zoom_event (self ,fig ,trans ,zoom ):
214
- """Simulate a zoom event from zoom[0] to zoom[1]"""
215
- xy1 ,xy2 = trans (zoom [0 ]),trans (zoom [1 ])
216
- press_zoom = MouseEvent ('' ,fig .canvas ,xy1 [0 ],xy1 [1 ],button = 1 )
217
- drag = MouseEvent ('' ,fig .canvas ,xy2 [0 ],xy2 [1 ],button = 1 )
218
- release_zoom = MouseEvent ('' ,fig .canvas ,xy2 [0 ],xy2 [1 ],button = 1 )
219
-
220
- fig .canvas .toolbar .press_zoom (press_zoom )
221
- fig .canvas .toolbar .drag_zoom (drag )
222
- fig .canvas .toolbar .release_zoom (release_zoom )
223
- self .draw ()
224
-
225
- def move (self ,fig ,direction ):
226
- """Simulate the back or forward button on fig"""
227
- getattr (fig .canvas .toolbar ,direction )()
228
- self .draw ()
114
+ Which plot configuration to test.
115
+
116
+ pause : float
117
+ The pause time between events.
118
+ """
119
+ rows_cols = 1 ,2
120
+ figs = fig_test ,fig_ref
121
+ zoom = [None ]* 3
122
+ zoom [0 ]= (0.5 ,0.5 ), (0.5 ,0.5 )
123
+ zoom [1 ]= (0.5 ,0.5 ), (0.6 ,0.54 )
124
+ zoom [2 ]= (0.52 ,0.52 ), (0.58 ,0.53 )
125
+
126
+ set_layout (figs ,layout ,True )
127
+ trans = add_subplots (figs ,plots ,rows_cols )
128
+ add_suptitle (figs ,'Test button: {}, axes: {}, layout manager: {}' .
129
+ format (button_call ,plots ,layout ))
130
+
131
+ if button_call == 'home' :
132
+ home_button (figs ,trans ,zoom )
133
+ elif button_call == 'back' :
134
+ back_button (figs ,trans ,zoom )
135
+
136
+ # set layout managers to False so that the figures don't change
137
+ # during saving.
138
+ set_layout (figs ,layout ,False )
139
+
140
+ def add_suptitle (figs ,title ):
141
+ for fig in figs :
142
+ fig .suptitle (title )
143
+
144
+ def set_layout (figs ,layout ,bool_ ):
145
+ for fig in figs :
146
+ if layout == 'constrained' :
147
+ fig .set_constrained_layout (bool_ )
148
+ elif layout == 'tight' :
149
+ arg = dict (rect = (0 ,0 ,1 ,0.95 ))if bool_ else bool_
150
+ fig .set_tight_layout (arg )
151
+
152
+ def add_subplots (figs ,plots ,rows_cols ):
153
+ rows ,cols = rows_cols
154
+ trans = []
155
+ for fig in figs :
156
+ if plots == 'subplots' :
157
+ ax = fig .subplots (rows ,cols ,squeeze = False )[rows - 1 ,cols - 1 ]
158
+ elif plots == 'subplots_gridspec' :
159
+ ax = fig .subplots (rows ,cols ,squeeze = False ,
160
+ gridspec_kw = {'left' :0.01 })[rows - 1 ,cols - 1 ]
161
+ elif plots == 'add_subplot' :
162
+ for i in range (1 ,rows * cols + 1 ):
163
+ ax = fig .add_subplot (rows ,cols ,i )
164
+ elif plots == 'add_axes' :
165
+ width = (1 - 0.1 )/ cols - 0.1
166
+ height = (1 - 0.1 )/ rows - 0.1
167
+ for i in range (rows ):
168
+ for j in range (cols ):
169
+ x0 = j * (width + 0.1 )+ 0.1
170
+ y0 = i * (height + 0.1 )+ 0.1
171
+ ax = fig .add_axes ((x0 ,y0 ,width ,height ))
172
+ elif plots == 'axesgrid' :
173
+ ax = AxesGrid (fig ,111 ,nrows_ncols = (2 ,2 ),
174
+ axes_pad = 0.1 )[- 1 ]
175
+
176
+ trans .append (ax .transData .transform )
177
+ draw (fig )
178
+ return trans
179
+
180
+ def draw (fig ):
181
+ fig .canvas .flush_events ()
182
+
183
+ def home_button (figs ,trans ,zoom ):
184
+ """Zoom twice and get back to home with the home button."""
185
+
186
+ fig_test ,fig_ref = figs
187
+ trans_test ,trans_ref = trans
188
+ #No zoom happens but this is sometimes necessary to get equal results
189
+ zoom_event (fig_ref ,trans_ref ,zoom [0 ])
190
+ zoom_event (fig_test ,trans_test ,zoom [1 ])
191
+ zoom_event (fig_test ,trans_test ,zoom [2 ])
192
+
193
+ move (fig_test ,'home' )
194
+
195
+ def back_button (figs ,trans ,zoom ):
196
+ """
197
+ Zoom once in the ref figure, zoom twice in the test figure and use
198
+ the back button twice followed by the forward button
199
+ """
200
+ fig_test ,fig_ref = figs
201
+ trans_test ,trans_ref = trans
202
+
203
+ zoom_event (fig_ref ,trans_ref ,zoom [1 ])
204
+ zoom_event (fig_test ,trans_test ,zoom [1 ])
205
+ zoom_event (fig_test ,trans_test ,zoom [2 ])
206
+
207
+ move (fig_test ,'back' )
208
+ move (fig_test ,'back' )
209
+ move (fig_test ,'forward' )
210
+
211
+ def zoom_event (fig ,trans ,zoom ):
212
+ """Simulate a zoom event from zoom[0] to zoom[1]"""
213
+ xy1 ,xy2 = trans (zoom [0 ]),trans (zoom [1 ])
214
+ press_zoom = MouseEvent ('' ,fig .canvas ,xy1 [0 ],xy1 [1 ],button = 1 )
215
+ drag = MouseEvent ('' ,fig .canvas ,xy2 [0 ],xy2 [1 ],button = 1 )
216
+ release_zoom = MouseEvent ('' ,fig .canvas ,xy2 [0 ],xy2 [1 ],button = 1 )
217
+
218
+ fig .canvas .toolbar .press_zoom (press_zoom )
219
+ fig .canvas .toolbar .drag_zoom (drag )
220
+ fig .canvas .toolbar .release_zoom (release_zoom )
221
+ draw (fig )
222
+
223
+ def move (fig ,direction ):
224
+ """Simulate the back or forward button on fig"""
225
+ getattr (fig .canvas .toolbar ,direction )()
226
+ draw (fig )
229
227
230
228
231
229
"""