1 安装第三方库pyecharts(使用清华镜像)
1 2 | #!pip install pyecharts !pip install -i https://pypi.tuna.tsinghua.edu.cn/simple pyecharts |
1 2 3 4 5 6 | Looking in indexes: https://pypi.tuna.tsinghua.edu.cn/simple Requirement already satisfied: pyecharts in d:\soft\anaconda\envs\data_analysis\lib\site-packages (1.7.1) Requirement already satisfied: simplejson in d:\soft\anaconda\envs\data_analysis\lib\site-packages (from pyecharts) (3.17.0) Requirement already satisfied: prettytable in d:\soft\anaconda\envs\data_analysis\lib\site-packages (from pyecharts) (0.7.2) Requirement already satisfied: jinja2 in d:\soft\anaconda\envs\data_analysis\lib\site-packages (from pyecharts) (2.11.1) Requirement already satisfied: MarkupSafe>=0.23 in d:\soft\anaconda\envs\data_analysis\lib\site-packages (from jinja2->pyecharts) (1.1.1) |
2 pyecharts使用说明
- echarts是百度开源的数据可视化工具,凭借着良好的交互性,精巧的图表设计,得到了众多开发者的认可。Python很适合用于数据处理。当数据分析遇上数据可视化时,Pyecharts诞生了。
- pyecharts的核心是一切都是配置
- pyecharts api可以参考:https://pyecharts.org/#/zh-cn/chart_api
2.1 pyecharts绘图的步骤
其中:
- 系列配置项 set_series_opts(),可配置图元样式、文字样式、标签样式、点线样式等;
- 全局配置项 set_global_opts(),可配置标题、动画、坐标轴、图例等;
2.2 示例
1 2 3 4 5 6 7 8 9 | from pyecharts.charts import Bar #柱状图构造方法 from pyecharts import options as opts bar = Bar() #创建图形图像 bar.add_xaxis(["衬衫", "毛衣", "领带", "裤子", "风衣", "高跟鞋", "袜子"]) #添加数据 bar.add_yaxis("商家A", [114, 55, 27, 101, 125, 27, 105]) bar.add_yaxis("商家B", [57, 134, 137, 129, 145, 60, 49]) bar.set_series_opts(label_opts=opts.LabelOpts(font_size=12),is_show=True) #配置系列参数 bar.set_global_opts(title_opts=opts.TitleOpts(title="某商场销售情况")) #配置全局参数 bar.render_notebook() #渲染图片 |
3 绘制全国疫情地图
3.1 导入包
1 2 3 4 | import json import datetime from pyecharts.charts import Map from pyecharts import options as opts |
3.2 读取数据
1 2 3 4 5 | # 读原始数据文件 today = datetime.date.today().strftime('%Y%m%d') #20200315 datafile = 'data/'+ today + '.json' with open(datafile, 'r', encoding='UTF-8') as file: json_array = json.loads(file.read()) |
1 | type(json_array) #这是一个列表 |
1 | list |
1 | json_array[0] #查看第0个元素,为湖北省全部数据 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | {'provinceName': '湖北省', 'provinceShortName': '湖北', 'currentConfirmedCount': 1283, 'confirmedCount': 67802, 'suspectedCount': 0, 'curedCount': 63326, 'deadCount': 3193, 'comment': '', 'locationId': 420000, 'statisticsData': 'https://file1.dxycdn.com/2020/0223/618/3398299751673487511-135.json', 'cities': [{'cityName': '武汉', 'currentConfirmedCount': 1279, 'confirmedCount': 50007, 'suspectedCount': 0, 'curedCount': 46175, 'deadCount': 2553, 'locationId': 420100}, |
3.3 分析全国实时确诊数据:'confirmedCount’字段
- json_array[0]是一个字典,确诊数据在’confirmedCount’字段中
- 使用for循环,访问json_array中的每个元素对应的’provinceShortName’字段,'confirmedCount’字段
1 2 3 | china_data = [] for province in json_array: china_data.append((province['provinceShortName'], province['confirmedCount'])) |
1 | china_data[:5] #提取前5个元素,每个元素是一个元组 |
1 | [('湖北', 67802), ('香港', 765), ('台湾', 329), ('上海', 522), ('北京', 582)] |
1 | china_data = sorted(china_data, key=lambda x: x[1], reverse=True) #根据确诊人数排序,reverse=True表示降序 |
1 | china_data[:5] |
1 | [('湖北', 67802), ('广东', 1507), ('河南', 1276), ('浙江', 1258), ('湖南', 1019)] |
3.4 全国疫情地图
3.4.1 自定义图表颜色对应的值
1 2 3 4 5 6 7 8 9 | pieces = [ {'min': 10000, 'color': '#540d0d'}, {'max': 9999, 'min': 1000, 'color': '#9c1414'}, {'max': 999, 'min': 500, 'color': '#d92727'}, {'max': 499, 'min': 100, 'color': '#ed3232'}, {'max': 99, 'min': 10, 'color': '#f27777'}, {'max': 9, 'min': 1, 'color': '#f7adad'}, {'max': 0, 'color': '#f7e4e4'}, ] |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | #labels = [data[0] for data in china_data] #counts = [data[1] for data in china_data] m = Map() #创建图形图像 #m.add("累计确诊", [list(z) for z in zip(labels, counts)], 'china') m.add("累计确诊", [list(z) for z in china_data], 'china') #系列配置项,可配置图元样式、文字样式、标签样式、点线样式等 m.set_series_opts(label_opts=opts.LabelOpts(font_size=12), is_show=False) #全局配置项,可配置标题、动画、坐标轴、图例等 m.set_global_opts(title_opts=opts.TitleOpts(title='全国实时确诊数据', subtitle='数据来源:丁香园'), legend_opts=opts.LegendOpts(is_show=False), visualmap_opts=opts.VisualMapOpts(pieces=pieces, is_piecewise=True, #是否为分段型 is_show=True)) #是否显示视觉映射配置 |
1 | <pyecharts.charts.basic_charts.map.Map at 0x23727ab1608> |
1 | m.render_notebook() #渲染图片 |
3.4.2 保存图形
1 2 | #render()会生成本地 HTML 文件,默认会在当前目录生成 render.html 文件,也可以传入路径参数,如 m.render("mycharts.html") m.render(path='data/全国实时确诊数据.html') |
4 绘制湖北省地图
4.1 下载城市对应数据集
- https://aistudio.baidu.com/aistudio/datasetlist/2
4.2 读入规范化的城市名称
1 2 | with open('data/pycharts_city.txt', 'r', encoding='UTF-8') as f: defined_cities = [line.strip() for line in f.readlines()] |
1 | type(defined_cities) |
1 | list |
1 | defined_cities[0] |
1 | '广东' |
1 | set('广东')&set('广东省') #将字符串转换为集合,提取相同的字符 |
1 | {'东', '广'} |
1 2 3 4 5 6 7 8 | def format_city_name(name, defined_cities): for defined_city in defined_cities: if len((set(defined_city) & set(name))) == len(name): name = defined_city if name.endswith('市') or name.endswith('区') or name.endswith('县') or name.endswith('自治州'): return name return name + '市' return None |
4.3 读取数据
1 2 3 4 5 6 7 | province_name = '湖北' for province in json_array: if province['provinceName'] == province_name or province['provinceShortName'] == province_name: json_array_province = province['cities'] hubei_data = [(format_city_name(city['cityName'], defined_cities), city['confirmedCount']) for city in json_array_province] hubei_data = sorted(hubei_data, key=lambda x: x[1], reverse=True) |
1 | hubei_data[:5] |
1 | [('武汉市', 50007), ('孝感市', 3518), ('黄冈市', 2907), ('荆州市', 1580), ('鄂州市', 1394)] |
4.4 pycharts绘图
1 2 3 4 5 6 7 8 9 | pieces = [ {'min': 10000, 'color': '#540d0d'}, {'max': 9999, 'min': 1000, 'color': '#9c1414'}, {'max': 999, 'min': 500, 'color': '#d92727'}, {'max': 499, 'min': 100, 'color': '#ed3232'}, {'max': 99, 'min': 10, 'color': '#f27777'}, {'max': 9, 'min': 1, 'color': '#f7adad'}, {'max': 0, 'color': '#f7e4e4'}, ] |
1 2 3 4 5 6 7 8 9 10 11 | m = Map() m.add("累计确诊", [list(z) for z in hubei_data], '湖北') m.set_series_opts(label_opts=opts.LabelOpts(font_size=12), is_show=False) m.set_global_opts(title_opts=opts.TitleOpts(title='湖北省实时确诊数据', subtitle='数据来源:丁香园'), legend_opts=opts.LegendOpts(is_show=False), visualmap_opts=opts.VisualMapOpts(pieces=pieces, is_piecewise=True, is_show=True)) m.render_notebook() #渲染图片 |
1 | m.render(path='data/湖北省实时确诊数据.html') |
1 | 'D:\\py_baidu\\day01\\data\\湖北省实时确诊数据.html' |