|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
如题,参加python比赛,在论坛朋友的帮助下本来已经写好了,但是比赛说不让用pandas库,我已经写好了,想用泡菜保存为本地二进制文件,但还是不行,还有两天交稿,有没有抢救的方法
- #import pandas as pd
- import matplotlib.pyplot as plt
- import matplotlib.ticker as ticker
- import matplotlib.animation as animation
- import random
- import pickle
- import easygui as g
- import os
- '''def get_pandafile(name):
- data0 = pd.read_excel(name, sheet_name='Sheet1',
- usecols=['大名称', '小名称', '日期', '累计确诊'])
- pickle_file = open('%s.pkl'% name, 'wb')
- pickle.dump(data0, pickle_file)
- pickle_file.close()'''
- def randomcolor():
- colorlist = ['1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F']
- color = ''
- for i in range(6):
- color += random.choice(colorlist)
- return '#' + color
- def get_colordict():
- area_list1 = set(df['小名称'])
- color_list = []
- for i in range(len(area_list1)):
- str_1 = randomcolor()
- color_list.append(str_1)
- str_1 = randomcolor()
- area_list2 = [i for i in area_list1]
- color_dict = dict(zip(area_list2, color_list))
- return color_dict
- def load_data(name):
- pickle_file = open('%s.pkl' % name, 'rb')
- pdfile = pickle.load(pickle_file)
- return pdfile
- try:
- choices = ['绘制中国新冠疫情动态图','世界各国新冠肺炎疫情动态图']
- reply = g.choicebox('请选择需要绘制的图像', choices = choices)
- #get_pandafile('世界各国新冠肺炎疫情.xlsx')
- #get_pandafile('中国省份新冠肺炎疫情.xlsx')
- #print('(输入1绘制中国疫情动态图,输入2绘制世界疫情动态图)')
- #a = input('请选择需要绘制的图像:')
- if reply=='绘制中国新冠疫情动态图':
- name = '中国省份新冠肺炎疫情.xlsx'
- elif reply=='世界各国新冠肺炎疫情动态图':
- name = '世界各国新冠肺炎疫情.xlsx'
- df = load_data(name)
- except NameError as reason:
- g.msgbox('谢谢使用')
- os._exit(0)
- colorlist = get_colordict()
- fig, ax = plt.subplots(figsize=(15, 8))
- colors = dict(zip(
- ['India', 'Europe', 'Asia', 'Latin America', 'Middle East', 'North America', 'Africa'],
- ['#adb0ff', '#ffb3ff', '#90d595', '#e48381', '#aafbff', '#f7bb5f', '#eafb50']
- ))
- group_lk = df.set_index('大名称')['小名称'].to_dict()
- def draw_barchart(current_day):
- dff = df[df['日期'].eq(current_day)].sort_values(by='累计确诊', ascending=True).tail(10)
- ax.clear()
- plt.rcParams['font.sans-serif'] = ['SimHei']
- ax.barh(dff['小名称'], dff['累计确诊'], color=[colorlist[x] for x in dff['小名称']])
- dx = dff['累计确诊'].max() / 200
- for i, (value, name) in enumerate(zip(dff['累计确诊'], dff['大名称'])):
- ax.text(value - dx, i, name, size=14, weight=600, ha='right', va='bottom')
- ax.text(value - dx, i - .25, group_lk[name], size=10, color='#444444', ha='right', va='baseline')
- ax.text(value + dx, i, f'{value:,.0f}', size=14, ha='left', va='center')
- ax.text(1, 0.4, current_day, transform=ax.transAxes, color='#777777', size=46, ha='right', weight=800)
- ax.text(0, 1.06, '累计人数', transform=ax.transAxes, size=12, color='#777777')
- ax.xaxis.set_major_formatter(ticker.StrMethodFormatter('{x:,.0f}'))
- ax.xaxis.set_ticks_position('top')
- ax.tick_params(axis='x', colors='#777777', labelsize=12)
- ax.set_yticks([])
- ax.margins(0, 0.01)
- ax.grid(which='major', axis='x', linestyle='-')
- ax.set_axisbelow(True)
- ax.text(0, 1.12, '新冠疫情累计确诊人数',
- transform=ax.transAxes, size=24, weight=600, ha='left')
- ax.text(1, 0, 'by二中朱宝恒', transform=ax.transAxes, ha='right',
- color='#777777', bbox=dict(facecolor='white', alpha=0.8, edgecolor='white'))
- plt.box(False)
- month_day = [0, 31, 29, 31, 30, 31, 30]
- def all_day(start, end):
- days = []
- cur_day = start
- while cur_day < end:
- mon = (cur_day // 100) % 100
- if cur_day % 100 == month_day[mon]:
- cur_day = 20200000 + (mon + 1) * 100 + 1
- else:
- cur_day += 1
- days.append(cur_day)
- return days
- days = all_day(20200122, 20200608)
- if __name__ == '__main__':
- draw_barchart(20200607)
- animator = animation.FuncAnimation(fig, draw_barchart, frames=days)
- plt.show()
复制代码 |
|