Welcome, guest | Sign In | My Account | Store | Cart

Polynomial explorer. You tell this module what order of polynomial you want and it will set up a figure with a graph of that polynomial plotted with x = -1 to +1. It will set up a second figure with a set of coefficient axes. You can click on the coeffiecient axes to set the coefficents. The graph will then update to reflect the values of the new coefficients.

This code illustrates the use of mouse interaction using matplotlib

Python, 101 lines
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
"""Polynomial explorer. You tell this module what order of polynomial you want and it will set up a figure
with a graph of that polynomial plotted with x = -1 to +1. It will set up a second figure with a set of
coefficient axes. You can click on the coeffiecient axes to set the coefficents. The graph will then update 
to reflect the values of the new coefficients"""

import pylab as M

def f(x,c):
	"""Pass in x (a scalar or vector) and pass in c (a vector) and this function will compute 
f(x) = x**n + c(n-1)*x**(n-1) ... + c(1)*x + c(0)"""
	
	N = len(c)
	y = M.zeros(len(x))
	for n in xrange(N):
		y += c[n]*x**n
	y += x**N
		
	return y

def fstring(c):
	"""Pass the coefficients and return a string corresponding to the ploynomial"""
	
	order = len(c)
	fx = "$f(x) = x^%d" %(order)
	for n in xrange(order-1):
		nn = order - 1 - n 
		fx += " + %1.3f x^%d" %(c[nn],nn)
	fx += " + %1.3f$" %(c[0])
	
	return fx


class Explorer:
	
	def __init__(self, norder = 2):
		"""Initializes the class when returning an instance. Pass it the polynomial order. It will 
set up two figure windows, one for the graph the other for the coefficent interface. It will then initialize 
the coefficients to zero and plot the (not so interesting) polynomial."""
		
		self.order = norder
		
		self.c = M.zeros(self.order,'f')
		self.ax = [None]*(self.order-1)#M.zeros(self.order-1,'i') #Coefficent axes
		
		self.ffig = M.figure() #The first figure window has the plot
		self.replotf()
		
		self.cfig = M.figure() #The second figure window has the 
		row = M.ceil(M.sqrt(self.order-1))
		for n in xrange(self.order-1):
			self.ax[n] = M.subplot(row, row, n+1)
			M.setp(self.ax[n],'label', n)
			M.plot([0],[0],'.')
			M.axis([-1, 1, -1, 1]);
			
		self.replotc()
		M.connect('button_press_event', self.click_event)
	
	def replotf(self):
		"""This replots the function given the coefficient array c."""

		x = M.arange(-1,1.,.001)
		M.ioff()
		M.figure(self.ffig.number)
		M.cla()
		M.plot(x, f(x,self.c))				
		M.title(fstring(self.c))
		M.draw()
		
	def replotc(self):
		"""This replots the coefficients."""
	
		M.figure(self.cfig.number)
		M.ioff()
		for n in xrange(self.order-1):
			M.axes(self.ax[n])
			#M.cla()
			M.plot([self.c[n]], [self.c[n+1]],'ko')
			M.xlabel("$c_%d$" %(n))
			M.ylabel("$c_%d$" %(n+1))
			M.axis([-1, 1, -1, 1]);
			del self.ax[n].lines[0]

		M.draw()
		
	def click_event(self, event):
		"""Whenever a click occurs on the coefficent axes we modify the coefficents and update the 
plot"""
		
		if event.xdata is None:#we clicked outside the axis
			return

		idx = M.getp(event.inaxes,'label')
		
		print idx, event.xdata, event.ydata
				
		self.c[idx] = event.xdata
		self.c[idx+1] = event.ydata

		self.replotf()
		self.replotc()
Created by Kaushik Ghose on Thu, 11 Sep 2008 (MIT)
Python recipes (4591)
Kaushik Ghose's recipes (15)

Required Modules

Other Information and Tasks