Sep-18-2018, 07:21 AM
Hi all.
I have an issue with a picking event using the matplotlib library. I am trying to pick a vertical line that I draw on a X-Y plot when I click near the line. However, wherever I click, the line is picked, and I do not understand why.
Here's my code:
Any idea?
I have an issue with a picking event using the matplotlib library. I am trying to pick a vertical line that I draw on a X-Y plot when I click near the line. However, wherever I click, the line is picked, and I do not understand why.
Here's my code:
import numpy as np
import matplotlib.pyplot as plt
class my_class:
def __init__( self , **kwargs ):
self.fig , self.ax = plt.subplots()
self.line = self.ax.axvline( x = 0.5 , **kwargs )
self.connect()
plt.show()
return
def connect( self ):
cid = self.fig.canvas.mpl_connect( 'button_press_event' , self.on_click )
return
def on_click( self , event ):
if self.line.contains( event ):
print( "Line picked." )
return
def main():
my_plot = my_class( pickradius = .5 )
if __name__ == "__main__":
main()The constructor creates the plot, the line, and connects to mouse events via the function "connect". The function "on_click" is a callback function to mouse clicking events that (should) print "Line picked." whenever I click near the line. But it prints "Line picked" wherever I click.Any idea?
