这是我已经工作了一段时间的程序,我已经在这里得到了一些很好的故障排除技巧。
它运行得很好,速度不是主要考虑因素,因为它只是想直观地看看不同的图形如何随着输入的变化而变化。但是,我怎样才能完全将图形显示为REDRAW,而不仅仅是线条(它将旧轴上的每个轴重新排列而不清除它,因此它会堆叠起来并迅速陷入困境)。
全部谢谢!
#!/usr/apps/Python/bin/python
import matplotlib, sys
matplotlib.use('TkAgg')
from numpy import arange, sin, pi
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg, NavigationToolbar2TkAgg
from matplotlib.figure import Figure
from Tkinter import *
import math
from matplotlib import pylab
import scipy
from scipy.stats import norm
master = Tk()
master.title("DePaul University Interactive Options Graphs")
pricePlot = Figure(figsize=(4,3), dpi=100, frameon=False)
a = pricePlot.add_subplot(111)
a.set_title('The Greeks')
priceDataPlot = FigureCanvasTkAgg(pricePlot, master=master)
priceDataPlot.get_tk_widget().grid(column=1, columnspan=2, row=1, rowspan=2)
vegaPlot = Figure(figsize=(4,3.5), dpi=75, frameon=False)
b = vegaPlot.add_subplot(1,1,1)
b.set_title('Vega')
vegaDataPlot = FigureCanvasTkAgg(vegaPlot, master=master)
vegaDataPlot.get_tk_widget().grid(row=2)
deltaPlot = Figure(figsize=(4,3.5), dpi=75, frameon=False)
c = deltaPlot.add_subplot(111)
c.set_title('Delta')
deltaDataPlot = FigureCanvasTkAgg(deltaPlot, master=master)
deltaDataPlot.get_tk_widget().grid(row=0,rowspan=2)
gammaPlot = Figure(figsize=(4,3.5), dpi=75, frameon=False)
d = gammaPlot.add_subplot(111)
d.set_title('Gamma')
gammaDataPlot = FigureCanvasTkAgg(gammaPlot, master=master)
gammaDataPlot.get_tk_widget().grid(column=3,row=2)
rhoPlot = Figure(figsize=(4,3.5), dpi=75, frameon=False)
e = rhoPlot.add_subplot(111)
e.set_title('Rho')
rhoDataPlot = FigureCanvasTkAgg(rhoPlot, master=master)
rhoDataPlot.get_tk_widget().grid(column=3,row=3)
thetaPlot = Figure(figsize=(4,3.5), dpi=75, frameon=False)
f = thetaPlot.add_subplot(111)
f.set_title('Theta')
thetaDataPlot = FigureCanvasTkAgg(thetaPlot, master=master)
thetaDataPlot.get_tk_widget().grid(column=3,row=0,rowspan=2)
a3Plot = Figure(figsize=(4,3.5), dpi=75, frameon=False)
g = a3Plot.add_subplot(111)
g.set_title('Price')
a3DataPlot = FigureCanvasTkAgg(a3Plot, master=master)
a3DataPlot.get_tk_widget().grid(row=3)
CallPutFlag='c'
def changePut():
CallPutFlag='p'
print CallPutFlag
def changeCall():
CallPutFlag='c'
print CallPutFlag
def main():
T=250 # This will
timeSpread = range(T-200,T+200,20) # all be user
# inputted
# values through
# Tkinter GUI
#
# JV 7/27/2012
r=float(interestRate.get())
S=float(stockPrice.get())
K=float(strikePrice.get())
v=float(volatility.get())
def BlackScholes(t):
d1 = (math.log(S/K)+(r+v*v/2.)*(float(t)/365))/(v*math.sqrt((float(t)/365)))
d2 = d1-v*math.sqrt((float(t)/365))
if CallPutFlag=='c':
return S*scipy.stats.norm.cdf(d1)-K*math.exp(-r*float(t))*scipy.stats.norm.cdf(d2)
else:
return K*math.exp(-r*float(t))*scipy.stats.norm.cdf(-d2)-S*scipy.stats.norm.cdf(-d1)
def delta(t):
d1 = (math.log(S/K)+(r+v*v/2.)*(float(t)/365))/(v*math.sqrt((float(t)/365)))
d2 = d1-v*math.sqrt((float(t)/365))
if CallPutFlag=='c':
callDelta = scipy.stats.norm.cdf(d1)
return callDelta
else:
putDelta = -scipy.stats.norm.cdf(-d1)
return putDelta
def gamma(t):
d1 = (math.log(S/K)+(r+v*v/2.)*(float(t)/365))/(v*math.sqrt((float(t)/365)))
d2 = d1-v*math.sqrt((float(t)/365))
gamma = scipy.stats.norm.pdf(d1)/(S*v*math.sqrt(float(t)))
return gamma
def rho(t):
d1 = (math.log(S/K)+(r+v*v/2.)*(float(t)/365))/(v*math.sqrt((float(t)/365)))
d2 = d1-v*math.sqrt((float(t)/365))
if CallPutFlag=='c':
callRho = (K*t*scipy.stats.norm.cdf(d2)/100)*math.e**-(r*t)
return callRho
else:
putRho = (-K*t*scipy.stats.norm.cdf(-d2)/100)*math.e**-(r*t)
return putRho
def vega(t):
d1 = (math.log(S/K)+(r+v*v/2.)*(float(t)/365))/(v*math.sqrt((float(t)/365)))
return S*scipy.stats.norm.pdf(d1)*math.sqrt(float(t))/100
def theta(t):
d1 = (math.log(S/K)+(r+v*v/2.)*(float(t)/365))/(v*math.sqrt((float(t)/365)))
d2 = d1-v*math.sqrt((float(t)/365))
b = math.e**-(r*t)
if CallPutFlag=='c':
callTheta = -S*scipy.stats.norm.pdf(d1)*v/(2*math.sqrt(float(t))-r*K*b*scipy.stats.norm.cdf(d2))
return callTheta
else:
putTheta = -S*scipy.stats.norm.pdf(d1)*v/(2*math.sqrt(float(t))+r*K*b*scipy.stats.norm.cdf(-d2))
return putTheta
tprices = [BlackScholes(i) for i in timeSpread]
tdeltas = [delta(i) for i in timeSpread]
tgammas = [gamma(i) for i in timeSpread]
trhos = [rho(i) for i in timeSpread]
tvegas = [vega(i) for i in timeSpread]
tthetas = [theta(i) for i in timeSpread]
a.cla()
a.plot(timeSpread,tdeltas,'g-')
a.plot(timeSpread,tgammas,'b-')
a.plot(timeSpread,trhos,'m-')
a.plot(timeSpread,tvegas,'r-')
a.plot(timeSpread,tthetas,'c-')
priceDataPlot.show()
line, = b.plot(timeSpread,tvegas,'r-')
vegaDataPlot.show()
line.remove()
line, = c.plot(timeSpread,tdeltas,'g-')
deltaDataPlot.show()
line.remove()
line, = d.plot(timeSpread,tgammas,'b-')
gammaDataPlot.show()
line.remove()
line, = e.plot(timeSpread,trhos,'m-')
rhoDataPlot.show()
line.remove()
line, = f.plot(timeSpread,tthetas,'c-')
thetaDataPlot.show()
line.remove()
line, = g.plot(timeSpread,tprices,'k-')
a3DataPlot.show()
line.remove()
##photo=PhotoImage(file='/Users/jaredvacanti/Desktop/Depaul.gif')
##Label(master, image=photo).grid()
w = Label(master, text="DePaul University Department of Finance \nInteractive Options Graphs")
w.grid(row=0,column=1,columnspan=2,pady=100)
r1 = Radiobutton(master, variable=CallPutFlag, value='c', text="Call",command=lambda root=master:main())
r2 = Radiobutton(master, variable=CallPutFlag, value='p', text="Put",command=lambda root=master:main())
r1.grid(row=0,column=1,columnspan=2,pady=50, sticky=N)
r2.grid(row=0,column=1,columnspan=1,pady=50, sticky=N)
interestRate = Scale(master, from_=0, to=5,orient=HORIZONTAL,label='Interest Rate', command=lambda root=master:main())
interestRate.set(1)
interestRate.grid(row=3,column=2)
stockPrice = Scale(master, from_=0, to=100,orient=HORIZONTAL,label='Stock Price', command=lambda root=master:main())
stockPrice.set(10.0)
stockPrice.grid(row=3,column=1,pady=10)
strikePrice = Scale(master, from_=0, to=100,orient=HORIZONTAL,label='Strike Price', command=lambda root=master:main())
strikePrice.set (11.0)
strikePrice.grid(row=3,rowspan=2,column=1,sticky=N)
volatility = Scale(master, from_=0, to=10,orient=HORIZONTAL,label='Volatility', command=lambda root=master:main())
volatility.set(1)
volatility.grid(row=3,rowspan=2,column=2,sticky=N)
def quit(master):
master.destroy()
Button(master, borderwidth=0, bg='gray', text="Quit", command=lambda root=master:quit(master)).grid(row=3,rowspan=2,column=2,sticky=S,pady=30)
Button(master, borderwidth=0, bg='gray', text="Calculate").grid(row=3,rowspan=2, column=1,columnspan=1,sticky=S,pady=30)
main()
master.mainloop()
答案 0 :(得分:1)
创建Figure对象时删除frameon=False
:
Figure(figsize=(4,3), dpi=100)
如果你想要图的背景颜色与Tk窗口相同,你可以通过facecolor
aurgument设置它:
Figure(figsize=(4,3), dpi=100, facecolor=TK_BACKGROUND_COLOR)
在我的系统上:
TK_BACKGROUND_COLOR = (212.0/256,208.0/256,200.0/256)