# 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()
# 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()
# put handles on the curve
import numpy as np
import matplotlib.pyplot as plt
from pdb import set_trace
x = np.linspace(0, np.pi, 200)
y = np.sin(x)
ymax = np.max(y)
yind = np.argmax(y)
fix, ax = plt.subplots()
#ax.plot(x, y, 'o',ls='-', markevery=5)
ax.plot(x, y, '-gD', markevery=[50, yind, 150])
plt.show()
# 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()