import matplotlib.pyplot as plt from matplotlib import font_manager import pymysql class show(object): info = {} TScore = [] # 综合评分 name = [] # 动漫名字 bfl = [] # 播放量 pls = [] # 评论数 scs = [] # 收藏数 def save(self):#把数据从数据中取出存入对象 db = pymysql.connect(host='47.106.183.36', port=3306, user='fuchuang', password='fuchuang', database='fuchuang', charset='utf8mb4') # 数据库连接 use = 'use fuchuang;' show = 'show tables;' str = 'select * from bangumi;' cursor = db.cursor() try: cursor.execute(show)# cursor.execute(str)#选中表 desc = cursor.description data = cursor.fetchall()#获取表信息 list = [] for data in data:#筛选出评分大于6的数据 if (data[3] != '暂无评分'): if (float(data[3]) > 9.5): #print(data) self.name.append(data[1])# 动漫名字 list = [data[2]] if '万' in list[0]: list[0] = float(list[0].replace('万', '')) elif '亿' in list[0]: list[0] = float(list[0].replace('亿', '')) * 10000 self.bfl.append(list[0]) # 播放量 list = [data[4]] if '万' in list[0]: list[0] = float(list[0].replace('万', '')) else: list[0] = float(list[0]) self.TScore.append(float(data[3])*list[0]) # 综合评分 self.scs.append(list[0])# 收藏数 list = [data[5]] if '万' in list[0]: list[0] = float(list[0].replace('万', '')) else: list[0] = float(list[0]) self.pls.append(list[0])# 评论数 except Exception as e: print(e) db.rollback() finally: cursor.close() db.close() print(self.name) print(self.TScore) print(self.bfl) print(self.pls) print(self.scs) #info = {'动漫名': self.name, '播放量(万)': self.bfl, '评论数(万)': self.pls, '收藏数(万)': self.scs, '综合评分': self.TScore} #dm_file = pandas.DataFrame(info) #dm_file.to_excel('Dongman.xlsx', sheet_name="动漫数据分析") # 将所有列表返回 return self.name, self.bfl, self.pls, self.scs, self.TScore def view(self):#数据可视化 # 为了坐标轴上能显示中文 plt.rcParams['font.sans-serif'] = ['SimHei'] plt.rcParams['axes.unicode_minus'] = False my_font = font_manager.FontProperties(fname='STHeiti-Medium.ttc') dm_name = self.name # 番剧名 dm_play = self.bfl # 番剧播放量 dm_review = self.pls # 番剧评论数 dm_favorite = self.scs # 番剧收藏数 dm_com_score = self.TScore # 番剧评分 y_score = [9.6, 9.7, 9.8, 9.9,10.0] #dm_all = self.TScore * self.scs fig, ax1 = plt.subplots() plt.bar(dm_name, dm_com_score, color='red') plt.title('综合评分和播放量数据分校', fontproperties=my_font) ax1.tick_params(labelsize=6) plt.xlabel('番剧名') plt.ylabel('综合评分') plt.xticks(rotation=90, color='green') ax2 = ax1.twinx() ax2.plot(dm_play, color='cyan') plt.ylabel('播放量') plt.plot(1, label='评分', color='red', linewidth=5.0) plt.plot(1, label='播放量', color="cyan", linewidth=1.0, linestyle="-") plt.legend() plt.savefig(r'E:1.png', dpi=1000, bbox_inches='tight') # **********************************************************************评论数和收藏数对比 # ********评论数条形图 fig, ax3 = plt.subplots() plt.bar(dm_name, dm_play, color='green') plt.title('番剧收藏数与评论数分析') plt.ylabel('评论数(万)') ax3.tick_params(labelsize=6) plt.xticks(rotation=90, color='green') # *******收藏数折线图 ax4 = ax3.twinx() # 组合图必须加这个 ax4.plot(dm_favorite, color='yellow') # 设置线粗细,节点样式 plt.ylabel('收藏数(万)') plt.plot(1, label='评论数', color="green", linewidth=5.0) plt.plot(1, label='收藏数', color="yellow", linewidth=1.0, linestyle="-") plt.legend() plt.savefig(r'E:2.png', dpi=1000, bbox_inches='tight') # **********************************************************************综合评分和收藏数对比 # *******综合评分条形图 fig, ax5 = plt.subplots() plt.bar(dm_name, dm_com_score, color='red') plt.title('综合评分和收藏数量数据分析') plt.ylabel('综合评分') ax5.tick_params(labelsize=6) plt.xticks(rotation=90, color='green') # *******收藏折线图 ax6 = ax5.twinx() # 组合图必须加这个 ax6.plot(dm_favorite, color='yellow') # 设置线粗细,节点样式 plt.ylabel('收藏数(万)') plt.plot(1, label='综合评分', color="red", linewidth=5.0) plt.plot(1, label='收藏数', color="yellow", linewidth=1.0, linestyle="-") plt.legend() plt.savefig(r'E:3.png', dpi=1000, bbox_inches='tight') # **********************************************************************播放量和评论数对比 # *******播放量条形图 fig, ax7 = plt.subplots() plt.bar(dm_name, dm_play, color='cyan') plt.title('播放量和收藏数 数据分析') plt.ylabel('播放量(万)') ax7.tick_params(labelsize=6) plt.xticks(rotation=90, color='green') # *******收藏数折线图 ax8 = ax7.twinx() # 组合图必须加这个 ax8.plot(dm_favorite, color='yellow') # 设置线粗细,节点样式 plt.ylabel('收藏数(万)') plt.plot(1, label='评论数', color="green", linewidth=5.0) plt.plot(1, label='收藏数', color="yellow", linewidth=1.0, linestyle="-") plt.legend() plt.savefig(r'E:4.png', dpi=1000, bbox_inches='tight') # *******评论数折线图 # ax8 = ax7.twinx() # 组合图必须加这个 # ax8.plot(dm_review, color='green') # 设置线粗细,节点样式 # plt.ylabel('评论数(万)') # plt.plot(1, label='播放量', color="cyan", linewidth=5.0) # plt.plot(1, label='评论数', color="green", linewidth=1.0, linestyle="-") # plt.legend() # plt.savefig(r'E:4.png', dpi=1000, bbox_inches='tight') #评论数的数据展示有问题 plt.show() def print(self): print(len(self.name)) print(len(self.bfl)) print(len(self.pls)) print(len(self.scs)) print(len(self.TScore)) def sort(self,i,j): temp = self.name[i] self.name[i] = self.name[j] self.name[j] = temp temp = self.bfl[i] self.bfl[i] = self.bfl[j] self.bfl[j] = temp temp = self.pls[i] self.pls[i] = self.pls[j] self.pls[j] = temp temp = self.scs[i] self.scs[i] = self.scs[j] self.scs[j] = temp temp = self.TScore[i] self.TScore[i] = self.TScore[j] self.TScore[j] = temp def main(): a=show()#创建对象 a.save()#从数据库取数据 a.print() # 输出各个数据个数 a.view()#可视化 if __name__ == '__main__': main()