Bottom Page

Thread Rating:
  • 1 Vote(s) - 2 Average
  • 1
  • 2
  • 3
  • 4
  • 5
 Pick a line in a plot with matplotlib
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 , = plt.subplots()
        self.line = x = 0.5 , **kwargs )
    def connect( self ):
        cid = self.fig.canvas.mpl_connect( 'button_press_event' , self.on_click )
    def on_click( self , event ):
        if self.line.contains( event ):
            print( "Line picked." )

def main():
    my_plot = my_class( pickradius = .5 )

if __name__ == "__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?
You could perhaps try an integer pickradius such as 5
Thanks for the answer Gribouillis.

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

Top Page

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

Forum Jump:

Users browsing this thread: 1 Guest(s)