'Get a draggable/choose cursor for matplotlib RangeSlider
So i'm trying to build an interactive UI to handle image analysis and i need to define two boundaries. The most logical solution would be to use matplotlib's RangeSlider. However I have found it to be inpractical because you cannot decide which range you will move when clicking. Sadly they did not implement something such as (for ex.) right click to change the highest range. I can also not get a draggable cursor, though in the exemples of matplotlib it seemed to be possibe:
Here you can see white circles at each end of the slider boundaries, allowing one to specify which boundary one wants to move. However when i try this code this is what i get :
No draggable cursor is present anymore. Is there any way to get a draggable cursor back, and to decide which bound will be moving each time ? (setting draggable true doesn't achieve this). I'm using matplotlib '3.4.3', with Anaconda and backend 'Qt5Agg' Many thanks
Solution 1:[1]
Ok so i did not find how to make the draggable white circle appear. However if found a way to be able to decide which bound will be moving (by default in matplotlib it is the bound closer to your click). This just adds behaviours for other mouse clicks.
So one creates the RangeSlider
# Make a horizontal slider to control the bounds. axbounds = plt.axes([0.25, 0.145, 0.65, 0.03], facecolor='lightgoldenrodyellow') my_sliderbounds = RangeSlider( ax=axbounds, label='Bounds', dragging=True, valmin=0, valmax=100, valinit=(0,100), valstep=1 ) my_sliderbounds.on_changed(update) #calls the update function when value changes
Then you connect your canvas to a click detection (or any button really)
cid = plt.gcf().canvas.mpl_connect('button_press_event', onclick_img)
When the button is clicked, it calls the onclick_img function. We check that we are on the slider axe and what was clicked. left click is already handled by matplotlib. I will use right click to set the right boundary position, middle click to set the left boundary position.
def onclick_img(event): #we detect clicks on the canvas.
if event.inaxes==axbounds:
if event.button == 1: #MouseButton.LEFT, already
pass
if event.button == 2: #MouseButton.MIDDLE : left bound
clicked_x = int(event.xdata)
my_sliderbounds.set_val( ( clicked_x , max ( my_sliderbounds.val) ) )
if event.button == 3: #MouseButton.RIGHT : right
clicked_x = int(event.xdata)
my_sliderbounds.set_val( [ min( my_sliderbounds.val) , clicked_x ] )
As Mr.T pointed out the draggable behaviour seems to be same, even without the white circle appearance. But i got some strange behaviour when one bound would be dragged over the other. This i don't know how to solve.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Adrien Mau |


