months = dict() for year in range(2007,2026): months[year]=[str(year*100+month) for month in range(1,13)] count, sum, sumSq, mean, stdev = dict(), dict(), dict(), dict(), dict() for firm in firms: for year in range(2007, 2026): t = (firm, year) #元組(公司,年) count[t] = 0 sum[t] = 0.0 sumSq[t] = 0.0 for row in rows: #字典value增加append元素 for firm in firms: if row[0] == firm: for year in range(2007, 2026): t = (firm, year) for month in months[year]: if row[1] == month: count[t] +=1 temp = float(row[8]) sum[t] += temp sumSq[t] += temp*temp for firm in firms: #以下計算各年度月報酬率的平均數與標準差 for year in range(2007, 2026): t = (firm, year) temp1 = count[t] temp2 = sum[t] mean[t] = temp2 / temp1 temp2 = sumSq[t] - temp2 * temp2 / temp1 temp2 = temp2/(temp1-1) stdev[t] = temp2 ** 0.5 from tkinter import * tk = Tk() #建構視窗,稱為tk tk.geometry('1000x650') tk.title('上市金融控股公司股價報酬率標準差(X軸)與平均數(Y軸)') canvas=Canvas(tk, width=1000,height=600,bg='white') canvas.pack() canvas.create_line(0,500,700,500,width=3,fill='black',arrow='last') canvas.create_line(10,600,10,5,width=3,fill='black',arrow='last') canvas.pack() canvas.create_text(700,500, text='劉任昌風險', anchor=W,fill='black', font=('Arial', 20, 'bold')) def draw(): for firm in firms: t=(firm, year.get()) x = 10+20*stdev[t]*2*3**0.5 #放大20 y = 500 - 10*mean[t]*12 #放大10 dot=canvas.create_oval(x-5,y-5,x+5,y+5,fill='blue') lab=canvas.create_text(x+10,y,text=firm[4:7],anchor=W,font=('微軟中黑體', 16)) def delete(): canvas.delete('all') years=[x for x in range(2007, 2025)] year=IntVar(tk) year.set(years[-1]) label = Label(tk, text="年度").pack(side=LEFT) #距離左側 option1 = OptionMenu(tk, year, *years).pack(side=LEFT) button1 = Button(tk, text=" 繪圖 ", command = draw, bg='black',fg='white').pack(side=LEFT) button2 = Button(tk, text=" 刪除 ", command = delete, bg='black',fg='white').pack(side=LEFT) tk.mainloop()

留言

  1. 用PRE才能呈現程式碼結構https://ktglgfk.blogspot.com/2025/06/blog-post.html

    回覆刪除

張貼留言

這個網誌中的熱門文章

侯政宇python運算CSV輸出TKINTER投資風險與報酬Markowitz