Matplotlib – python – add Gaussian at the click

# add Gaussian at the click
# @

import matplotlib.pyplot as plt
import numpy as np
import pdb
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)
y = np.array(y)
pts = []

def onPress(event):
    print(event.xdata, event.ydata)
    global guess
    for pt in pts:
        x = pt.get_xdata()
        y = pt.get_ydata()
        ax.plot(x, y)
    guess = [event.ydata, event.xdata, guess[2]]
    y = Gauss(x, *guess)
    pt, = ax.plot(x, y)

fig, ax = plt.subplots()
pt1, = ax.plot(x, y)
pts = [pt1]
pt1.figure.canvas.mpl_connect('button_press_event', onPress)
ax.set_title('Add Gaussian at the click')

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