在python中使用matplotlib循环

在python中使用matplotlib循环,python,excel,matplotlib,plot,quantitative-finance,Python,Excel,Matplotlib,Plot,Quantitative Finance,我定义了一个函数(也许我会将其拆分并将所有内容放在一个类中),该函数从pdf文件中获取数据,修改它们,然后将它们打印到一个新的xlsx文件中。 问题是,每次脚本运行时,我都会在桌面上保存一个绘图。 我在列表中有四个变量,我想运行它。 但每次我运行它时(使用函数打印的for循环),图形都会获取以前的数据并将所有内容都放在绘图中,但我只希望绘图用于新数据。 所以我想要4个不同的excel文件(我做了)和4个不同的图(每个图只有一个变量数据系列)。 我试图将绘图的输入列表重置为空列表,但无效。 你将如

我定义了一个函数(也许我会将其拆分并将所有内容放在一个类中),该函数从pdf文件中获取数据,修改它们,然后将它们打印到一个新的xlsx文件中。
问题是,每次脚本运行时,我都会在桌面上保存一个绘图。
我在列表中有四个变量,我想运行它。 但每次我运行它时(使用函数打印的for循环),图形都会获取以前的数据并将所有内容都放在绘图中,但我只希望绘图用于新数据。
所以我想要4个不同的excel文件(我做了)和4个不同的图(每个图只有一个变量数据系列)。
我试图将绘图的输入列表重置为空列表,但无效。
你将如何解决这个问题?也许使用其他模块? 我附上的图片显示了我所做的混乱,因为它包含了以前的数据和新的数据,但我只想要新数据的三种颜色的时间序列

PS:我想在每次运行该函数时添加一个工作表,但我无法找到一种方法,所以每次都选择创建一个新的xlsx文件

import pandas as pd
import pandas_datareader as pdr
import datetime as dt
from datetime import timedelta
from tabula import read_pdf
from scipy.stats import norm
import numpy
import math
import matplotlib.pyplot as plt
import xlsxwriter

date=dt.datetime.now().date()
date_ok=str(date-timedelta(days=1))

#digits_list=[5,6,7,6]
#range_PDF_list=[range(0,28),range(1,30),range(0,21),range(1,26)]
#range_black_list=[range(0,28),range(0,29),range(0,21),range(0,25)]
#pages_list=[5,3,2,3]
#initial_list=[0,1,0,1]
strike_PDF={'eurostoxx':['^STOXX50E',5,range(0,28),range(0,28),5,0],'dax':['^GDAXI',6,range(1,30),range(0,29),3,1],'sensex':
            ['^BSESN',7,range(0,21),range(0,21),2,0],'smi':['^SSMI',6,range(1,26),range(0,25),3,1]}



#print (strike_PDF['eurostoxx'])[1]
#print strike_PDF['eurostoxx']
#print strike_PDF.keys()


def define_the_index(vol_GBM,step_dt,vol,T):
    index=raw_input()
    strike_price_list=[]
    real_price=[]

    if index in strike_PDF.keys():
        df=pdr.get_data_yahoo((strike_PDF[index])[0],date_ok)
        raw_initial=read_pdf((index+'.pdf'),pages=[1])
        if index=='eurostoxx':
            initial=float((raw_initial.ix[(strike_PDF[index])[5]][0])[:(strike_PDF[index])[1]].decode('utf-8').replace(',','').encode('utf-8'))
            initial=initial/2
        else:
            initial=float((raw_initial.ix[(strike_PDF[index])[5]][0])[:(strike_PDF[index])[1]].decode('utf-8').replace(',','').encode('utf-8'))
        table=read_pdf((index+'.pdf'),pages=[(strike_PDF[index])[4]])

    for j in reversed((strike_PDF[index])[2]):
        raw_strike_price=table.ix[j][0]
        strike_price=raw_strike_price[:(strike_PDF[index])[1]].decode('utf-8').replace(',','').encode('utf-8')
        strike_price_list.append(float(strike_price))

    for j in reversed((strike_PDF[index])[2]):
        raw_last_price=table.ix[j][10]
        if ',' in str(raw_last_price):
            last_price=raw_last_price[:(strike_PDF[index])[1]].decode('utf-8').replace(',','').encode('utf-8')
        else:
            last_price=raw_last_price

        real_price.append(float(last_price))

    close_price=float(df['Close'][0])

    change_step=[]
    index_level=[close_price]
    #vol_GBM=0.015
    #step_dt=0.01
    a=numpy.random.randn(1,1000)


    uncertainty_steps=a.tolist()[0]
    f_uncertainty_steps=uncertainty_steps[:]

    for i in range(0,1000):
        change_step.append( float( f_uncertainty_steps[i] )
                          * mat.sqrt( vol_GBM )
                          * step_dt
                          * ( close_price )
                            )           # ? do parentheses match an intent
        # this should be indented
        # I don't understand how to adjust it in here,
        # but in my code works fine with indentation of the for loop

    for i in range(0,1000):
        index_level.append(float(index_level[i])+float(change_step[i]))

    # vol = 0.35
    # T   = 0.25
    Black_list=[]
    Black_price=[]

    for j in (strike_PDF[index])[3]:
        K=float(strike_price_list[j])
        for i in range(0,1000):
            S=index_level[i]
            d1=(math.log(S/K)+0.5*T*vol*vol)/vol*T
            d2=d1-vol*T
            Black_p=((S*norm.cdf(d1)-K*norm.cdf(d2))/K)*initial
            if Black_p>0:
                Black_list.append(Black_p)
            else:
                Black_list.append(0)

        only_price=numpy.percentile(Black_list,0.01)
        Black_price.append(only_price)

    # ELN
    ELN_price=[]
    for i in (strike_PDF[index])[3]:
        ELN_price.append(Black_price[i]+100)


    Black_list_mod=[]
    Black_price_mod=[]

    for j in (strike_PDF[index])[3]:
        Km=float(strike_price_list[j])
        for i in range(0,1000):
            Sm=(index_level[i+1]/index_level[i])*close_price
            d1=(math.log(Sm/Km)+0.5*T*vol*vol)/vol*T
            d2=d1-vol*T
            Black_p_mod=((Sm*norm.cdf(d1)-Km*norm.cdf(d2))/Km)*initial
            if Black_p_mod>0:
                Black_list_mod.append(Black_p_mod)
            else:
                Black_list_mod.append(0)

        only_price_mod=numpy.percentile(Black_list_mod,0.01)
        Black_price_mod.append(only_price_mod)

    # ELN mod
    ELN_price_mod=[]
    for i in (strike_PDF[index])[3]:
        ELN_price_mod.append(Black_price_mod[i]+100)

    # plot
    plt.plot(Black_price,strike_price_list,'o-',label='Normal')
    plt.plot(Black_price_mod,strike_price_list,'o-',color='r',label='Modified')
    plt.plot(real_price,strike_price_list,'o-',color='grey',label='Last Price')
    plt.xlabel('Option Price')
    plt.ylabel('Strike Price')
    plt.title(index + ' Option Price')
    plt.legend()
    plt.savefig(index + ' Option.png')


[enter image description here][1]

    # excel
    workbook = xlsxwriter.Workbook(index+'.xlsx')
    worksheet = workbook.add_worksheet(index)
    for i in (strike_PDF[index])[3]:
        worksheet.write_number(i+1,0,strike_price_list[i])
    worksheet.write_string(0,0,index + ' Strike')
    for i in (strike_PDF[index])[3]:
        worksheet.write_number(i+1,1,Black_price[i])
    worksheet.write_string(0,1,index + ' Call Option Price')
    for i in (strike_PDF[index])[3]:
        worksheet.write_number(i+1,2,ELN_price[i])
    worksheet.write_string(0,2,index+' ELN Price')
    for i in (strike_PDF[index])[3]:
        worksheet.write_number(i+1,3,Black_price_mod[i])
    worksheet.write_string(0,3,index+' Call Option Price Mod')
    for i in (strike_PDF[index])[3]:
        worksheet.write_number(i+1,4,ELN_price_mod[i])
    worksheet.write_string(0,4,index+' ELN Price Mod')

    worksheet.insert_image('K5',index+' Option.png')

    workbook.close()

    Black_price=[]
    Black_price_mod=[]
    strike_price_list=[]
    real_price=[]

for z in range(0,4):
    print define_the_index(0.015,0.01,0.35,0.25)

要“刷新”绘图,必须清除图形的轴。否则,它们将保留整个循环中的所有数据。另见

要使用其他工作表,只需在循环过程中添加它们(但在此之前创建工作簿)。例如


将给出1份工作簿和3份工作表。这就是您要找的吗?

是否正在清除轴?我在你的代码中找不到它,也许这会起作用。我只是重置了作为绘图输入的列表,也就是在函数的末尾,在那里我放置了空列表。但是,如果有特定的命令用于清理绘图轴,请告诉我;)保存图形后,感谢您使用
plt.clf()
(清除图形)或
plt.cla()
(清除轴)!让我知道,如果我把plt.cla()放在函数的末尾,它是否有效!!!谢谢你,伙计。对于在同一个xlsx文件中添加工作表,您有什么建议吗?如果我这样做,我将只使用一个名称调用该文件,从而删除索引+'.xls'。但是,当运行循环时,它不是添加工作表,而是在现有工作表上写入,最后运行最后一个工作表。Myabe是模块xlsxwirte的一项功能?谢谢阿加尼在我的回答中展示了这一点:)如果这就是你想要的——否则,请让我知道!
import xlsxwriter

data = [[1,2,3,4],
        [5,6,7,8],
        [9, 10, 11, 12]]

def write_xlsx(data, filename):
    workbook = xlsxwriter.Workbook(filename + ".xlsx")

    for item in data:
        worksheet = workbook.add_worksheet()
        worksheet.write_row("A1", item)

    workbook.close()

write_xlsx(data, "test")