在python中使用matplotlib循环

问题描述 投票:-1回答:1

我定义了一个函数(也许我将它分开并将所有内容放在一个类中),它从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)
python excel matplotlib plot quantitative-finance
1个回答
0
投票

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

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

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")

将为1个工作簿提供3个工作表。那是你在找什么?

© www.soinside.com 2019 - 2024. All rights reserved.