2017-05-25 33 views
-1

有没有办法在tkinter图上更改坐标轴的格式?正如你所看到的,我使用一个绘图小部件将我的图形放在我的框架上。有没有办法让我在num/num格式的底部制作日期?喜欢4/18而不是2017年4月18日?继承人我的代码:(?也就是有可能改变我的图形背景的颜色他们都默认为灰色)更改日期格式并在tkinter上显示

import numpy as np 
import datetime as dt 
import yahoo_finance as yf 
import matplotlib.pyplot as plt 
from Tkinter import * 
import quandl 

from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg 


root=Tk() 
root.geometry('1400x875') 

root.title("Stock Information") 


fmain=Frame(root, width=1400, height=900) 
fmain.place(x=100, y=0) 

today=dt.date.today() 

thirty_day_graph_frame=Frame(fmain, width=1290, height=300) 
thirty_day_graph_frame.place(x=0, y=240) 

thirty_days=dt.timedelta(days=43) 
thirty_days_ago=today-thirty_days 


five_yrs_graph_frame=Frame(fmain, width=1290, height=300) 
five_yrs_graph_frame.place(x=0, y=540) 


five_years=dt.timedelta(days=1825) 
five_years_ago=today-five_years 


def stock_info(stock_name): 
    global five_yrs_graph_frame 
    five_yrs_graph_frame.destroy() 

    global thirty_day_graph_frame 
    thirty_day_graph_frame.destroy() 





    thirty_day_graph_frame=Frame(fmain, width=1290, height=300) 
    thirty_day_graph_frame.place(x=0, y=240) 

    five_yrs_graph_frame=Frame(fmain, width=1290, height=300) 
    five_yrs_graph_frame.place(x=0, y=540) 

    stock=yf.Share(stock_name) 
    stock_price=stock.get_price() 

    name_price_label=Label(fmain, text=(stock_name,':', stock_price),font=("Times New Roman",23)) 
    name_price_label.place(x=400, y=10) 

    day_change=stock.get_change() 

    if float(day_change) > 0: 
     font_color="green" 
    elif float(day_change) < 0: 
     font_color="red" 
    else: 
     font_color="yellow" 

    day_change_label=Label(fmain, text=(day_change),font=("Times New Roman",20),fg=str(font_color)) 
    day_change_label.place(x=400, y=40) 

    thirty_day_data = quandl.get("WIKI/"+str(stock_name), start_date=str(thirty_days_ago), end_date=str(today),column_index=4) #So quandl.get gives a lot of info, so the column_index=4 is just getting closing prices 
    five_year_data = quandl.get("WIKI/"+str(stock_name),start_date=str(five_years_ago), end_date=str(today), column_index=4) 


    thirty_day_fig = plt.figure(figsize=(10,3.75)) 
    plt.plot(thirty_day_data) 
    canvas = FigureCanvasTkAgg(thirty_day_fig, master=thirty_day_graph_frame) 
    plot_widget = canvas.get_tk_widget() 
    plot_widget.place(x=0,y=0) 


    five_year_fig=plt.figure(figsize=(10,3.75)) 
    plt.plot(five_year_data) 
    canvas1=FigureCanvasTkAgg(five_year_fig, master=five_yrs_graph_frame) 
    plot_widget1=canvas1.get_tk_widget() 
    plot_widget1.place(x=1,y=0) 



apple_button=Button(root,text='AAPL', command=lambda:stock_info('AAPL')) 
tesla_button=Button(root,text='TSLA', command=lambda:stock_info('TSLA')) 
google_button=Button(root,text='GOOG', command=lambda:stock_info('GOOG')) 


apple_button.place(x=10, y=15) 
tesla_button.place(x=10, y=45) 
google_button.place(x=10,y=75) 

root.mainloop() 

回答

0

您可以使用datetime库使用功能strftime

from datetime import datetime 

today = datetime.today() 
print ('ISO :', today) # ISO: 2017-05-25 16:23:35.850570 
format = "%d/%m" 
s = today.strftime(format) 

print (s) # 25/05 

你可以阅读更多关于这个功能和格式选项here

+0

谢谢你真棒!你知道如何让我的图表具有不同的背景颜色吗?你会注意到,当你运行它在一个灰色的框。我可以改变它吗? – Addison