Point picker event_handler drawing line and displaying coordinates in matplotlib

horcle_buzz

I have the following class that draws a vertical line through the y-axis, so that when I click on it a horizontal line gets drawn at the location. My goal is to get the y-coordinate to actually print right at the y-axis where the horizontal line gets drawn. For testing, I am trying to print a title with the y-ccordinate, but it is not working as expected.

What I am trying to really accomplish is to make the y-axis on a bar plot clickable so that the user can select a point on the y-axis, and then a bunch of "stuff" happens (including the drawing of a horizontal line). I really see no other way to make this happen, other than drawing a plottable vertical line through the y-axis to make it pickable. This seems rather kludgey, but I have not had any success with any other methods.

import matplotlib.pyplot as plt

class PointPicker(object):
    def __init__(self):

        self.fig = plt.figure()
        self.ax = self.fig.add_subplot(111)
        self.lines2d, = self.ax.plot((0, 0), (-6000, 10000), 'k-', linestyle='-',picker=5)

        self.fig.canvas.mpl_connect('pick_event', self.onpick)
        self.fig.canvas.mpl_connect('key_press_event', self.onpress)
        fig.canvas.mpl_connect('button_press_event', self.onclick)

    def onpress(self, event):
        """define some key press events"""
        if event.key.lower() == 'q':
            sys.exit()

    def onpick(self,event):
        x = event.mouseevent.xdata
        y = event.mouseevent.ydata
        L =  self.ax.axhline(y=y)
        print(y)
        ax.axvspan(0, 0, facecolor='y', alpha=0.5, picker=10)
        self.fig.canvas.draw()

    def onclick(event):
        self.fig.canvas.set_title('Selected item came from {}'.format(event.ydata))
        print(event.xdata, event.ydata)

if __name__ == '__main__':

    plt.ion()
    p = PointPicker()
    plt.show()

Assuming there is no ther way to achieve my end result, all is well with this method, except I cannot for the life of me get the title to print (using the self.fig.canvas.set_title('Selected item came from {}'.format(event.ydata)).

ImportanceOfBeingErnest

You can use the 'button_press_event' to connect to a method, which verifies that the click has happened close enough to the yaxis spine and then draws a horizontal line using the clicked coordinate.

import matplotlib.pyplot as plt

class PointPicker(object):
    def __init__(self, ax, clicklim=0.05):
        self.fig=ax.figure
        self.ax = ax
        self.clicklim = clicklim
        self.horizontal_line = ax.axhline(y=.5, color='y', alpha=0.5)
        self.text = ax.text(0,0.5, "")
        print self.horizontal_line
        self.fig.canvas.mpl_connect('button_press_event', self.onclick)


    def onclick(self, event):
        if event.inaxes == self.ax:
            x = event.xdata
            y = event.ydata
            xlim0, xlim1 = ax.get_xlim()
            if x <= xlim0+(xlim1-xlim0)*self.clicklim:
                self.horizontal_line.set_ydata(y)
                self.text.set_text(str(y))
                self.text.set_position((xlim0, y))
                self.fig.canvas.draw()


if __name__ == '__main__':

    fig = plt.figure()
    ax = fig.add_subplot(111)
    ax.bar([0,2,3,5],[4,5,1,3], color="#dddddd")
    p = PointPicker(ax)
    plt.show()

enter image description here

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Matplotlib event_handling line picker

From Dev

Matplotlib event_handling line picker

From Dev

Finding coordinates of a point on a line

From Dev

Drawing a Line - Maximum Point

From Dev

SWT - drawing with floating point coordinates and subpixel precision

From Dev

Draggable point in matplotlib - How to retrieve point coordinates

From Dev

get coordinates of arrow of a line point

From Dev

Drawing a line at a specific point in JFrame

From Dev

Animate a point alongside a line from known coordinates

From Dev

How to get coordinates of point where drawing arc ended in canvas?

From Dev

Move point of origin while drawing using polar coordinates in visual basic

From Dev

Does matplotlib have a function for drawing diagonal lines in axis coordinates?

From Dev

Drawing nodes with coordinates in correct position using NetworkX/Matplotlib

From Dev

drawing a line on a 3D plot in matplotlib

From Dev

Drawing a colorbar aside a line plot, using Matplotlib

From Dev

drawing a line on a 3D plot in matplotlib

From Dev

Python: displaying a line of text outside a matplotlib chart

From Dev

code for drawing a line from start point to goal point not working

From Dev

How to Drawing a curve line(peaks) using list of x,y coordinates

From Dev

Drawing curve line peaks using list of x,y coordinates points

From Dev

Unix shell not displaying UTF8 line drawing symbols

From Dev

Getting point coordinates from `matplotlib.pyplot.plot`

From Dev

Insert a dot at a certain point on a line with matplotlib

From Dev

Matplotlib transparent point over transparent line

From Dev

Finding the coordinates on the image knowing the center point and slope of a line

From Dev

Does canvas support floating point numbers when drawing line or arc?

From Dev

Drawing a line when given is center point, angle and length

From Dev

drawing straight line which passes through a point in between

From Dev

Store mouse click event coordinates with matplotlib

Related Related

  1. 1

    Matplotlib event_handling line picker

  2. 2

    Matplotlib event_handling line picker

  3. 3

    Finding coordinates of a point on a line

  4. 4

    Drawing a Line - Maximum Point

  5. 5

    SWT - drawing with floating point coordinates and subpixel precision

  6. 6

    Draggable point in matplotlib - How to retrieve point coordinates

  7. 7

    get coordinates of arrow of a line point

  8. 8

    Drawing a line at a specific point in JFrame

  9. 9

    Animate a point alongside a line from known coordinates

  10. 10

    How to get coordinates of point where drawing arc ended in canvas?

  11. 11

    Move point of origin while drawing using polar coordinates in visual basic

  12. 12

    Does matplotlib have a function for drawing diagonal lines in axis coordinates?

  13. 13

    Drawing nodes with coordinates in correct position using NetworkX/Matplotlib

  14. 14

    drawing a line on a 3D plot in matplotlib

  15. 15

    Drawing a colorbar aside a line plot, using Matplotlib

  16. 16

    drawing a line on a 3D plot in matplotlib

  17. 17

    Python: displaying a line of text outside a matplotlib chart

  18. 18

    code for drawing a line from start point to goal point not working

  19. 19

    How to Drawing a curve line(peaks) using list of x,y coordinates

  20. 20

    Drawing curve line peaks using list of x,y coordinates points

  21. 21

    Unix shell not displaying UTF8 line drawing symbols

  22. 22

    Getting point coordinates from `matplotlib.pyplot.plot`

  23. 23

    Insert a dot at a certain point on a line with matplotlib

  24. 24

    Matplotlib transparent point over transparent line

  25. 25

    Finding the coordinates on the image knowing the center point and slope of a line

  26. 26

    Does canvas support floating point numbers when drawing line or arc?

  27. 27

    Drawing a line when given is center point, angle and length

  28. 28

    drawing straight line which passes through a point in between

  29. 29

    Store mouse click event coordinates with matplotlib

HotTag

Archive