Article directory
1.json data format
It is essentially a string (str) with a specific format, responsible for data transfer and interaction in different programming languages
The json format is a dictionary, or a list of dictionaries
[{
"name":"jack","age":18},{
"name":"peter","age":17}]
{
"name":"haha"}
For example, we can convert the dictionary in python into json (string) that c language can understand
(1) python to json (dumps)
import json
a=[{
"name":"jack","age":18},{
"name":"peter","age":17}] # 以字典组成的列表为例
a=json.dumps(a) # python 转 json
print(type(a))
print(a)
"""
<class 'str'>
# [{"name": "jack", "age": 18}, {"name": "peter", "age": 17}]
"""
If there is Chinese in the dictionary, you can use ensure_ascii=False
import json
a={
"name":"张三","age":18}
a=json.dumps(a,ensure_ascii=False)
print(type(a))
print(a)
"""
<class 'str'>
{"name": "张三", "age": 18}
"""
(2) json to python (loads)
import json
b='{"name":"haha"}' # 字符串必须带引号,为了防止和内部的双引号冲突,外侧使用单引号
b=json.loads(b) # json 转 python
print(type(b))
print(b)
"""
<class 'dict'>
{'name': 'haha'}
"""
2.pyecharts
First download the pyecharts package
(1) Basic
main.py
from pyecharts.charts import Line
line=Line() # 得到折线图对象
line.add_xaxis(["中国","美国","英国"]) # x轴
line.add_yaxis("GDP",[30,20,10]) # y轴
line.render() # 生成图表
open image mode
(2) Set the global configuration item
① Title
from pyecharts.options import TitleOpts
pos_left="center" The distance from the title to the left: Center
pos_bottom="1%" The title is 1% from the bottom
from pyecharts.options import TitleOpts # 导入标题相关
line.set_global_opts(
title_opts=TitleOpts(title="标题名称",pos_left="center",pos_bottom="1%") # 设置标题
)
② legend
from pyecharts.options import LegendOpts
is_show=False no legend
is_show=True has legend (default)
from pyecharts.options import LegendOpts # 导入图例相关
line.set_global_opts(
legend_opts=LegendOpts(is_show=True) # 设置图例
)
③Toolbox
from pyecharts.options import ToolboxOpts
is_show=True with toolbox
is_show=False without toolbox (default)
from pyecharts.options import TitleOpts,LegendOpts,ToolboxOpts # 导入标题相关
line.set_global_opts(
toolbox_opts=ToolboxOpts(is_show=True)
)
④Visual mapping
from pyecharts.options import VisualMapOpts
is_show=True Yes
is_show=False No (default)
from pyecharts.options import VisualMapOpts
line.set_global_opts(
visualmap_opts=VisualMapOpts(is_show=True)
)
overall
from pyecharts.charts import Line
line=Line() # 得到折线图对象
line.add_xaxis(["中国","美国","英国"]) # x轴
line.add_yaxis("GDP",[30,20,10]) # y轴
from pyecharts.options import TitleOpts,LegendOpts,ToolboxOpts,VisualMapOpts # 导入
line.set_global_opts(
title_opts=TitleOpts(title="标题名称",pos_left="center",pos_bottom="1%"), # 标题
legend_opts=LegendOpts(is_show=True), # 图例
toolbox_opts=ToolboxOpts(is_show=True), # 工具箱
visualmap_opts=VisualMapOpts(is_show=True) # 视觉映射
)
line.render() # 生成图表
For other global configuration items, see pyecharts official website
(3) pyecharts module
pyecharts-gallery official website
Copy any piece of code and run
Open the automatically generated html file with a browser
3. Data processing and image rendering
open and read
f_us=open("D:/折线图数据/美国.txt","r",encoding="UTF-8")
us_data=f_us.read()
The given txt file is json type
When programming, it is necessary to remove the non-standard data at the beginning and end (unselected part)
us_data=us_data.replace("jsonp_1629344292311_69436(","") # 开头部分替换为空
us_data=us_data[:-2] # 以切片形式删除最后两个字符
(1) Data viewing
Online Lazy Tool
Select JSON View
copy valid code in txt
(2) Data extraction
Obtain the No. 0 element of the data list, which has two keys (name and trend), and trend contains updateDate and list
import json
us_dict=json.loads(us_data) # json转python字典
trend_data=us_dict["data"][0]["trend"]
① The x-axis data
is taken out from updateDate,
read the date data, and take the date data of each day in the first year as the x-axis
x_data=trend_data["updateDate"][:314]
②Y-axis data
Select list-0-data as the y-axis data, the data corresponds to the x-axis one-to-one, and you can get 314 (excluding)
y_data=trend_data["list"][0]["data"][:314]
full code
f_us=open("D:/折线图数据/美国.txt","r",encoding="UTF-8")
us_data=f_us.read()
us_data=us_data.replace("jsonp_1629344292311_69436(","") # 开头部分替换为空
us_data=us_data[:-2] # 以切片形式删除最后两个字符
import json
us_dict=json.loads(us_data) # json转python字典
trend_data=us_dict["data"][0]["trend"]
x_data=trend_data["updateDate"][:314]
y_data=trend_data["list"][0]["data"][:314]
Similarly, to handle other country data, complete code
f_us=open("D:/折线图数据/美国.txt","r",encoding="UTF-8")
f_jp=open("D:/折线图数据/日本.txt","r",encoding="UTF-8")
f_in=open("D:/折线图数据/印度.txt","r",encoding="UTF-8")
us_data=f_us.read()
jp_data=f_jp.read()
in_data=f_in.read()
us_data=us_data.replace("jsonp_1629344292311_69436(","") # 开头部分替换为空
jp_data=jp_data.replace("jsonp_1629350871167_29498(","") # 开头部分替换为空
in_data=in_data.replace("jsonp_1629350745930_63180(","") # 开头部分替换为空
us_data=us_data[:-2] # 以切片形式删除最后两个字符
jp_data=jp_data[:-2] # 以切片形式删除最后两个字符
in_data=in_data[:-2] # 以切片形式删除最后两个字符
import json
us_dict=json.loads(us_data) # json转python字典
jp_dict=json.loads(jp_data) # json转python字典
in_dict=json.loads(in_data) # json转python字典
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]
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]
(3) Image drawing
from pyecharts.charts import Line
line=Line()
line.add_xaxis(us_x_data) # 横轴都是日期,任选一个即可
line.add_yaxis("美国确诊人数",us_y_data)
line.add_yaxis("日本确诊人数",jp_y_data)
line.add_yaxis("印度确诊人数",in_y_data)
line.render()
f_us.close()
f_jp.close()
f_in.close()
You can hide the numbers that appear in the image, use label_opts=LabelOpts(is_show=False to process the y coordinates
from pyecharts.options import LabelOpts
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))
Set chart title, add toolbox and visual map
line.set_global_opts(
title_opts=TitleOpts(title="2020年美日印三国确诊人数对比折线图",pos_left="center",pos_bottom="1%"), # 标题
toolbox_opts=ToolboxOpts(is_show=True),
visualmap_opts=VisualMapOpts(is_show=True,pos_right="1%")
)
full code
from pyecharts.options import TitleOpts,ToolboxOpts,VisualMapOpts,LabelOpts
f_us=open("D:/折线图数据/美国.txt","r",encoding="UTF-8")
f_jp=open("D:/折线图数据/日本.txt","r",encoding="UTF-8")
f_in=open("D:/折线图数据/印度.txt","r",encoding="UTF-8")
us_data=f_us.read()
jp_data=f_jp.read()
in_data=f_in.read()
us_data=us_data.replace("jsonp_1629344292311_69436(","") # 开头部分替换为空
jp_data=jp_data.replace("jsonp_1629350871167_29498(","") # 开头部分替换为空
in_data=in_data.replace("jsonp_1629350745930_63180(","") # 开头部分替换为空
us_data=us_data[:-2] # 以切片形式删除最后两个字符
jp_data=jp_data[:-2] # 以切片形式删除最后两个字符
in_data=in_data[:-2] # 以切片形式删除最后两个字符
import json
us_dict=json.loads(us_data) # json转python字典
jp_dict=json.loads(jp_data) # json转python字典
in_dict=json.loads(in_data) # json转python字典
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]
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]
from pyecharts.charts import Line
line=Line()
line.add_xaxis(us_x_data) # 横轴都是日期,任选一个即可
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%"), # 标题
toolbox_opts=ToolboxOpts(is_show=True),
visualmap_opts=VisualMapOpts(is_show=True,pos_right="1%")
)
line.render()
f_us.close()
f_jp.close()
f_in.close()