Python Forum
How do i get matplotlib event handling to work?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How do i get matplotlib event handling to work?
#1
Dear all, it is my first time trying to do mping. It appears that code below does not do anything upon clicking on my graph (i.e. it does not go into onclick(event) funciton. Any suggestions? Eventually, upon clicking on my points I want to plot an image (I did comment these lines out in my code as right now I just want to make sure it does go into that funciton upon clicking on it). Thank yoU!
import matplotlib.image as mpimg
import numpy as np
import matplotlib.pyplot as plt

x=[1,2,3,4]
y=[1,4,9,16]

plt.close('all')
fig = plt.figure()
ax = fig.add_subplot(111)
ax.plot(x,y,'o')
coords = []

def onclick(event):
    print("hello there")
    global ix, iy
    ix, iy = event.xdata, event.ydata
    #print 'x = %f, y = %f'%(ix, iy)
    global coords
    coords.append((ix, iy))
    print(len(coords))
    z=len(coords)-1
    print(coords[z][1])
    per = (10*coords[z][1])/100
    errp = abs(coords[z][1]+per)
    errn = abs(coords[z][1]-per)
    print("errn=%f, errp=%f"%(errn, errp))
    for i in range(0,1000):
        print(i)
        if abs(float(y[i])) >= errn and abs(float(y[i])) <= errp:
            print(y[i])
            # Eventually, after clicking on the point I will plot the imshow as below
            #fig2 = plt.figure()
            #mymap = read_mydata('IMAGE'+str(i+1)+'.txt')
            #mymap = mymap.reshape(dims[0],dims[1])
            #plt.imshow(mymap)
            #fig2.show()
    return coords

cid = fig.canvas.mpl_connect('button_press_event', onclick)
plt.show()
Reply


Messages In This Thread
How do i get matplotlib event handling to work? - by jenya56 - Apr-24-2019, 02:05 PM
non of the examples are working - by jenya56 - Apr-24-2019, 05:31 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
Star python exception handling handling .... with traceback mg24 3 1,312 Nov-09-2022, 07:29 PM
Last Post: Gribouillis
  Matplotlib: How do I convert Dates from Excel to use in Matplotlib JaneTan 1 3,271 Mar-11-2021, 10:52 AM
Last Post: buran
  Matplotlib Hovering Event displaying (x, y) data RawlinsCross 0 6,247 Oct-18-2019, 06:13 PM
Last Post: RawlinsCross

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020