视频教程:黑马程序员P99-110
Json数据格式(前置知识)
什么是Json:
Json是一种在各个编程语言流通的数据格式,负责不同编程语言的数据传递和交互
Json的本质是带有特定格式的字符串
如图
Python数据和Json数据的相互转换
列表转换为json数据
ensure_ascii=False 参数,能够把中文正确显示出来
import json
data=[{"name":"Sassybox","age":3},{"name":"Box","age":4}]
json_str=json.dumps(data,ensure_ascii=False)
print(type(json_str))
print(json_str)
输出结果:类型为字符串
字典转换为json数据
dict={"name":"Box","addr":"China"}
json_str=json.dumps(dict,ensure_ascii=False)
print(type(json_str))
输出结果:类型为字符串
Json数据转换为列表
s='[{"name": "Sassybox", "age": 3}, {"name": "Box", "age": 4}]'
list=json.loads(s)
print(type(list))
print(list)
输出结果:类型为列表
Json数据转换为字典
s='{"name": "Sassybox", "age": 3}'
dict=json.loads(s)
print(type(dict))
print(dict)
输出结果:类型为字典
Pyecharts模块的使用
Pyecharts官网
简介 - pyecharts - A Python Echarts Plotting Library built with love.
Pyecharts画廊功能:
中文简介 - Document (pyecharts.org)
安装Pyecharts包
pip install pyecharts
如果以前安装过Pyecharts包,检查其是否存在
pip show pyecharts
Pyecharts模块的入门使用
先导包-再使用其相关功能
Pyecharts的配置选项
全局配置选项
针对整个图像:图像的标题,图例,工具箱等
set_global_opts()方法
系列配置选项
例如针对轴数据,如y轴个性化的配置
#导包,导入Line功能构建折线图对象
from pyecharts.charts import Line
from pyecharts.options import TitleOpts,LegendOpts,ToolboxOpts,VisualMapOpts
#得到折线图对象
line=Line()
#添加x轴数据
line.add_xaxis(["中国","美国","英国"])
line.add_yaxis("GDP",[30,20,10])
#生成图表
'''
设置全局配置项
'''
line.set_global_opts(
title_opts= TitleOpts(title="GDP展示",pos_left="center",pos_bottom="1%"),#标题
legend_opts= LegendOpts(is_show=True),#图例
toolbox_opts=ToolboxOpts(is_show=True),#工具箱
visualmap_opts=VisualMapOpts(is_show=True)#视觉映射
)
#render方法将代码生成为图象
line.render()
运行代码,项目目录中生成render.html文件,run and debug,则可在浏览器中看到效果图
数据准备
JSON转换网站
JSON着色工具-json数据着色-懒人工具|www.ab173.com
在嵌套结构中取出数据
import json
from pyecharts.charts import Line
from pyecharts.options import TitleOpts,LabelOpts
#处理数据
f_us=open("E:\\Python_self_learning\\Python_learning\\美国.txt","r",encoding="UTF-8")
us_data=f_us.read()
f_jp=open("E:\\Python_self_learning\\Python_learning\\日本.txt","r",encoding="UTF-8")
jp_data=f_jp.read()
f_in=open("E:\\Python_self_learning\\Python_learning\\印度.txt","r",encoding="UTF-8")
in_data=f_in.read()
#删除不符合Json规范的开头
us_data=us_data.replace("jsonp_1629344292311_69436("," ")
jp_data=jp_data.replace("jsonp_1629350871167_29498("," ")
in_data=in_data.replace("jsonp_1629350745930_63180("," ")
#删除不符合Json规范的结尾
us_data=us_data[:-2] #不包含-2
jp_data=jp_data[:-2]
in_data=in_data[:-2]
#json转成Python字典
us_dict=json.loads(us_data)
jp_dict=json.loads(jp_data)
in_dict=json.loads(in_data)
#获取trend key
us_trend_data=us_dict['data'][0]['trend']
jp_trend_data=jp_dict['data'][0]['trend']
in_trend_data=in_dict['data'][0]['trend']
#获取日期数据
us_x_data=us_trend_data['updateDate'][:314]
jp_x_data=jp_trend_data['updateDate'][:314]
in_x_data=in_trend_data['updateDate'][:314]
#获取确诊数据用于y轴
us_y_data=us_trend_data['list'][0]['data'][:314]
jp_y_data=jp_trend_data['list'][0]['data'][:314]
in_y_data=in_trend_data['list'][0]['data'][:314]
#生成图表
line=Line() #构建折线图对象
#添加x轴数据
line.add_xaxis(us_x_data)
#添加y轴数据
line.add_yaxis("美国确诊人数",us_y_data,label_opts=LabelOpts(is_show=False))
line.add_yaxis("日本确诊人数",jp_y_data,label_opts=LabelOpts(is_show=False))
line.add_yaxis("印度确诊人数",in_y_data,label_opts=LabelOpts(is_show=False))
#全局选项
line.set_global_opts(
#标题设置
title_opts=TitleOpts(title="2020年美日印三国确诊人数对比图",pos_left="center",pos_bottom="1%")
)
#生成图表
line.render()
#关闭文件对象
f_us.close()
f_jp.close()
f_in.close()
地图可视化
基础地图演示
'''
演示地图可视化的基本使用
'''
from pyecharts.charts import Map
from pyecharts.options import VisualMapOpts,LabelOpts
#准备地图对象
map=Map()
#准备数据
data= [
("北京",99),
("上海",199),
("湖南",299),
("台湾",399),
("广东", 499 )
]
#添加数据
map.add("测试地图",data,"china")
#设置全局选项
map.set_global_opts(
visualmap_opts=VisualMapOpts(
is_show=True,
is_piecewise=True, #允许手动校准
pieces=[
{"min":1,"max":9,"label":"1-9","color":"#CCFFFF"},
{"min":10,"max":99,"label":"10-99","color":"#FF6666"},
{"min":100,"max":500,"label":"100-500","color":"#990033"},
]
)
)
#绘图
map.render()
国内疫情地图
import json
from pyecharts.charts import Map
from pyecharts.options import*
#读取数据
f=open("E:\\Python_self_learning\\Python_learning\\疫情.txt","r",encoding="UTF-8")
data=f.read()
#关闭文件
f.close()
#将字符串json转为python字典
data_dict=json.loads(data)
#从字典中取出省份的数据
province_data_list=data_dict["areaTree"][0]["children"]
#组装每个省份和确诊人数为元组,并把各个省份的数据封装入列表
data_list=[] #绘图需要用到的数据列表
for province_data in province_data_list:
province_name=province_data["name"]
province_confirm=province_data["total"]["confirm"]
data_list.append((province_name,province_confirm))
#创建地图对象
map=Map()
#添加数据
map.add("各省份确诊人数",data_list,"china")
#设置全局配置,定制分段的视觉映射
map.set_global_opts(
title_opts=TitleOpts(title="全国疫情地图"),
visualmap_opts=VisualMapOpts(
is_show=True , #是否显示
is_piecewise=True, #是否分段
pieces=[
{"min":1,"max":99,"label":"1-99人","color":"#CCFFFF"},
{"min":100,"max":999,"label":"100-999人","color":"#FFFF99"},
{"min":1000,"max":4999,"label":"1000-4999人","color":"#FF9966"},
{"min":5000,"max":9999,"label":"5000-9999人","color":"#FF6666"},
{"min":10000,"max":99999,"label":"10000-99999人","color":"#CC3333"},
{"min":100000,"lable":"100000+","color":"#990033"}
]
)
)
map.render("全国疫情地图.html")
遇到的报错信息和解决方案
解决方案:应该再加上一个括号
省级疫情地图
基础柱状图:
'''
基础柱状图
'''
from pyecharts.charts import Bar
bar=Bar()
bar.add_xaxis(["中国","美国","英国"])
bar.add_yaxis("GDP",[30,20,10])
bar.render("基础柱状图.html")
翻转x,y轴,添加代码
#翻转x轴和y轴
bar.reversal_axis()
把数字移动到柱状图的最右侧,添加两行代码
bar.add_yaxis("GDP",[30,20,10],label_opts=LabelOpts(position="right"))
增加参数
from pyecharts.options import LabelOpts #顶部导入
bar.add_yaxis("GDP",[30,20,10],label_opts=LabelOpts(position="right"))
'''
基础柱状图
'''
from pyecharts.charts import Bar
from pyecharts.options import LabelOpts
bar=Bar()
bar.add_xaxis(["中国","美国","英国"])
bar.add_yaxis("GDP",[30,20,10],label_opts=LabelOpts(position="right"))
#翻转x轴和y轴
bar.reversal_axis()
bar.render("基础柱状图.html")
基础柱状图创建时间线
创建时间线:
#构建时间线对象
timeline=Timeline(
{"theme":ThemeType.LIGHT}
)
自动播放设置:
#自动播放设置
timeline.add_schema(
play_interval=1000, #自动播放的时间间隔(毫秒)
is_timeline_show=True,#自动播放时显示时间线
is_auto_play=True, #是否自动播放
is_loop_play=True #是否循环播放
)
颜色
'''
带有时间线的柱状开发
'''
from pyecharts.charts import Bar,Timeline
from pyecharts.options import LabelOpts
from pyecharts.globals import ThemeType
bar1=Bar()
bar1.add_xaxis(["中国","美国","英国"])
bar1.add_yaxis("GDP",[30,30,20],label_opts=LabelOpts(position="right"))
bar1.reversal_axis()
bar2=Bar()
bar2.add_xaxis(["中国","美国","英国"])
bar2.add_yaxis("GDP",[50,50,50],label_opts=LabelOpts(position="right"))
bar2.reversal_axis()
bar3=Bar()
bar3.add_xaxis(["中国","美国","英国"])
bar3.add_yaxis("GDP",[70,60,60],label_opts=LabelOpts(position="right"))
bar3.reversal_axis()
#构建时间线对象
timeline=Timeline(
{"theme":ThemeType.LIGHT}
)
#在时间线内添加柱状图对象
timeline.add(bar1,"点1")
timeline.add(bar2,"点2")
timeline.add(bar3,"点3")
#绘图用时间线对象绘图,而不是bar对象
timeline.render("基础时间线柱状图.html ")
#自动播放设置
timeline.add_schema(
play_interval=1000, #自动播放的时间间隔(毫秒)
is_timeline_show=True,#自动播放时显示时间线
is_auto_play=True, #是否自动播放
is_loop_play=True #是否循环播放
)