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()

Matplotlib – python – click and add Gaussian peaks

# click and add Gaussian peaks

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)
    for pt in pts:
        x = pt.get_xdata()
        y = pt.get_ydata()
        ax.plot(x, y)
    pt, = ax.plot(x, y + 5)
    pts.append(pt)
    pt1.figure.canvas.draw()

fig, ax = plt.subplots()
pt1, = ax.plot(x, y)
pt2, = ax.plot(x, y + 2)
pts = [pt1, pt2]
pt1.figure.canvas.mpl_connect('button_press_event', onPress)
plt.cla()
plt.show()

Matplotlib – python – cla()

# draw - erase - replot

import matplotlib.pyplot as plt
import numpy as np
import pdb
plt.ion()

x = np.linspace(0, 2*3.14, 100)
y = np.sin(x)
pts = []

fig, ax = plt.subplots()
pt1, = ax.plot(x, y)
pt2, = ax.plot(x, y + 2)
pts = [pt1, pt2]
#pnt.figure.canvas.mpl_connect('button_press_event', onPress)
#pnt.figure.canvas.mpl_connect('motion_notify_event', onPress)
input("Tap Enter: ")
plt.cla()
input("Tap Enter: ")
x = pts[0].get_xdata()
y = pts[0].get_ydata()
ax.plot(x, y)
x = pts[1].get_xdata()
y = pts[1].get_ydata()
ax.plot(x, y)
input('Tap Enter: ')
plt.show()

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
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)

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)
    pnt.figure.canvas.draw()

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

plt.show()

Matplotlib – python – click-drag to change gaussian peak width

# draw one point using subplot
# click and drag to increase/decrease width of -
# a Gaussian curve
# multi_gauss.py  https://github.com/jithesh82

import matplotlib.pyplot as plt
from pdb import set_trace
import numpy as np
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)

press = False

mouse = None
def onPress(event):
    global press
    global mouse
    contains, _ = pnt.contains(event)
    # make sure the click is on the object
    if contains:
        press = True
        mouse = event.xdata
    print(press)


def onMove(event):
    global press
    global guess
    global mouse
    if press == True:
        # only for events inside the axes
        if event.inaxes == pnt.axes:
            print(event.xdata, event.ydata)
            x = list(pnt.get_xdata())
            if mouse < event.xdata:
                guess[2] += 0.01
            else: 
                guess[2] -= 0.01
            y = Gauss(x, *[guess[0], guess[1],\
                    guess[2]])
            pnt.set_data(x, y)
            #pnt.set_data([event.xdata, event.ydata])
            pnt.figure.canvas.draw()

def onRelease(event):
    global press
    global mouse
    press = False
    mouse = None
    print(press)
    pnt.figure.canvas.draw()

fig, ax = plt.subplots()
pnt, = ax.plot(x, y, label='click-drag - increase length')
pnt.set_linewidth(3)
ax.set_title('click-drag - increase length')
ax.set_xlabel('x')
ax.set_ylabel('y')
ax.plot(x, Gauss(x, *[guess[0] + 50, guess[1], guess[2]]))
pnt.figure.canvas.mpl_connect('button_press_event', onPress)
pnt.figure.canvas.mpl_connect('motion_notify_event', onMove)
pnt.figure.canvas.mpl_connect('button_release_event', onRelease)

plt.show()

Matplotlib – python – click-drag – change Gaussian peak height

# draw one point using subplot
# click and drag to increase/decrease height of -
# a Gaussian curve

import matplotlib.pyplot as plt
from pdb import set_trace
import numpy as np
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)

press = False

def onPress(event):
    global press
    contains, _ = pnt.contains(event)
    # make sure the click is on the object
    if contains:
        press = True
    print(press)

def onMove(event):
    global press
    if press == True:
        # only for events inside the axes
        if event.inaxes == pnt.axes:
            print(event.xdata, event.ydata)
            x = list(pnt.get_xdata())
            y = Gauss(x, *[event.ydata, guess[1],\
                    guess[2]])
            pnt.set_data(x, y)
            #pnt.set_data([event.xdata, event.ydata])
            pnt.figure.canvas.draw()

def onRelease(event):
    global press
    press = False
    print(press)
    pnt.figure.canvas.draw()

fig, ax = plt.subplots()
pnt, = ax.plot(x, y, label='click-drag - increase length')
pnt.set_linewidth(3)
ax.set_title('click-drag - increase length')
ax.set_xlabel('x')
ax.set_ylabel('y')
ax.plot(x, Gauss(x, *[guess[0] + 50, guess[1], guess[2]]))
pnt.figure.canvas.mpl_connect('button_press_event', onPress)
pnt.figure.canvas.mpl_connect('motion_notify_event', onMove)
pnt.figure.canvas.mpl_connect('button_release_event', onRelease)

plt.show()

Matplotlib – python – click and drag a Gaussian peak around

# move a guassian curve around
# source multi_guass 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)
y = np.sin(x)
guess = [100, 3, 0.5]
y = Gauss(x, *guess)

press = False

def onPress(event):
    global press
    contains, _ = pnt.contains(event)
    # make sure the click is on the object
    if contains:
        press = True
    print(press)

def onMove(event):
    global press
    if press == True:
        # only for events inside the axes
        if event.inaxes == pnt.axes:
            x = pnt.get_xdata()
            y = pnt.get_ydata()
            ymax = np.max(y)
            yind = np.argmax(y)
            xmax = x[yind]
            y = y + event.ydata - ymax
            x = x + event.xdata - xmax
            pnt.set_data(x, y)
            print(event.xdata, event.ydata)
            pnt.figure.canvas.draw()

def onRelease(event):
    global press
    press = False
    print(press)
    pnt.figure.canvas.draw()

fig, ax = plt.subplots()
pnt, = ax.plot(x, y, 'ro')
pnt.figure.canvas.mpl_connect('button_press_event', onPress)
pnt.figure.canvas.mpl_connect('motion_notify_event', onMove)
pnt.figure.canvas.mpl_connect('button_release_event', onRelease)

plt.show()

Matplotlib – python – click-drag and increase/decrease line length

# draw one point using subplot
# click and drag to increase/decrease length of a line

import matplotlib.pyplot as plt
from pdb import set_trace

press = False

def onPress(event):
    global press
    contains, _ = pnt.contains(event)
    # make sure the click is on the object
    if contains:
        press = True
    print(press)

def onMove(event):
    global press
    if press == True:
        # only for events inside the axes
        if event.inaxes == pnt.axes:
            print(event.xdata, event.ydata)
            x = list(pnt.get_xdata())
            y = list(pnt.get_ydata())
            y[1] = event.ydata
            pnt.set_data(x, y)
            #pnt.set_data([event.xdata, event.ydata])
            pnt.figure.canvas.draw()

def onRelease(event):
    global press
    press = False
    print(press)
    pnt.figure.canvas.draw()

fig, ax = plt.subplots()
pnt, = ax.plot([2, 2],[5, 7], label='click-drag - increase length')
pnt.set_linewidth(6)
#set_trace()
ax.set_title('click-drag - increase length')
ax.set_xlabel('x')
ax.set_ylabel('y')
ax.plot([1, 1], [5, 20])
pnt.figure.canvas.mpl_connect('button_press_event', onPress)
pnt.figure.canvas.mpl_connect('motion_notify_event', onMove)
pnt.figure.canvas.mpl_connect('button_release_event', onRelease)

plt.show()

Matplotlib – python – click on two curves and change their colors independently

# click on the 2 curves and change their colors -
# independently

import matplotlib.pyplot as plt
import random
import numpy as np
import pdb

x = np.linspace(0, 2*3.14, 100)
y = np.sin(x)

class changeColor:
    def __init__(self, pnt):
        self.pnt = pnt
    def connect(self):
        self.pnt.figure.canvas.mpl_connect('button_press_event', \
                self.onPress)
    def onPress(self, event):
        if event.inaxes == self.pnt.axes:
            contains, _ = self.pnt.contains(event)
            if contains:
                #print(True)
                print(event.xdata, event.ydata)
                x = list(self.pnt.get_xdata())
                y = list(self.pnt.get_ydata())
                self.pnt.set_data(x, y)
                #print(dir(pnt))
                r = random.random()
                g = random.random()
                b = random.random()
                color = (r, g, b)
                self.pnt.set_markerfacecolor(color)
                self.pnt.set_markeredgecolor(color)
                self.pnt.figure.canvas.draw()

fig, ax = plt.subplots()
pnt1, = ax.plot(x, y, 'ro')
pnt2, = ax.plot(x + 1, y, 'ro')

p = []
for pnt in [pnt1, pnt2]:
    I = changeColor(pnt)
    I.connect()
    p.append(I)

plt.show()