Skip to content

Instantly share code, notes, and snippets.

@yrevar
Last active August 29, 2015 14:23
Show Gist options
  • Save yrevar/c8f6199f973000f9439b to your computer and use it in GitHub Desktop.
Save yrevar/c8f6199f973000f9439b to your computer and use it in GitHub Desktop.
import numpy
import pylab
from pylab import *
from matplotlib.widgets import Slider, Button, RadioButtons
# default 2d transformation matrix values
x1_def, x2_def, y1_def, y2_def = 1,0,0,1
ax = subplot(111)
subplots_adjust(bottom=0.25)
# Stretch X and Y
A = numpy.matrix([[x1_def,x2_def],[y1_def,y2_def]])
x = numpy.matrix([[1, 2, 3, 4, 1, 1, 1, 4, 4, 4, 2, 2, 2, 3, 3, 3],[1, 2, 3, 4, 2, 3, 4, 1, 2, 3, 1, 3, 4, 1, 2, 4]])
y = A*x
l1, = pylab.plot(x[0].tolist()[0], x[1].tolist()[0], "*");
l2, = pylab.plot(y[0].tolist()[0], y[1].tolist()[0], "o");
pylab.ylim(-30,30)
pylab.xlim(-30,30)
slider_axis_bg_color = 'lightgoldenrodyellow'
axes_x1 = axes([0.15, 0.16, 0.65, 0.03], axisbg=slider_axis_bg_color)
x1valslider = Slider(axes_x1, 'x1', -15.0, 20.0, valinit=x1_def)
axes_x2 = axes([0.15, 0.11, 0.65, 0.03], axisbg=slider_axis_bg_color)
x2valslider = Slider(axes_x2, 'x2', -15.0, 20.0, valinit=x2_def)
axes_y1 = axes([0.15, 0.06, 0.65, 0.03], axisbg=slider_axis_bg_color)
y1valslider = Slider(axes_y1, 'y1', -15.0, 20.0, valinit=y1_def)
axes_y2 = axes([0.15, 0.01, 0.65, 0.03], axisbg=slider_axis_bg_color)
y2valslider = Slider(axes_y2, 'y2', -15.0, 20.0, valinit=y2_def)
resetax = axes([0.90, 0.01, 0.1, 0.04])
button = Button(resetax, 'Reset', color=slider_axis_bg_color, hovercolor='0.975')
pylab.show()
def reset(event):
A = numpy.matrix([[x1_def,x2_def],[y1_def,y2_def]])
y = A*x
l2.set_xdata(y[0].tolist()[0])
l2.set_ydata(y[1].tolist()[0])
x1valslider.set_val(A[0,0])
x2valslider.set_val(A[0,1])
y1valslider.set_val(A[1,0])
y2valslider.set_val(A[1,1])
draw()
button.on_clicked(reset)
def update(val):
A[1,0] = y1valslider.val
A[1,1] = y2valslider.val
A[0,0] = x1valslider.val
A[0,1] = x2valslider.val
y = A*x
l2.set_xdata(y[0].tolist()[0])
l2.set_ydata(y[1].tolist()[0])
draw()
y1valslider.on_changed(update)
y2valslider.on_changed(update)
x1valslider.on_changed(update)
x2valslider.on_changed(update)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment