From 4024188f203a4b601a6ffe21fa538b2857410e00 Mon Sep 17 00:00:00 2001 From: WellenWoo Date: Tue, 6 Mar 2018 18:37:28 +0800 Subject: [PATCH 1/5] branch dev add a dev ver,change GUI,delete "sum_boxoffice","sum_boxoffice_pre"button --- BoxOfficeGui_dev.py | 152 ++++++++++++++++++++++++++++++++++++++++++++ plot_figure_dev.py | 141 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 293 insertions(+) create mode 100644 BoxOfficeGui_dev.py create mode 100644 plot_figure_dev.py diff --git a/BoxOfficeGui_dev.py b/BoxOfficeGui_dev.py new file mode 100644 index 0000000..3c91b21 --- /dev/null +++ b/BoxOfficeGui_dev.py @@ -0,0 +1,152 @@ +#-*- coding: utf-8 -*- + +import wx +import wx.lib.dialogs +from collections import namedtuple + +from plot_figure_dev import plt_fig,plt_fig_month +from utility_template import layout_template + +__author__ = 'WellenWoo' +__mail__ = 'wellenwoo@163.com' + +""" +本程序可获取国内当天票房和近期历史电影票房数据, +并将其可视化。 +""" + +class MainWindow(wx.Frame): + def __init__(self,parent,title): + wx.Frame.__init__(self,parent,title=title,size=(600,-1)) + static_font = wx.Font(12, wx.SWISS, wx.NORMAL, wx.NORMAL) + static_font2 = wx.Font(10, wx.SWISS, wx.NORMAL, wx.NORMAL) + + Size = namedtuple("Size",['x','y']) + s = Size(100,50) + sm = Size(100,25) + + """预定义参数""" + self.fig = plt_fig() + self.fig_month = plt_fig_month() + self.lt = layout_template() + self.name = 'BoxOffice_plot' + self.version = '1.0' + self.des = '''BoxOffice data visualization.\n''' + self.git_website = "https://github.com/WellenWoo/BoxOfficePlot" + self.copyright = "(C) 2017 All Right Reserved" + + b_labels = [ + 'day_boxoffice', + 'day_boxoffice_pre', +## 'sum_boxoffice', +## 'sum_boxoffice_pre', + 'month_boxoffice', + 'month_boxoffice_pre' + ] + + TipString = [ u'今日票房榜', + u'今日票房占比', +## u'总票房榜', +## u'总票房占比', + u'月票房榜', + u'月票房占比', + ] + funcs = [self.day_boxoffice,self.day_boxoffice_pre, +## self.sum_boxoffice,self.sum_boxoffice_pre, + self.month_boxoffice,self.month_boxoffice_pre] + + """创建菜单栏""" + filemenu = wx.Menu() + menuExit = filemenu.Append(wx.ID_EXIT,"E&xit\tCtrl+Q","Tenminate the program 退出") + + helpmenu = wx.Menu () + menuhelpdoc = helpmenu.Append(wx.ID_HELP ,"Help\tF1","Help") + menuAbout = helpmenu.Append(wx.ID_ABOUT ,"&About","Information about this program 关于本软件") + + menuBar = wx.MenuBar () + menuBar.Append(filemenu,"&File") + menuBar.Append(helpmenu,"&Help") + self.SetMenuBar(menuBar) + + '''创建按钮''' + self.sizer0 = wx.FlexGridSizer(rows=2, hgap=4, vgap=2) + buttons = [] + for i,label in enumerate(b_labels): + b = wx.Button(self, id = i,label = label,size = (1.5*s.x,s.y)) + buttons.append(b) + self.sizer0.Add(b) + + '''菜单绑定函数''' + self.Bind(wx.EVT_MENU,self.OnExit,menuExit) + self.Bind(wx.EVT_MENU,self.OnAbout,menuAbout) + self.Bind(wx.EVT_MENU,self.Onhelpdoc,menuhelpdoc) + + '''设置各控件的颜色、字体属性,绑定控件函数''' + for i,button in enumerate(buttons): + button.SetForegroundColour('red') + button.SetFont(static_font2) + button.SetToolTipString(TipString[i]) + button.Bind(wx.EVT_BUTTON,funcs[i]) + + '''设置页面布局''' + self.SetSizer(self.sizer0) + self.SetAutoLayout(1) + self.sizer0.Fit(self) + + self.CreateStatusBar() + self.Show(True) + + def day_boxoffice(self,evt): + self.fig.day_boxoffice() + + def day_boxoffice_pre(self,evt): + self.fig.day_boxoffice_pre() + + def month_boxoffice(self,evt): + month = self.get_month() + title = u'{m}票房'.format(m = month) + self.fig_month.day_boxoffice(u'月份票房',u'票房\万元',month) + + def month_boxoffice_pre(self,evt): + month = self.get_month() + title = u'{m}票房占比'.format(m = month) + self.fig_month.day_boxoffice_pre(title,month) + + def get_month(self): + dlg = wx.TextEntryDialog( + self, u'please input the month输入月份', + 'month_boxoffice', 'oooo-oo') + dlg.SetValue("xxxx-xx") + if dlg.ShowModal() == wx.ID_OK: + month = dlg.GetValue() + dlg.Destroy() + return month + + def OnExit(self,event): + """退出函数""" + self.Close() + + def OnAbout(self, evt): + info = self.lt.About_info(self.name,self.version,self.copyright, + self.des,self.git_website, + __author__+'\n'+__mail__,wx.ClientDC(self)) + wx.AboutBox(info) + + def Onhelpdoc(self, evt): + f0 = "readme.md" + with open(f0,"r") as f: + helpdoc = f.read() + dlg = wx.lib.dialogs.ScrolledMessageDialog(self, helpdoc, u"helpdoc使用说明") + dlg.ShowModal() + + def raise_msg(self,msg): + '''添加警告信息''' + info = wx.AboutDialogInfo() + info.Name = "Warning Message" + info.Copyright = msg + wx.AboutBox(info) + +if __name__ == '__main__': + app = wx.App(False) + frame = MainWindow(None,'BoxOffice_Plot') + app.MainLoop() diff --git a/plot_figure_dev.py b/plot_figure_dev.py new file mode 100644 index 0000000..ba892d4 --- /dev/null +++ b/plot_figure_dev.py @@ -0,0 +1,141 @@ +# -*- coding: UTF-8 -*- + +import matplotlib.pyplot as plt +import pandas as pd +import numpy as np +import tushare as ts +import time +import os + +plt.rcParams['font.family'] = 'SimHei' +__author__ = 'WellenWoo' + +class plt_fig(): + def __init__(self): + tt = time.gmtime() + self.today = str(tt[0]) + str(tt[1]) + str(tt[2]) + + def cd_dir(self): + path_now = os.getcwd() + if path_now.endswith('data'): + return None + elif os.path.exists('data'): + pass + else: + os.mkdir('data') + os.chdir(r'data') + + def get_data(self,*args): + self.cd_dir() + f0 = self.today+'.xlsx' + try: + d1 = pd.read_excel(f0) + except IOError: + d0 = ts.realtime_boxoffice() + d0.to_excel(f0) + d1 = pd.read_excel(f0) + d2 = d1.Irank + d3 = d1.BoxOffice + d4 = d1.MovieName + d5 = d1.sumBoxOffice + return d2,d3,d4,d5 + + def day_boxoffice(self,title1 = u'本日票房',title2 =u'本日影片累计票房',ylabel = u'票房\万元',*args): + if len(args)>0: + irank,box,name,sumbox = self.get_data(args[0]) + else: + irank,box,name,sumbox = self.get_data() + self.plt_bar(irank,box,sumbox,name,title1,title2,ylabel) + + def day_boxoffice_pre(self,title1 = u'本日票房占比',title2 = u'累计票房占比',*args): + if len(args)>0: + irank,box,name,sumbox = self.get_data(args[0]) + else: + irank,box,name,sumbox = self.get_data() + self.plt_pie(name,box,sumbox,title1,title2) + + def plt_bar(self,xdata,y1,y2,xticks,title1,title2,ylabel): + fig,(ax0,ax1) = plt.subplots(nrows=2, figsize=(6, 8)) + bar_width = 0.65 + + ax0.bar(xdata,y1,bar_width,color = 'r') + ax0.set_title(title1) + ax0.set_ylabel(ylabel) + ax0.grid() + + ax1.bar(xdata,y2,bar_width,color = 'b') + plt.xticks(xdata+bar_width/2,xticks) + ax1.set_title(title2) + ax1.set_ylabel(ylabel) + ax1.grid() + + fig.autofmt_xdate() + plt.tight_layout() + plt.show() + + def plt_pie(self,labels,y1,y2,title1,title2): + fig,(ax0,ax1) = plt.subplots(ncols=2, figsize=(8, 6)) + + ax0.pie(y1,labels = labels,autopct = '%1.1f%%',shadow = True) + ax0.set_title(title1) + + ax1.pie(y2,labels = labels,autopct = '%1.1f%%',shadow = True) + ax1.set_title(title2) + + plt.show() + +class plt_fig_month(plt_fig): + + def get_data(self,*args): + self.cd_dir() + month = args[0] + f0 = month+'.xlsx' + try: + d1 = pd.read_excel(f0) + except IOError: + d0 = ts.month_boxoffice(month) + d0.to_excel(f0) + d1 = pd.read_excel(f0) + d2 = d1.Irank[:-1] + d3 = d1.boxoffice[:-1] + d4 = d1.MovieName[:-1] + d5 = d1.avgboxoffice[:-1] + return d2,d3,d4,d5 + + def day_boxoffice(self,title = u'本日票房',ylabel = u'票房\万元',*args): + if len(args)>0: + irank,box,name,sumbox = self.get_data(args[0]) + else: + irank,box,name,sumbox = self.get_data() + self.plt_bar(irank,box,name,title,ylabel) + + def day_boxoffice_pre(self,title = u'本日票房占比',*args): + if len(args)>0: + irank,box,name,sumbox = self.get_data(args[0]) + else: + irank,box,name,sumbox = self.get_data() + self.plt_pie(box,name,title) + + def plt_bar(self,xdata,ydata,xticks,title,ylabel): + fig = plt.figure() + ax = fig.add_subplot(111) + bar_width = 0.65 + + ax.bar(xdata,ydata,bar_width,color = 'r') + plt.xticks(xdata+bar_width/2,xticks) + ax.set_title(title) + ax.set_ylabel(ylabel) + plt.grid() + fig.autofmt_xdate() + plt.tight_layout() + plt.show() + + def plt_pie(self,xdata,ydata,title): + fig = plt.figure() + ax = fig.add_subplot(111) + + ax = fig.add_subplot(111) + ax.pie(xdata,labels = ydata,autopct = '%1.1f%%',shadow = True) + ax.set_title(title) + plt.show() + From af17731c21bf178fc1d204ba96b8522e5df5467d Mon Sep 17 00:00:00 2001 From: WellenWoo Date: Tue, 6 Mar 2018 22:54:36 +0800 Subject: [PATCH 2/5] rewrite plt_bar function with seaborn MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 用seaborn代替matplotlib 改写plt_bar函数。 --- plot_figure_dev.py | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/plot_figure_dev.py b/plot_figure_dev.py index ba892d4..4f96709 100644 --- a/plot_figure_dev.py +++ b/plot_figure_dev.py @@ -6,7 +6,9 @@ import tushare as ts import time import os +import seaborn as sns +sns.set(style="white", context="talk") plt.rcParams['font.family'] = 'SimHei' __author__ = 'WellenWoo' @@ -58,14 +60,17 @@ def plt_bar(self,xdata,y1,y2,xticks,title1,title2,ylabel): fig,(ax0,ax1) = plt.subplots(nrows=2, figsize=(6, 8)) bar_width = 0.65 - ax0.bar(xdata,y1,bar_width,color = 'r') +## ax0.bar(xdata,y1,bar_width,color = 'r') + sns.barplot(xdata,y1,palette = "Set1",ax = ax0) ax0.set_title(title1) ax0.set_ylabel(ylabel) ax0.grid() - ax1.bar(xdata,y2,bar_width,color = 'b') - plt.xticks(xdata+bar_width/2,xticks) +## ax1.bar(xdata,y2,bar_width,color = 'b') + sns.barplot(xdata,y2,palette = "Set2",ax = ax1) + plt.xticks(xdata-bar_width,xticks) ax1.set_title(title2) + ax1.set_xlabel("MovieName") ax1.set_ylabel(ylabel) ax1.grid() @@ -121,8 +126,10 @@ def plt_bar(self,xdata,ydata,xticks,title,ylabel): ax = fig.add_subplot(111) bar_width = 0.65 - ax.bar(xdata,ydata,bar_width,color = 'r') - plt.xticks(xdata+bar_width/2,xticks) +## ax.bar(xdata,ydata,bar_width,color = 'r') + sns.barplot(xdata,ydata,palette = "Set3",ax = ax) + plt.xticks(xdata-bar_width,xticks) + ax.set_xlabel("MovieName") ax.set_title(title) ax.set_ylabel(ylabel) plt.grid() From 134c38ea861cd5f2792198879f570c796c103458 Mon Sep 17 00:00:00 2001 From: WellenWoo Date: Tue, 6 Mar 2018 23:02:36 +0800 Subject: [PATCH 3/5] updata readme.md for dev version --- readme.md | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/readme.md b/readme.md index 789e8eb..1165fdc 100644 --- a/readme.md +++ b/readme.md @@ -1,18 +1,21 @@ # **BoxOffice_Plot** 0. 本程序可获取国内当天票房和近期历史电影票房数据,并将其可视化。 + 1. day_boxoffice按钮:获取当天票房数据并以excel表格形式保存在data文件夹下, - 并据此绘制当日票房榜; - day_boxoffice_pre按钮:获取当天票房数据,并据此绘制当天票房百分占比饼图; - sum_boxoffice按钮:获取当天票房数据,绘制当天上映电影的累计票房柱状图; - sum_boxoffice_pre按钮:获取当天票房数据,绘制当天上映电影累计票房的百分占比饼图。 + 并据此绘制当日票房榜及当天上映电影的累计票房柱状图; + + day_boxoffice_pre按钮:获取当天票房数据,并据此绘制当天票房百分占比饼图及当天上映电影累计票房的百分占比饼图; + month_boxoffice按钮:获取指定月份的票房数据,并绘制该月票房榜(以“xxxx-xx"形式输入指定月份); + month_boxoffice_pre按钮:获取指定月份的票房数据,并绘制该月上映电影票房的百分之百饼图。 + 2. 票房数据均采集自第三方网站,本程序不负责其真实性或精确性。 ## Note 注意事项: 需安装以下第三方库,本脚本才能正常运行: -wx,tushare,matplotlib,pandas,numpy。 +wx,tushare,matplotlib,pandas,numpy,seaborn。 ## Feedback: 有任何疑问或建议欢迎[反馈](https://github.com/WellenWoo/BoxOffice_Plot.git)。 From 1d3045d48b1c8814df5bc9928d53697a96dc39a8 Mon Sep 17 00:00:00 2001 From: WellenWoo Date: Fri, 5 Jul 2019 15:35:37 +0800 Subject: [PATCH 4/5] modify self.today expression MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 修改self.today 的赋值方式 --- plot_figure.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plot_figure.py b/plot_figure.py index fc71d99..e84ef31 100644 --- a/plot_figure.py +++ b/plot_figure.py @@ -13,7 +13,7 @@ class plt_fig(): def __init__(self): tt = time.gmtime() - self.today = str(tt[0]) + str(tt[1]) + str(tt[2]) + self.today = str(tt.tm_year) + str(tt.tm_mon) + str(tt.tm_mday) def cd_dir(self): path_now = os.getcwd() From 170ed4e36207f19737edce36312856ac35eb3fb3 Mon Sep 17 00:00:00 2001 From: WellenWoo Date: Fri, 5 Jul 2019 15:36:33 +0800 Subject: [PATCH 5/5] modify self.today expression MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 修改self.today的赋值方式 --- plot_figure_dev.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plot_figure_dev.py b/plot_figure_dev.py index 4f96709..219ffc0 100644 --- a/plot_figure_dev.py +++ b/plot_figure_dev.py @@ -15,7 +15,7 @@ class plt_fig(): def __init__(self): tt = time.gmtime() - self.today = str(tt[0]) + str(tt[1]) + str(tt[2]) + self.today = str(tt.tm_year) + str(tt.tm_mon) + str(tt.tm_mday) def cd_dir(self): path_now = os.getcwd()