我是Python的新手(来自C / C ++),我在使这个小程序工作时遇到了一些麻烦。
它将从雅虎获得一些市场日期并打印出两张图表。图表首先工作,然后我添加了tkinter输入窗口。只需按"提交"和"打印"。问题是图表没有绘制,我认为它与tkinter没有完成或者它不能再打开第二个窗口,任何帮助都将受到高度赞赏
import pandas.io.data as web
import pandas as pd
import numpy as np
import datetime
import math
import matplotlib.pyplot as plt
import random
from itertools import accumulate
from tkinter import *
from pandas.io.data import DataReader
#Functions
def VWAP(data):
return(np.cumsum(data.Bought*data.Close)/
np.cumsum(data.Bought)).round(2)
def Print():
df2 = web.DataReader(conversion.moniker, 'yahoo', conversion.start_date, conversion.end_date)
df2['30_SMA_Close'] = pd.stats.moments.rolling_mean(df2['Close'], 30)
df2['150_SMA_Close'] = pd.stats.moments.rolling_mean(df2['Close'], 150)
df2["Bought"] = pd.Series(np.random.randint(1,1000,len(df2.index)), index=df2.index)
df2["vwap"] = pd.Series(VWAP(df2), index=df2.index)
#print("VWAP : ", df2["vwap"])
print("----------------PLOT-----------------")
top = plt.subplot2grid((6,6), (0, 0), rowspan=3, colspan=6)
top.plot(df2.index, df2["Close"], label='Close')
top.plot(df2.index, df2['30_SMA_Close'], label='30 Day SMA')
top.plot(df2.index, df2['150_SMA_Close'], label='150 Day SMA')
top.plot(df2.index, df2['vwap'], label='VWAP',color='k',linestyle="--")
plt.title('S&P Price from 2007 - 2012')
plt.legend(loc='upper left', numpoints = 1, prop={'size':7})
bottom = plt.subplot2grid((6,6), (4,0), rowspan=2, colspan=6)
bottom.bar(df2.index, df2['Volume'])
plt.title('S&P Trading Volume in Millions')
plt.gcf().set_size_inches(15,8)
print("----------------PLOT END-----------------")
def conversion():
print("Data conversion")
conversion.moniker = e1.get()
conversion.start_date = datetime.datetime.strptime(e2.get(),'%Y-%m-%d')
conversion.end_date = datetime.datetime.strptime(e3.get(),'%Y-%m-%d')
master = Tk()
Label(master, text="Moniker Name").grid(row=0)
Label(master, text="Start Date").grid(row=1)
Label(master, text="End Date").grid(row=2)
e1 = Entry(master)
e1.insert(10,"SPY")
e2 = Entry(master)
e2.insert(10,"2010-12-31")
e3 = Entry(master)
e3.insert(10,"2014-01-01")
e1.grid(row=0, column=1)
e2.grid(row=1, column=1)
e3.grid(row=2, column=1)
def close_window (): master.destroy()
Button(master, text='Cancel', command=close_window).grid(row=3, column=0, sticky=W, pady=4)
Button(master, text='Submit', command=conversion).grid(row=3, column=1, sticky=W, pady=4)
Button(master, text='Print', command=Print).grid(row=3, column=2, sticky=W, pady=4)
#df2 = web.DataReader(conversion.moniker, 'yahoo', conversion.start_date, conversion.end_date)
#df2 = web.DataReader(conversion.moniker, 'yahoo',datetime.datetime(2010,1,1),datetime.datetime(2014,1,1) )
#mainloop( )
master.destroy()
答案 0 :(得分:0)
我无法运行所有代码,因为我没有一些模块
但如果我按submit
然后按print
,我会得到一些图表
我什么都没改变。
编辑:我发现了问题 - 您需要在plt.show()
之前print('PLOT END')
这里我的代码是mods
Print
拨打conversion
,因此我不需要按钮Submit
import pandas as pd
import pandas.io.data as web
import numpy as np
import datetime
#import math
import matplotlib.pyplot as plt
import random
#from itertools import accumulate
from tkinter import *
from pandas.io.data import DataReader
#Functions
def VWAP(data):
return(np.cumsum(data.Bought*data.Close)/
np.cumsum(data.Bought)).round(2)
def ploting():
conversion()
df2 = web.DataReader(conversion.moniker, 'yahoo', conversion.start_date, conversion.end_date)
df2['30_SMA_Close'] = pd.stats.moments.rolling_mean(df2['Close'], 30)
df2['150_SMA_Close'] = pd.stats.moments.rolling_mean(df2['Close'], 150)
df2["Bought"] = pd.Series(np.random.randint(1,1000,len(df2.index)), index=df2.index)
df2["vwap"] = pd.Series(VWAP(df2), index=df2.index)
#print("VWAP : ", df2["vwap"])
print("----------------PLOT-----------------")
top = plt.subplot2grid((6,6), (0, 0), rowspan=3, colspan=6)
print top.plot(df2.index, df2["Close"], label='Close')
print top.plot(df2.index, df2['30_SMA_Close'], label='30 Day SMA')
print top.plot(df2.index, df2['150_SMA_Close'], label='150 Day SMA')
print top.plot(df2.index, df2['vwap'], label='VWAP',color='k',linestyle="--")
print plt.title('S&P Price from %s - %s' % (e2.get(), e3.get()))
print plt.legend(loc='upper left', numpoints = 1, prop={'size':7})
bottom = plt.subplot2grid((6,6), (4,0), rowspan=2, colspan=6)
print bottom.bar(df2.index, df2['Volume'])
print plt.title('S&P Trading Volume in Millions')
print plt.gcf().set_size_inches(15,8)
print plt.show() # <---- HERE
print("----------------PLOT END-----------------")
def conversion():
print("Data conversion")
conversion.moniker = e1.get()
conversion.start_date = datetime.datetime.strptime(e2.get(),'%Y-%m-%d')
conversion.end_date = datetime.datetime.strptime(e3.get(),'%Y-%m-%d')
#-----------------------------------------------------------------------------
def close_window ():
master.destroy()
master = Tk()
Label(master, text="Moniker Name").grid(row=0)
Label(master, text="Start Date").grid(row=1)
Label(master, text="End Date").grid(row=2)
e1 = Entry(master)
e1.insert(10,"SPY")
e2 = Entry(master)
e2.insert(10,"2010-12-31")
e3 = Entry(master)
e3.insert(10,"2014-01-01")
e1.grid(row=0, column=1)
e2.grid(row=1, column=1)
e3.grid(row=2, column=1)
Button(master, text='Cancel', command=close_window).grid(row=3, column=0, sticky=W, pady=4)
#Button(master, text='Submit', command=conversion).grid(row=3, column=1, sticky=W, pady=4)
Button(master, text='Plot', command=ploting).grid(row=3, column=2, sticky=W, pady=4)
#df2 = web.DataReader(conversion.moniker, 'yahoo', conversion.start_date, conversion.end_date)
#df2 = web.DataReader(conversion.moniker, 'yahoo', datetime.datetime(2010,1,1),datetime.datetime(2014,1,1) )
master.mainloop()
2008-12-31 - 2013-01-01
1998-12-31 - 2004-01-01