Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set the min and max value for SpanSelector widget programmatically

I want to update subplots based on a SpanSelector which works as expected. But another requirement is that I need to initialize the selected span to some given values. I have tried to call the _press, _release functions but they just return False and nothing happens. How can I set the span programmatically such that the selected span is visible in the SpanSelector as well?

from matplotlib.widgets import SpanSelector
from collections import namedtuple

widgets.append(
    SpanSelector(range_ax, onselect=self._plot_subplots, direction='horizontal', rectprops=dict(alpha=0.5, facecolor='red'), span_stays=True)
)

# fake some events
Event = namedtuple('Event', ['xdata', 'ydata'])
widgets[0]._press(Event(0, 119))
widgets[0]._release(Event(500, 120))
like image 378
KIC Avatar asked Dec 06 '25 11:12

KIC


1 Answers

Set the selection rectangle stay_rect and call the onselect handler with the initial xmin and xmax values.

Example: initial view of the example from the docs with the following inserted before plt.show():

xmin, xmax = 3, 4 
span.stay_rect.set_bounds(xmin, 0, xmax-xmin, 1)
span.stay_rect.set_visible(True)
span.onselect(xmin, xmax)

enter image description here

like image 148
Stef Avatar answered Dec 09 '25 01:12

Stef