Bottom Page

Thread Rating:
  • 1 Vote(s) - 2 Average
  • 1
  • 2
  • 3
  • 4
  • 5
 Pick a line in a plot with matplotlib
#1
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:

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?
Quote
#2
You could perhaps try an integer pickradius such as 5
Quote
#3
Thanks for the answer Gribouillis.

I tried that, but it still picks the line wherever I click.
Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  Learning indexing with python, pick dates dervast 1 219 Jul-11-2019, 07:29 AM
Last Post: scidam
  MatPlotLib 2d plot of current density? ruben 0 244 May-13-2019, 06:47 AM
Last Post: ruben
  Plot Line chart based on the input parthi1705 3 394 May-09-2019, 05:01 AM
Last Post: scidam
  Bar Plot with Python ,matplotlib and sqlite3 tables gauravbhardwajee 0 958 Sep-25-2018, 06:17 PM
Last Post: gauravbhardwajee
  Draw a square-aspect log-linear plot via matplotlib Antonio 4 2,694 Jun-19-2018, 05:57 PM
Last Post: Antonio
  Remove internal lines donut plot matplotlib diego_last 5 2,346 Dec-14-2017, 04:05 PM
Last Post: diego_last
  How to pick random entries of stock prices? Alberto 1 1,240 Jul-16-2017, 09:51 PM
Last Post: wavic

Forum Jump:


Users browsing this thread: 1 Guest(s)