使用工具:
- Pycharm
- Python3.9
- Pyecharts
pyecharts官网
一、介绍
1.1、版本区分
- V0.5x版本
- app开发定制公司不再进行维护。仅支持python2.7、3.4+
- V1.0x版本
- 仅支持python3.6+
1.2、技术介绍
app开发定制公司是一个由百度开源的,app开发定制公司凭借着良好的交互性,app开发定制公司精巧的图表设计,app开发定制公司得到了众多开发者的认可。而 Python app开发定制公司是一门富有表达力的语言,很适合用于数据处理。当数据分析遇上数据可视化时, 诞生了。
二、疫情数据可视化项目
2.1、了解数据
了解一个数据要从不同的维度来了解
例如疫情的数据,可以从以下几个方面来了解:
- 维度
- 成品
2.2、绘制单日境外输入和本土新增的图
分析:有一个总的数据,然后两个加起来属于总的,所以对于这种比例的,推荐饼图
对于占比的数据,推荐使用饼图,因为能更好的看清楚各种比例
- 成品
- 【官方案例】Pie - Pie_base
#导包from pyecharts import options as optsfrom pyecharts.charts import Piefrom pyecharts.faker import Faker #Faker包里面有样例数据#链式调用c = ( #实例化一个饼图对象 Pie() #添加数据 .add("", [list(z) for z in zip(Faker.choose(), Faker.values())]) #设置标题 .set_global_opts(title_opts=opts.TitleOpts(title="Pie-基本示例")) #设置标签 .set_series_opts(label_opts=opts.LabelOpts(formatter="{b}: {c}")) #网页渲染方式 .render("pie_base.html"))#jupyter独有的#c.render_notebook()
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 修改官方代码实现功能:
#导包from pyecharts import options as optsfrom pyecharts.charts import Pie# from pyecharts.faker import Fakerdata = [['境外输入',10],['本土新增',9]]c = ( Pie() .add(series_name="",data_pair=data) .set_global_opts(title_opts=opts.TitleOpts(title="单日新增患者")) .set_series_opts(label_opts=opts.LabelOpts(formatter="{b}: {c}")) #设置颜色 .set_colors(['rgb(70, 133, 212)','rgb(186, 33, 16)']) .render("./templates/index2.html"))
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
2.3、绘制一周内数据的新增情况
分析:数据量适中,每条数据中有三个类目
采用极坐标系图
- 成品
- 【官网案例】Polar - Polar_angleaxis
from pyecharts import options as optsfrom pyecharts.charts import Polarfrom pyecharts.faker import Fakerc = ( #实例化极坐标系对象 Polar() #设置角轴每一部分的名称,还有类型 .add_schema(angleaxis_opts=opts.AngleAxisOpts(data=Faker.week, type_="category")) #增加数据,设置类型,支持散点、柱状、还有其他类型,stark相同可以堆叠反之,如果不一样,会单独成列 .add("A", [1, 2, 3, 4, 3, 5, 1], type_="bar", stack="stack0") .add("B", [2, 4, 6, 1, 2, 3, 1], type_="bar", stack="stack0") .add("C", [1, 2, 3, 4, 1, 2, 5], type_="bar", stack="stack0") #设置标题 .set_global_opts(title_opts=opts.TitleOpts(title="Polar-AngleAxis")) #渲染方式 .render("polar_angleaxis.html"))
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 修改官方代码实现功能:
from pyecharts import options as optsfrom pyecharts.charts import Polarfrom pyecharts.faker import Fakerdate = ['12月25日', '12月26日', '12月27日', '12月28日', '12月29日', '12月30日', '12月31日']c = ( Polar() .add_schema(angleaxis_opts=opts.AngleAxisOpts(data=date, type_="category")) .add("境外输入", [12, 10, 15, 12, 17, 16, 10], type_="bar", stack="stack0") .add("新增本土", [8, 12, 6, 15, 7, 9, 9], type_="bar", stack="stack0") .add("新增无症状", [19, 15, 20, 8, 17, 8, 9], type_="bar", stack="stack0") .set_global_opts(title_opts=opts.TitleOpts(title="本周新增患者情况")) .render("./templates/index3.html"))
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
2.4、绘制本月新增病例情况
分析:数据量多,类目单一,期待这个数据的变化趋势
所以推荐折线图,能看出增长变化的趋势
- 成品
- 【官方案例】Line - Line_base
import pyecharts.options as optsfrom pyecharts.charts import Linefrom pyecharts.faker import Fakerc = ( Line() #设置x轴 .add_xaxis(Faker.choose()) #设置数据的名称以及数据 .add_yaxis("商家A", Faker.values()) .add_yaxis("商家B", Faker.values()) #设置标题 .set_global_opts(title_opts=opts.TitleOpts(title="Line-基本示例")) #渲染网页方式 .render("line_base.html"))
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 修改代码:
import pyecharts.options as optsfrom pyecharts.charts import Line# from pyecharts.faker import Fakerdata = [9,16,17,17,18,15,12,15,12,15,13,24,16,17,12,7,12,17,23,23,15,15,17,14,20,22,21,27,24,25,19]c = ( Line() #设置x轴 .add_xaxis([i for i in range(1,32)]) #设置数据的名称以及数据 .add_yaxis("新增患者人数", data) .set_global_opts(title_opts=opts.TitleOpts(title="12月新增患者")) .render("./templates/index4.html"))# print([i for i in range(1,32)])
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
2.5、绘制2020年各月新增人数
分析:数据量适中,类名单一
采用带时间轴的柱状图
- 成品
- 【官方案例】
from pyecharts import options as optsfrom pyecharts.charts import Barfrom pyecharts.faker import Fakerc = ( Bar() #设置x轴 .add_xaxis(Faker.days_attrs) #设置数据标题,y轴数据 .add_yaxis("商家A", Faker.days_values) #设置标题和时间轴 .set_global_opts( title_opts=opts.TitleOpts(title="Bar-DataZoom(slider-水平)"), datazoom_opts=opts.DataZoomOpts(), ) .render("bar_datazoom_slider.html"))
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 修改代码
from pyecharts import options as optsfrom pyecharts.charts import Bar# from pyecharts.faker import Fakerdata = [11719,68033,1730,1320,143,517,803,721,356,583,545,529]c = ( Bar() # 设置x轴 .add_xaxis([str(i)+"月" for i in range(1,13)]) # 设置数据标题,y轴数据 .add_yaxis("新增人数", data) # 设置标题和时间轴 .set_global_opts( title_opts=opts.TitleOpts(title="2020年各月新增数据"), datazoom_opts=opts.DataZoomOpts(), ) .render("./templates/index5.html"))# print([str(i)+"月" for i in range(1,13)])
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
三、整合图表
3.1、步骤
- 创建一个Page对象,并运用
DraggablePageLayout
布局 add()
把各个图添加进来render()
渲染成网页- 调整网页布局,按
Save Config
保存配置 - 调用重新渲染方法
save_resize_html('下载的网页',cfg_file='json文件',dest='新文件路径')
,并把下载的文件导入进去,再确定新的存放位置
3.2、代码
- 成品
自己用手拖吧~~ :)
- 【官网案例】Page DraggablePageLayout 布局
#实例化Page对象page = Page(layout=Page.DraggablePageLayout)#添加图表page.add(bar_datazoom_slider(), line_markpoint(), pie_rosetype(), grid_mutil_yaxis())#保存渲染page.render()
- 1
- 2
- 3
- 4
- 5
- 6
- 修改代码
from pyecharts.charts import Page#01_单日疫情增长.py#导包from pyecharts import options as optsfrom pyecharts.charts import Piedata = [['境外输入',10],['本土新增',9]]pie = ( Pie() .add(series_name="",data_pair=data) .set_global_opts(title_opts=opts.TitleOpts(title="单日新增患者")) .set_series_opts(label_opts=opts.LabelOpts(formatter="{b}: {c}")) #设置颜色 .set_colors(['rgb(70, 133, 212)','rgb(186, 33, 16)']))#02_周增长人数.pyfrom pyecharts import options as optsfrom pyecharts.charts import Polardate = ['12月25日', '12月26日', '12月27日', '12月28日', '12月29日', '12月30日', '12月31日']polar = ( Polar() .add_schema(angleaxis_opts=opts.AngleAxisOpts(data=date, type_="category")) .add("境外输入", [12, 10, 15, 12, 17, 16, 10], type_="bar", stack="stack0") .add("新增本土", [8, 12, 6, 15, 7, 9, 9], type_="bar", stack="stack0") .add("新增无症状", [19, 15, 20, 8, 17, 8, 9], type_="bar", stack="stack0") .set_global_opts(title_opts=opts.TitleOpts(title="本周新增患者情况")))#03_每月疫情新增人数.pyimport pyecharts.options as optsfrom pyecharts.charts import Linedata = [9,16,17,17,18,15,12,15,12,15,13,24,16,17,12,7,12,17,23,23,15,15,17,14,20,22,21,27,24,25,19]line = ( Line() #设置x轴 .add_xaxis([i for i in range(1,32)]) #设置数据的名称以及数据 .add_yaxis("新增患者人数", data) .set_global_opts(title_opts=opts.TitleOpts(title="12月新增患者")))#04_2020整年新增人数.pyfrom pyecharts import options as optsfrom pyecharts.charts import Bardata = [11719,68033,1730,1320,143,517,803,721,356,583,545,529]bar = ( Bar() # 设置x轴 .add_xaxis([str(i)+"月" for i in range(1,13)]) # 设置数据标题,y轴数据 .add_yaxis("新增人数", data) # 设置标题和时间轴 .set_global_opts( title_opts=opts.TitleOpts(title="2020年各月新增数据"), datazoom_opts=opts.DataZoomOpts(), ))#实例化Page对象page = Page(layout=Page.DraggablePageLayout)#添加图表page.add(pie,polar,line,bar)#保存图表page.render("./end.html")page.save_resize_html("end.html",cfg_file="./chart_config.json",dest="./output.html")
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78