Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
Obtain cartesian coordinates by clicking with the mouse on any point on a graph and produce trading actions

Hi everyone.
First of all, I am so sorry by my bad English.

I would be very grateful if you could help me with the following problem:
I am programming, in python, a GUI in which the user is allowed to click, with the mouse, any point on a graph. That event will produce certain information related to the point that was clicked, to indicate which trading actions should be taken.

The problem is that to produce the information of each point, it is necessary to convert the coordinates in pixels of the point to cartesian coordinates, that is, obtain the original x and y coordinates. Where x is an integer from xmin = 0 to xmax. While y is a real number, it ranges from ymin to ymax.

I get some values, but I can't get more or less precise values ​​with the following instructions, which I have within the function that handles the mouse click event:

    abs_coord_x = root.winfo_pointerx() - root.winfo_rootx()  
    abs_coord_y = root.winfo_pointery() - root.winfo_rooty()  

    width =root.winfo_width()  
    height = root.winfo_height()  
    xreal=(abs_coord_x*(xmax - xmin))/ width + xmin  
   yreal=((alto - abs_coord_y)*(ymax - ymin))/ height + ymin

Obviously, there are problems identifying the margins of the windows where I insert the graphic, but I don't know how to get those margins. The graphic is inserted into a window created with tkinter. With the following instructions it is observed that these coordinates can be obtained, but I do not see the way to use them in my program:

toolbar = NavigationToolbar2Tk(canvas, root)  
toolbar.update()  
canvas.get_tk_widget().pack(side=tkinter.TOP, fill=tkinter.BOTH, expand=1)

Hopefully someone can help me with this.
A greeting.
Luciano

1 response

Hi everyone,
After reviewing here and there, I realized that there are some functions to convert the coordinates of pixels to Cartesian and vice versa.

transData.transform ()
transData.inverted ()

I get the Cartesian X coordinate correctly, but not the Y coordinate.
If someone can tell me how to correctly get the y coordinate, I would really appreciate it.

a greeting
José L.