【头歌】综合训练项目3:新冠疫情数据分析与可视化

第1关:从文件中读取疫情数据

def get_data(file_name, read_type='r'):
    # 请在此添加代码,实现编程要求
    #********** Begin *********#
    infile = open(file_name,'r')
    data = infile.readlines()
    for i in range(1,len(data)):
        data[i] = data[i].strip().split(',')
    #********** End **********#
    return data

file_in = input()
result = get_data('src/step1/'+file_in, 'r')
print("地区 ", "确诊人数 ", "治愈人数 ", "死亡人数" )
result=result[1:]

temp="{}{:>9s}{:>9s}{:>9s}"
for data in result:
    data[0]=data[0].replace('-','0')
    data[1]=data[1].replace('-','0')
    data[2]=data[2].replace('-','0')
    data[3]=data[3].replace('-','0')
    print(temp.format(data[0],data[1],data[2],data[3]))
    

第2关:结合列表与字典的疫情数据存储

def get_data(file_name, read_type='r'):
    # 请在此添加代码,实现编程要求
    #********** Begin *********#
    infile = open(file_name,'r',encoding="utf-8")
    data = infile.readlines()
    for i in range(0,len(data)):
        data[i] = data[i].strip().split(',')
    #********** End **********#
    return data

# 读取各省份疫情数据
outbreak_data = input()  # 2月5日上午9点疫情数据.csv
infile = open('src/step2/'+outbreak_data, 'r', encoding="utf-8")
result = get_data('src/step2/'+outbreak_data, 'r')
data = infile.readlines()
types = data[0].strip().split(',')[1:4]    # 统计的三种类型
status = {}
# 请在此添加代码,实现编程要求
#********** Begin *********#

for data in result[1:]:
    data[0]=data[0].replace('-','0')
    data[1]=data[1].replace('-','0')
    data[2]=data[2].replace('-','0')
    data[3]=data[3].replace('-','0')
    status[data[0]]={types[0]:int(data[1]),types[1]:int(data[2]),types[2]:int(data[3])}

#********** End **********#
print(status)
infile.close()


# 读取各省的省会经纬度
Latitude_longitude_file = input()  # '各省份的省会经纬度.csv'
infile = open('src/step2/'+Latitude_longitude_file, 'r', encoding="utf-8")
data = get_data('src/step2/'+Latitude_longitude_file, 'r')
prov_loc = {}
# 请在此添加代码,实现编程要求
#********** Begin *********#

for i in data:

    prov_loc[i[0]]=[float(i[1]),float(i[2])]


#********** End **********#
print(prov_loc)
infile.close()
    

第3关:疫情数据可视化:堆积柱状图绘制

#-*- coding : utf-8 -*-
# coding: utf-8
import matplotlib
matplotlib.use('Agg')
import matplotlib.pyplot as plt
import csv

def get_data(file_name, read_type='r'):
    data = []
    with open(file_name,read_type,encoding='gb2312') as csvfile:
        for datum in csv.reader(csvfile):
            data.append(datum)
    return data


def draw_picture(data):
    plt.rcParams['font.sans-serif']=['SimHei']
    # 请在此添加代码,实现编程要求
    #********** Begin *********#
    plt.figure(figsize=(8, 4)) # 图片大小为8inch*4inch
    total_confirm = [int(x[1]) for x in data]
    total_heal = [int(x[2]) for x in data]
    index = [x for x in range(total_confirm.__len__())] # x 轴序列数据
    bar_width = 0.35 #条形宽度
    p1 = plt.bar(index, total_confirm, bar_width, alpha=0.7, color='#3896ff')
    p2 = plt.bar(index, total_heal, bar_width, alpha=0.7, color='#7aff51')
    x_labels = [x[0] for x in data]
    plt.xticks(index, x_labels) #横轴标签
    plt.legend((p1[0], p2[0]), ('总确诊', '治愈'))
    plt.ylim(0,3500)

    #********** End **********#
    plt.savefig('src/step3/picture/result.png')
    
    
data = get_data('src/step3/2月27日湖北部分地区数据.csv', 'r')
data = data[1:] # 首行为题头,无效数据。
draw_picture(data)

第4关:2020年2月5日疫情数据分析

#-*- coding : utf-8 -*-
# coding: utf-8
import matplotlib
matplotlib.use('Agg')
import matplotlib.pyplot as plt

def rgb2hex(rgbcolor):
    r, g, b = rgbcolor
    return hex((r * 2**16) + (g * 2**8) + b)
    
def shortname(s):
    if s[:2] == '广西':   # 广西壮族自治区
        s = '广西'
    if s[:3] == '内蒙古': # 内蒙古自治区
        s = '内蒙古'
    if s[:2] == '宁夏':   # 宁夏回族自治区
        s = '宁夏'
    if s[:2] == '新疆':   # 新疆维吾尔自治区
        s = '新疆'
    if s[:2] == '西藏':   # 西藏自治区
        s = '西藏'
    if s[:3] == '黑龍江':
        s = '黑龙江'
    return s
 
import numpy as np
from pylab import mpl
mpl.rcParams['font.sans-serif']= ['SimHei']
from mpl_toolkits.basemap import Basemap

# 请在此添加代码,实现编程要求
# -------------------begin------------------------
# 1、读取各省份疫情数据
infile = open('src/step4/2月5日上午9点疫情数据.csv', 'r',encoding='gbk')
data = infile.readlines()
types = data[0].strip().split(',')[1:4]    # 统计的三种类型
status = {}
for line in data[1:]:    # 第一行(标题行)不作为数据处理
    line = line.strip().split(',')  # 先去掉换行符,再切分
    number = {}
    for i in range(3):  # 统计3种类型的人数
        number[types[i]] = int(line[i+1]) if line[i+1] != '-' else 0
    status[line[0]] = number
infile.close()

# 2、读取各省的省会经纬度
prov_loc = {}
infile = open('src/step4/各省份的省会经纬度.csv', 'r',encoding='gbk')
data = infile.readlines()
for line in data:
    line = line.strip().split(',')
    prov_loc[line[0]] = [float(line[1]), float(line[2])]
infile.close()

fig= plt.figure(figsize = [10, 8])
# 1、读取各省份疫情数据

# 2、读取各省的省会经纬度

# -------------------end------------------------
fig= plt.figure(figsize = [10, 8])

# 3、创建地图、绘制地图
# 中国的经纬度范围是东经135度2分30秒-东经73度40分,北纬3度52分-北纬53度33分
m = Basemap(projection='mill', llcrnrlon=70, llcrnrlat=3, urcrnrlon=136, urcrnrlat=60)
m.drawcoastlines(linewidth=0.1)  #绘制海岸线
m.drawcountries(linewidth=0.1)   #绘制国界线
CHN = 'src/step4/中国地图-行政边界/china'
m.readshapefile(CHN+'/gadm36_CHN_1','states',drawbounds=True)
m.readshapefile(CHN+'/gadm36_TWN_1','taiwan',drawbounds=True)

colors = [(255, 220-220*i//5, 220-220*i//5) for i in range(5)]
ax = plt.gca()   # gca: Get Current Axes

# 4、着色打点标记
for info, seg in zip(m.states_info, m.states):
    statename = info['NL_NAME_1'] 
    statename = statename.split('|')
    if len(statename) > 1:
        prov = statename[1]
    else:
        prov = statename[0]
    prov = shortname(prov)   # 用短的省名,比如“广西壮族自治区”转为“广西”
    color = colors[len(str(status[prov][types[0]]))-1]
    # print(prov,color)
    color = '#' + str(rgb2hex(color))[2:]
    # print(color)
    poly = plt.Polygon(seg, facecolor=color, edgecolor=color)
    ax.add_patch(poly)
    long, lat = prov_loc[prov]
    x, y = m(long, lat)    
    m.plot(x, y, 'b.')                 # 在省份打点
    xshift = -50000
    yshift = 60000
    plt.text(x+xshift, y+yshift, prov, fontsize=8)   # 标明省份名称
   
for seg in m.taiwan:
    prov = '台湾'
    color = colors[len(str(status[prov][types[0]]))-1]
    color = '#' + str(rgb2hex(color))[2:]
    poly = plt.Polygon(seg, facecolor=color, edgecolor=color)
    ax.add_patch(poly)

    long, lat = prov_loc[prov]
    x, y = m(long, lat)    
    m.plot(x, y, 'b.')                 # 在省份打点
    xshift = -50000
    yshift = 60000
    plt.text(x+xshift, y+yshift, prov, fontsize=8)   # 标明省份名称

plt.savefig('src/step4/picture/result.png')

小技巧,这个后台没写好,能看到答案/doge

  • 3
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值