Matplotlib – python – add Gaussian at the click

# add Gaussian at the click
# multi_gauss.py @ https://github.com/jithesh82

import matplotlib.pyplot as plt
import numpy as np
import pdb
import sys
sys.path.append('/home/jk/jk/python/xpswork')
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)
    pts.append(pt)
    pt1.figure.canvas.draw()

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')
ax.set_xlabel('x')
ax.set_ylabel('y')
plt.show()

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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