2017-05-25 5 views
-1

Gibt es eine Möglichkeit, das Format der Achse in einem Tkinter-Diagramm zu ändern? Wie Sie sehen können, verwende ich ein Diagramm-Widget, um meine Graphen auf meinen Rahmen zu setzen. Gibt es eine Möglichkeit für mich, die Daten unten im num/num Format zu erstellen? Wie 4/18 statt 18. April 2017? Heres mein Code: (? Auch ist es möglich, Farbe meiner Graphen Hintergründe ändern Sie sind grau vorbelegt)Datumsformat ändern und auf tkinter anzeigen

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

Antwort

0

Sie die Datetime-Bibliothek mit Hilfe der Funktion verwenden können 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 

Sie können lesen Sie mehr über diese Funktion und Formate Optionen in here

+0

thanks super! Weißt du, wie ich meine Graphen verschiedene Hintergrundfarben haben kann? Sie werden feststellen, dass wenn Sie es ausführen, es in einer grauen Box ist. Kann ich das ändern? – Addison