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
#2
I am using the user guide for event_handling and nothing works. For example, the code below (borrowed from the document) just produces the figure and when i click on points nothing happens. Bug????
https://matplotlib.org/users/event_handling.html
"""
compute the mean and stddev of 100 data sets and plot mean vs stddev.
When you click on one of the mu, sigma points, plot the raw data from
the dataset that generated the mean and stddev
"""
import numpy as np
import matplotlib.pyplot as plt

X = np.random.rand(100, 1000)
xs = np.mean(X, axis=1)
ys = np.std(X, axis=1)

fig = plt.figure()
ax = fig.add_subplot(111)
ax.set_title('click on point to plot time series')
line, = ax.plot(xs, ys, 'o', picker=5)  # 5 points tolerance


def onpick(event):

    if event.artist!=line: return True

    N = len(event.ind)
    if not N: return True


    figi = plt.figure()
    for subplotnum, dataind in enumerate(event.ind):
        ax = figi.add_subplot(N,1,subplotnum+1)
        ax.plot(X[dataind])
        ax.text(0.05, 0.9, 'mu=%1.3f\nsigma=%1.3f'%(xs[dataind], ys[dataind]),
                transform=ax.transAxes, va='top')
        ax.set_ylim(-0.5, 1.5)
    figi.show()
    return True

fig.canvas.mpl_connect('pick_event', onpick)

plt.show()
Reply
#3
The last code posted worked for me
The first code posted the onclick event works but give the following error
Error:
Traceback (most recent call last): File "C:\Users\XXXXXXX\Anaconda3\lib\site-packages\matplotlib\cbook\__init__.py", line 215, in process func(*args, **kwargs) File "c:/Users/XXXXXXX/Documents/forumpost.py", line 30, in onclick if abs(float(y[i])) >= errn and abs(float(y[i])) <= errp: IndexError: list index out of range
Reply
#4
Strange. The last code has not worked for me. I am using anaconda. It does plot the figure but when I click on points it does not do anything:(
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Matplotlib: How do I convert Dates from Excel to use in Matplotlib JaneTan 1 1,024 Mar-11-2021, 10:52 AM
Last Post: buran
  Matplotlib Hovering Event displaying (x, y) data RawlinsCross 0 3,741 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