Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to distinguishing right and left button in SpanSelector?

I want to use SpanSelector to select two intervals in a plot. To save the different extrema of the intervals, I would like to use a flag depending on if I selected the interval using the right or the left button (so I can distinguish the two wanted intervals).

Is the above possible?


Edited:

To be more specific: I would like that once displayed the plot, the SpanSelector draws a red span of area if done by the left button and a blue span of area if done by the right button.


Example:

The code below lets the user interactively select an interval and then prints such interval

import numpy as np
import matplotlib.pyplot as plt
import matplotlib.widgets as mwidgets  

fig = plt.figure() 
ax = plt.axes() 
x = np.arange(0,2*np.pi)  
y = np.sin(x) 
ax.plot(x,y) 

def onselect(vmin, vmax):
    print(vmin, vmax)

span = mwidgets.SpanSelector(ax, onselect, 'horizontal') 

plt.show()                                                               

I would like to modify the code above such that if the interval is drawn by the left button, then it prints "LEFT: vimin, vmax" and, that if the interval is drawn by the right button, then it prints "RIGHT: vmin, vmax".

Is the above possible?

like image 402
Stefano Avatar asked Sep 03 '25 07:09

Stefano


2 Answers

SpanSelector(..., button=1)

would be the span selector for the left mousebutton and

SpanSelector(..., button=3)

would be the span selector for the right mousebutton.

like image 163
ImportanceOfBeingErnest Avatar answered Sep 04 '25 20:09

ImportanceOfBeingErnest


I solved it using mpl_connect. I did not make the distinction between left and right click but I made the code treat the SpanSelector input in a different way depending on if the mouse-selected span was preceded by an enter keystroke or a shift+enter keystroke. I leave the code below in case it is useful to someone else.

import numpy as np
import matplotlib.pyplot as plt
import matplotlib.widgets as mwidgets  

fig = plt.figure() 
ax = plt.axes() 
x = np.arange(0,2*np.pi)  
y = np.sin(x) 
ax.plot(x,y) 
ax.set_title('[Press \'enter\' and \'shift+enter\' to select the intervals]')

def onselect(vmin, vmax):
    if plot_key_input == 'enter':
        print('Interval type 1:', vmin, vmax)
    if plot_key_input == 'enter+shift':
        print('Interval type 2:', vmin, vmax)

# The variable plot_key_input will store the key that is pressed during the plot visualization
plot_key_input = None
# Get the key pressed during the plot visualization
def onPressKey(event):
# Defined as a global variable so it will affect other programs and functions
    global plot_key_input
    plot_key_input = event.key

# Connect the keys to the function onPressKey during the plot visualization
cid = fig.canvas.mpl_connect('key_press_event', onPressKey)

span = mwidgets.SpanSelector(ax, onselect, 'horizontal') 

plt.show() 

# Disconnect the keys to the function onPressKey
fig.canvas.mpl_disconnect(cid)
like image 34
Stefano Avatar answered Sep 04 '25 21:09

Stefano