Matplotlib pyplot图的Tkinter输入
我对Python非常陌生(来自C/C++),在让这个小程序正常工作时遇到了一些困难 它将从雅虎那里获得一些市场数据,并打印出两张图表。这些图表一开始工作正常,然后我添加了tkinter输入窗口。只需按“提交”和“打印”。问题是图形没有绘制,我想这与tkinter没有完成或者它无法再打开第二个窗口有关,任何帮助都将不胜感激Matplotlib pyplot图的Tkinter输入,matplotlib,tkinter,Matplotlib,Tkinter,我对Python非常陌生(来自C/C++),在让这个小程序正常工作时遇到了一些困难 它将从雅虎那里获得一些市场数据,并打印出两张图表。这些图表一开始工作正常,然后我添加了tkinter输入窗口。只需按“提交”和“打印”。问题是图形没有绘制,我想这与tkinter没有完成或者它无法再打开第二个窗口有关,任何帮助都将不胜感激 import pandas.io.data as web import pandas as pd import numpy as np import datetime impo
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()
我无法运行所有代码,因为我没有一些模块
但是如果我按
submit
,然后按print
,我会得到一些图表。我几乎什么也没变
编辑:我发现了问题-您需要在打印(“打印结束”)之前执行
plt.show()
这里是我的mods代码
- 按钮
调用打印
所以我不需要按钮转换
提交
- 情节标题中的日期
将熊猫作为pd导入
将pandas.io.data作为web导入
将numpy作为np导入
导入日期时间
#输入数学
将matplotlib.pyplot作为plt导入
随机输入
#从itertools导入
从tkinter进口*
从pandas.io.data导入数据读取器
#功能
def VWAP(数据):
返回(np.cumsum(data.bunded*data.Close)/
np.cumsum(data.Bunded)).round(2)
def绘图():
转换()
df2=web.DataReader(conversion.monike'yahoo',conversion.start\u date,conversion.end\u date)
df2['30_SMA_Close']=pd.统计数据.力矩.滚动平均值(df2['Close'],30)
df2['150_SMA_Close']=pd.统计数据.力矩.滚动平均值(df2['Close'],150)
df2[“购买”]=pd.系列(np.random.randint(11000,len(df2.index)),index=df2.index)
df2[“vwap”]=pd.系列(vwap(df2),索引=df2.索引)
#打印(“VWAP:,df2[“VWAP”])
打印(“--------------绘图--------------------”)
top=plt.subplot2grid((6,6),(0,0),rowspan=3,colspan=6)
打印top.plot(df2.index,df2[“关闭”],label='Close')
打印顶部绘图(df2.index,df2['30\u SMA\u Close',label='30天SMA')
打印顶部绘图(df2.index,df2['150\u SMA\u Close',标签='150天SMA')
打印顶部.plot(df2.index,df2['vwap'],label='vwap',color='k',linestyle=“--”)
打印plt.title(“%S-%S%”(e2.get(),e3.get())的标准普尔价格)
打印plt.legend(loc='左上角',numpoints=1,prop={'size':7})
底部=plt.subplot2grid((6,6)、(4,0),行span=2,列span=6)
打印底部的.bar(df2.index,df2['Volume'])
打印打印打印标题(“标准普尔交易量以百万计”)
打印plt.gcf().设置尺寸英寸(15,8)
打印plt.show()#缩进不正确-在问题编辑期间使用按钮{}
。关闭窗口()
是否为空函数?然后在其中使用pass
。在最后一行中运行master.destroy()
,关闭窗口。您忘记使用root.masterloop()错过了master.destroy()再次粘贴您的工作代码(并在其上使用按钮{}
)这就是它应该显示的样子,您是否尝试了不同的时间范围,因此,图表发生了变化,看到了新的图像2008-12-31-2013-01-01
和1998-12-31-2004-01-01
我在一些Python Shell中运行代码(DreamingPie
)并得到了图。但当我直接在控制台中运行它时,它不起作用。但我发现了问题。在打印(“打印结束”)之前,需要plt.show()
。我添加了带有答案的代码。嗨,所以我再次尝试在我的机器上运行它,但在第27行print top.plot(df2.index,df2[“Close”],label='Close')上出现语法错误。知道我缺少什么吗???Cheesersolved:所有打印语句缺少括号
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()