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.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() diff --git a/plot_figure_dev.py b/plot_figure_dev.py new file mode 100644 index 0000000..219ffc0 --- /dev/null +++ b/plot_figure_dev.py @@ -0,0 +1,148 @@ +# -*- coding: UTF-8 -*- + +import matplotlib.pyplot as plt +import pandas as pd +import numpy as np +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' + +class plt_fig(): + def __init__(self): + tt = time.gmtime() + self.today = str(tt.tm_year) + str(tt.tm_mon) + str(tt.tm_mday) + + 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') + 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') + 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() + + 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') + 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() + 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() + 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)。