Bottom Page

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
 How do i get matplotlib event handling to work?
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


fig = plt.figure()
ax = fig.add_subplot(111)
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))
    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):
        if abs(float(y[i])) >= errn and abs(float(y[i])) <= errp:
            # 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])
    return coords

cid = fig.canvas.mpl_connect('button_press_event', onclick)
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????
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.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)
    return True

fig.canvas.mpl_connect('pick_event', onpick)
The last code posted worked for me
The first code posted the onclick event works but give the following error
Traceback (most recent call last): File "C:\Users\XXXXXXX\Anaconda3\lib\site-packages\matplotlib\cbook\", line 215, in process func(*args, **kwargs) File "c:/Users/XXXXXXX/Documents/", line 30, in onclick if abs(float(y[i])) >= errn and abs(float(y[i])) <= errp: IndexError: list index out of range
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:(

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  Handling multiple errors when using datafiles in Pandas alphanov 1 84 Jul-16-2019, 03:17 AM
Last Post: scidam
  HX711&matplotlib problems olego 0 49 Jul-12-2019, 12:22 PM
Last Post: olego
  problem using custom exception handling in python srm 3 130 Jul-03-2019, 09:10 PM
Last Post: ichabod801
  Matplotlib problem ift38375 0 81 Jul-02-2019, 02:55 PM
Last Post: ift38375
  Matplotlib contour no data coordinates b4rtt 1 168 Jun-18-2019, 09:36 AM
Last Post: b4rtt
  Matplotlib ImportError robertinoc 4 273 Jun-14-2019, 09:11 AM
Last Post: snippsat
  an easy way to disable exception handling Skaperen 6 383 Jun-02-2019, 10:38 PM
Last Post: Gribouillis
  Matplotlib animation problem Potatoez 1 306 May-21-2019, 03:58 PM
Last Post: Potatoez
  MatplotLib Sliders punksnotdead 4 260 May-18-2019, 08:45 PM
Last Post: punksnotdead
  Animation using matplotlib query JohnDoe 0 126 May-09-2019, 08:07 PM
Last Post: JohnDoe

Forum Jump:

Users browsing this thread: 1 Guest(s)