Matplotlib – python – move gaussian curve to click

# move gaussian to the clicked point

import matplotlib.pyplot as plt
from pdb import set_trace
import numpy as np
import sys
from multi_gauss import multiGauss as Gauss

x = np.linspace(0, 2*3.14, 100)
guess = [100, 3, 0.5]
y = Gauss(x, *guess)

def onPress(event):
    global guess
    print(event.xdata, event.ydata)
    guess = [event.ydata, event.xdata, guess[2]]
    y = Gauss(x, *guess)
    pnt.set_data(x, y)

fig, ax = plt.subplots()
pnt, = ax.plot(x, y)
pnt.figure.canvas.mpl_connect('button_press_event', onPress)
ax.plot(x, Gauss(x, *[guess[0] + 50, guess[1], guess[2]]))
ax.set_title('Mouse Events')

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s