line chart
map
Dynamic GDP Growth Chart
Supplementary knowledge:
json
1) JSON is a lightweight data interaction format. Data can be organized and encapsulated in the format specified by JSON
2) JSON is essentially a string with a specific format
Main functions: json is a data format that circulates in various programming languages, responsible for data transmission and interaction in different programming languages.
json format
The data requirements in json format are very strict, the following case:
Mutual conversion between Python data and Json data
pyecharts module
Overview: Echarts is a data visualization open sourced by Baidu. With its good interactivity and exquisite chart design, it has been recognized by many developers. Python is an expressive language, which is very suitable for data processing. When data When analysis meets data visualization, pyecharts was born.
pyecharts module installation
pip install pyecharts -i https://pypi.tuna.tsinghua.edu.cn/simple #清华源
Basic Line Chart
What are the configuration options for pyecharts
There are many configuration options in the pyecharts module, and two categories of options are commonly used: global configuration options series configuration options
set_global_opts method
The global configuration options here can be configured through the set_global_opts method, and the corresponding options and options are as follows:
Corresponding code block:
Modules need to be imported when operating on set_global_opts()
Line chart related configuration items
Create a line chart
.add_yaxis related configuration options:
from pyecharts.charts import Line
from pyecharts.options import TitleOpts, LegendOpts, ToolboxOpts, VisualMapOpts
line = Line()
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")
)
line.render()
Practice questions:
Draw a line chart of the epidemic transformation of the United States, Japan and India
Related information download: Link: https://pan.baidu.com/s/1FHOAivxPafjsRFYIeNQbsg?pwd=t2og
Extraction code: t2og
from pyecharts.charts import Line
from pyecharts.options import TitleOpts, ToolboxOpts, LabelOpts
import json
f_us = open("美国.txt", "r", encoding="UTF-8")
us_data = f_us.read()
f_jp = open("日本.txt", "r", encoding="UTF-8")
jp_data = f_jp.read()
f_in = open("印度.txt", "r", encoding="UTF-8")
in_data = f_in.read()
us_data = us_data.replace("jsonp_1629344292311_69436(", "")
us_data = us_data[:-2]
jp_data = jp_data.replace("jsonp_1629350871167_29498(", "")
jp_data = jp_data[:-2]
in_data = in_data.replace("jsonp_1629350745930_63180(", "")
in_data = in_data[:-2]
us_dict = json.loads(us_data)
jp_dict = json.loads(jp_data)
in_dict = json.loads(in_data)
us_trend_data = us_dict["data"][0]["trend"]
us_x_data = us_trend_data["updateDate"][:314]
us_y_data = us_trend_data['list'][0]['data'][:314]
jp_trend_data = jp_dict["data"][0]["trend"]
jp_x_data = jp_trend_data["updateDate"][:314]
jp_y_data = jp_trend_data['list'][0]['data'][:314]
in_trend_data = in_dict["data"][0]["trend"]
in_x_data = in_trend_data["updateDate"][:314]
in_y_data = in_trend_data['list'][0]['data'][:314]
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)
)
line.render()
f_us.close()
f_jp.close()
f_in.close()
Base Map Demo
Basemap Demo - Visual Mapper
Set global configuration options
Case number one:
from pyecharts.charts import Map
from pyecharts.options import VisualMapOpts
map = Map()
data = [
("北京", 99),
("上海", 199),
("湖南", 989),
("江西", 995),
("四川", 98)
]
map.set_global_opts(
visualmap_opts=VisualMapOpts(is_show=True)
)
map.add("测试地图", data, "china")
map.render()
Case two:
import json
from pyecharts.charts import Map
from pyecharts.options import VisualMapOpts
f = open("疫情.txt", "r", encoding="UTF-8")
data = f.read()
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(
visualmap_opts=VisualMapOpts(is_show=True)
)
map.render()
f.close()
Case three:
import json
from pyecharts.charts import Map
from pyecharts.options import VisualMapOpts
f = open("疫情.txt", "r", encoding="UTF-8")
data = f.read()
data_dict = json.loads(data)
cities_data = data_dict["areaTree"][0]["children"][3]["children"]
data_list = []
for city_data in cities_data:
city_name = city_data["name"] + "市"
city_confirm = city_data["total"]["confirm"]
data_list.append((city_name, city_confirm))
map = Map()
map.add("河南疫情情况", data_list, "河南")
map.set_global_opts(
visualmap_opts=VisualMapOpts(is_show=True)
)
map.render()
f.close()
Build a basic histogram through Bar
Invert the x and y axes
Effect:
Value labels are on the right
Effect:
create timeline
Timeline() - Timeline
If a Bar and Line object is a chart, the timeline is to create a one-dimensional x-axis, and each point on the axis is a chart object.
set autoplay
Case number one:
rom 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, 20, 10], label_opts=LabelOpts(position="right"))
bar1.reversal_axis()
bar2 = Bar()
bar2.add_xaxis(["中国", "美国", "英国"])
bar2.add_yaxis("GDP", [50, 30, 30], label_opts=LabelOpts(position="right"))
bar2.reversal_axis()
bar3 = Bar()
bar3.add_xaxis(["中国", "美国", "英国"])
bar3.add_yaxis("GDP", [80, 50, 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")
timeline.add_schema(
play_interval=1000,
is_timeline_show=True,
is_auto_play=True,
is_loop_play=True
)
timeline.render()
Effect:
Case two:
from pyecharts.charts import Bar, Timeline
from pyecharts.options import *
from pyecharts.globals import ThemeType
f = open("1960-2019全球GDP数据.csv", "r", encoding="GB2312")
data = f.readlines()
data.pop(0)
data_dict = dict()
T = Timeline({"theme": ThemeType.LIGHT})
for data_line in data:
year = int(data_line.split(",")[0])
country = data_line.split(",")[1]
gdp = float(data_line.split(",")[2])
try:
data_dict[year].append([country, gdp])
except KeyError:
data_dict[year] = []
data_dict[year].append([country, gdp])
sorted_year_list = sorted(data_dict.keys())
for year in sorted_year_list:
data_dict[year].sort(key=lambda element: element[1], reverse=True)
year_data = data_dict[year][0:8]
x_data = []
y_data = []
for f_data in year_data:
x_data.append(f_data[0])
y_data.append(f_data[1]/100000000)
bar = Bar()
x_data.reverse()
y_data.reverse()
bar.add_xaxis(x_data)
bar.add_yaxis("GDP(亿元)", y_data, label_opts=LabelOpts(position="right"))
bar.reversal_axis()
bar.set_global_opts(
title_opts=TitleOpts(title=f"{year}年GDP情况")
)
T.add(bar, str(year))
T.add_schema(
play_interval=1000,
is_auto_play=True,
is_timeline_show=True,
is_loop_play=False
)
T.render("1960-2019全球GDP前8国家.html")
Effect: