@@ -114,11 +114,16 @@ def add_linear_selector(
114114
115115 Parameters
116116 ----------
117- selection: float
118- initial position of the selector
117+ Parameters
118+ ----------
119+ selection: float, optional
120+ selected point on the linear selector, computed from data if not provided
121+
122+ axis: str, default "x"
123+ axis that the selector resides on
119124
120- padding: float
121- pad the length of the selector
125+ padding: float, default 0.0
126+ Extra padding to extend the linear selector along the orthogonal axis to make it easier to interact with.
122127
123128 kwargs
124129 passed to :class:`.LinearSelector`
@@ -129,32 +134,10 @@ def add_linear_selector(
129134
130135 """
131136
132- data = self .data .value [~ np .any (np .isnan (self .data .value ), axis = 1 )]
133-
134- if axis == "x" :
135- # xvals
136- axis_vals = data [:, 0 ]
137-
138- # yvals to get size and center
139- magn_vals = data [:, 1 ]
140- elif axis == "y" :
141- axis_vals = data [:, 1 ]
142- magn_vals = data [:, 0 ]
137+ bounds_init , limits , size , center = self ._get_linear_selector_init_args (axis , padding )
143138
144139 if selection is None :
145- selection = axis_vals [0 ]
146- limits = axis_vals [0 ], axis_vals [- 1 ]
147-
148- if not limits [0 ] <= selection <= limits [1 ]:
149- raise ValueError (
150- f"the passed selection: { selection } is beyond the limits: { limits } "
151- )
152-
153- # width or height of selector
154- size = int (np .ptp (magn_vals ) * 1.5 + padding )
155-
156- # center of selector along the other axis
157- center = np .nanmean (magn_vals )
140+ selection = bounds_init [0 ]
158141
159142 selector = LinearSelector (
160143 selection = selection ,
@@ -174,16 +157,26 @@ def add_linear_selector(
174157 return weakref .proxy (selector )
175158
176159 def add_linear_region_selector (
177- self , padding : float = 0.0 , axis : str = "x" , ** kwargs
160+ self ,
161+ selection : tuple [float , float ] = None ,
162+ padding : float = 0.0 ,
163+ axis : str = "x" ,
164+ ** kwargs
178165 ) -> LinearRegionSelector :
179166 """
180167 Add a :class:`.LinearRegionSelector`. Selectors are just ``Graphic`` objects, so you can manage,
181168 remove, or delete them from a plot area just like any other ``Graphic``.
182169
183170 Parameters
184171 ----------
185- padding: float, default 100.0
186- Extends the linear selector along the y-axis to make it easier to interact with.
172+ selection: (float, float), optional
173+ the starting bounds of the linear region selector, computed from data if not provided
174+
175+ axis: str, default "x"
176+ axis that the selector resides on
177+
178+ padding: float, default 0.0
179+ Extra padding to extend the linear region selector along the orthogonal axis to make it easier to interact with.
187180
188181 kwargs
189182 passed to ``LinearRegionSelector``
@@ -195,34 +188,14 @@ def add_linear_region_selector(
195188
196189 """
197190
198- n_datapoints = self .data .value .shape [0 ]
199- value_25p = int (n_datapoints / 4 )
191+ bounds_init , limits , size , center = self ._get_linear_selector_init_args (axis , padding )
200192
201- # remove any nans
202- data = self .data .value [~ np .any (np .isnan (self .data .value ), axis = 1 )]
203-
204- if axis == "x" :
205- # xvals
206- axis_vals = data [:, 0 ]
207-
208- # yvals to get size and center
209- magn_vals = data [:, 1 ]
210- elif axis == "y" :
211- axis_vals = data [:, 1 ]
212- magn_vals = data [:, 0 ]
213-
214- bounds_init = axis_vals [0 ], axis_vals [value_25p ]
215- limits = axis_vals [0 ], axis_vals [- 1 ]
216-
217- # width or height of selector
218- size = int (np .ptp (magn_vals ) * 1.5 + padding )
219-
220- # center of selector along the other axis
221- center = np .nanmean (magn_vals )
193+ if selection is None :
194+ selection = bounds_init
222195
223196 # create selector
224197 selector = LinearRegionSelector (
225- selection = bounds_init ,
198+ selection = selection ,
226199 limits = limits ,
227200 size = size ,
228201 center = center ,
@@ -241,44 +214,31 @@ def add_linear_region_selector(
241214 return weakref .proxy (selector )
242215
243216 # TODO: this method is a bit of a mess, can refactor later
244- def _get_linear_selector_init_args (self , padding : float , ** kwargs ):
245- # computes initial bounds, limits, size and origin of linear selectors
246- data = self .data .value
217+ def _get_linear_selector_init_args (self , axis : str , padding ) -> tuple [tuple [float , float ], tuple [float , float ], float , float ]:
218+ # computes args to create selectors
219+ n_datapoints = self .data .value .shape [0 ]
220+ value_25p = int (n_datapoints / 4 )
247221
248- if "axis" in kwargs .keys ():
249- axis = kwargs ["axis" ]
250- else :
251- axis = "x"
222+ # remove any nans
223+ data = self .data .value [~ np .any (np .isnan (self .data .value ), axis = 1 )]
252224
253225 if axis == "x" :
254- offset = self .offset [0 ]
255- # x limits
256- limits = (data [0 , 0 ] + offset , data [- 1 , 0 ] + offset )
257-
258- # height + padding
259- size = np .ptp (data [:, 1 ]) + padding
260-
261- # initial position of the selector
262- position_y = (data [:, 1 ].min () + data [:, 1 ].max ()) / 2
263-
264- # need y offset too for this
265- origin = (limits [0 ] - offset , position_y + self .offset [1 ])
266-
267- else :
268- offset = self .offset [1 ]
269- # y limits
270- limits = (data [0 , 1 ] + offset , data [- 1 , 1 ] + offset )
226+ # xvals
227+ axis_vals = data [:, 0 ]
271228
272- # width + padding
273- size = np .ptp (data [:, 0 ]) + padding
229+ # yvals to get size and center
230+ magn_vals = data [:, 1 ]
231+ elif axis == "y" :
232+ axis_vals = data [:, 1 ]
233+ magn_vals = data [:, 0 ]
274234
275- # initial position of the selector
276- position_x = ( data [:, 0 ]. min () + data [:, 0 ]. max ()) / 2
235+ bounds_init = axis_vals [ 0 ], axis_vals [ value_25p ]
236+ limits = axis_vals [ 0 ], axis_vals [ - 1 ]
277237
278- # need x offset too for this
279- origin = ( position_x + self . offset [ 0 ], limits [ 0 ] - offset )
238+ # width or height of selector
239+ size = int ( np . ptp ( magn_vals ) * 1.5 + padding )
280240
281- # initial bounds are 20% of the limits range
282- bounds_init = ( limits [ 0 ], int ( np .ptp ( limits ) * 0.2 ) + offset )
241+ # center of selector along the other axis
242+ center = np .nanmean ( magn_vals )
283243
284- return bounds_init , limits , size , origin , axis
244+ return bounds_init , limits , size , center
0 commit comments