-2

这是我已经研究了一段时间的程序,我已经在这里获得了一些很好的故障排除技巧。

它运行得很好,速度不是主要问题,因为它只是应该直观地了解不同的图表如何随着不同的输入而变化。但是,我怎样才能让图表完全变成 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()
4

1 回答 1

1

frameon=False创建 Figure 对象时删除:

Figure(figsize=(4,3), dpi=100)

如果希望 Figure 的背景颜色与 Tk 窗口相同,可以通过facecolor参数设置:

Figure(figsize=(4,3), dpi=100, facecolor=TK_BACKGROUND_COLOR)

在我的系统上:

TK_BACKGROUND_COLOR = (212.0/256,208.0/256,200.0/256)
于 2012-09-07T06:30:47.617 回答