参考
官方文档:https://pyecharts.org/#/zh-cn/quickstart Python使用Pyecharts画Geo地图,如何显示具体地点名称 pyecharts世界地图用:国家中英文对照表.xlsx Map中的地理坐标问题 https://gallery.pyecharts.org/#/Map/china_gdp_from_1993_to_2018
安装与查看pyecharts
安装pyecharts
代码语言:javascript复制pip install pyecharts==2.0.3
# Successfully installed prettytable-3.7.0 pyecharts-2.0.3 simplejson-3.19.1
查看版本
代码语言:javascript复制import pyecharts
print(pyecharts.__version__)
# 2.0.3
地图实现-Geo
pyecharts的地图实现包括:
Geo:地理坐标系 Map:地图 BMap:百度地图
本部分完成Map实现,并带有grid与timeline等多种组合技巧,代码来源于: https://gallery.pyecharts.org/#/Map/china_gdp_from_1993_to_2018
导入依赖
代码语言:javascript复制from typing import List
import pyecharts.options as opts
from pyecharts.globals import ThemeType
from pyecharts.commons.utils import JsCode
from pyecharts.charts import Timeline, Grid, Bar, Map, Pie, Line
生成数据集
生成2013-2018年的各个省份GDP数据
数据为中国GDP数据,数据集如下:
代码语言:javascript复制"""
Gallery 使用 pyecharts 1.3.0
From pyecharts 交流分享群 -- 郭昱
"""
data = [
{
"time": "2013年",
"data": [
{"name": "广东省", "value": [62475.0, 9.85, "广东省"]},
{"name": "江苏省", "value": [59753.0, 9.42, "江苏省"]},
{"name": "山东省", "value": [55230.0, 8.71, "山东省"]},
{"name": "浙江省", "value": [37757.0, 5.95, "浙江省"]},
{"name": "河南省", "value": [32191.0, 5.07, "河南省"]},
{"name": "河北省", "value": [28443.0, 4.48, "河北省"]},
{"name": "辽宁省", "value": [27213.0, 4.29, "辽宁省"]},
{"name": "四川省", "value": [26392.0, 4.16, "四川省"]},
{"name": "湖北省", "value": [24792.0, 3.91, "湖北省"]},
{"name": "湖南省", "value": [24622.0, 3.88, "湖南省"]},
{"name": "福建省", "value": [21868.0, 3.45, "福建省"]},
{"name": "上海市", "value": [21818.0, 3.44, "上海市"]},
{"name": "北京市", "value": [19801.0, 3.12, "北京市"]},
{"name": "安徽省", "value": [19229.0, 3.03, "安徽省"]},
{"name": "内蒙古", "value": [16917.0, 2.67, "内蒙古"]},
{"name": "陕西省", "value": [16205.0, 2.55, "陕西省"]},
{"name": "黑龙江省", "value": [14455.0, 2.28, "黑龙江省"]},
{"name": "广西壮族自治区", "value": [14450.0, 2.28, "广西壮族自治区"]},
{"name": "天津市", "value": [14442.0, 2.28, "天津市"]},
{"name": "江西省", "value": [14410.0, 2.27, "江西省"]},
{"name": "吉林省", "value": [13046.0, 2.06, "吉林省"]},
{"name": "重庆市", "value": [12783.0, 2.02, "重庆市"]},
{"name": "山西省", "value": [12665.0, 2.0, "山西省"]},
{"name": "云南省", "value": [11832.0, 1.87, "云南省"]},
{"name": "新疆维吾尔自治区", "value": [8444.0, 1.33, "新疆维吾尔自治区"]},
{"name": "贵州省", "value": [8087.0, 1.27, "贵州省"]},
{"name": "甘肃省", "value": [6331.0, 1.0, "甘肃省"]},
{"name": "海南省", "value": [3178.0, 0.5, "海南省"]},
{"name": "宁夏回族自治区", "value": [2578.0, 0.41, "宁夏回族自治区"]},
{"name": "青海省", "value": [2122.0, 0.33, "青海省"]},
{"name": "西藏自治区", "value": [816.0, 0.13, "西藏自治区"]},
],
},
{
"time": "2014年",
"data": [
{"name": "广东省", "value": [67810.0, 9.91, "广东省"]},
{"name": "江苏省", "value": [65088.0, 9.51, "江苏省"]},
{"name": "山东省", "value": [59427.0, 8.68, "山东省"]},
{"name": "浙江省", "value": [40173.0, 5.87, "浙江省"]},
{"name": "河南省", "value": [34938.0, 5.11, "河南省"]},
{"name": "河北省", "value": [29421.0, 4.3, "河北省"]},
{"name": "辽宁省", "value": [28627.0, 4.18, "辽宁省"]},
{"name": "四川省", "value": [28537.0, 4.17, "四川省"]},
{"name": "湖北省", "value": [27379.0, 4.0, "湖北省"]},
{"name": "湖南省", "value": [27037.0, 3.95, "湖南省"]},
{"name": "福建省", "value": [24056.0, 3.52, "福建省"]},
{"name": "上海市", "value": [23568.0, 3.44, "上海市"]},
{"name": "北京市", "value": [21331.0, 3.12, "北京市"]},
{"name": "安徽省", "value": [20849.0, 3.05, "安徽省"]},
{"name": "内蒙古", "value": [17770.0, 2.6, "内蒙古"]},
{"name": "陕西省", "value": [17690.0, 2.58, "陕西省"]},
{"name": "天津市", "value": [15727.0, 2.3, "天津市"]},
{"name": "江西省", "value": [15715.0, 2.3, "江西省"]},
{"name": "广西壮族自治区", "value": [15673.0, 2.29, "广西壮族自治区"]},
{"name": "黑龙江省", "value": [15039.0, 2.2, "黑龙江省"]},
{"name": "重庆市", "value": [14263.0, 2.08, "重庆市"]},
{"name": "吉林省", "value": [13803.0, 2.02, "吉林省"]},
{"name": "云南省", "value": [12815.0, 1.87, "云南省"]},
{"name": "山西省", "value": [12761.0, 1.86, "山西省"]},
{"name": "新疆维吾尔自治区", "value": [9273.0, 1.36, "新疆维吾尔自治区"]},
{"name": "贵州省", "value": [9266.0, 1.35, "贵州省"]},
{"name": "甘肃省", "value": [6837.0, 1.0, "甘肃省"]},
{"name": "海南省", "value": [3501.0, 0.51, "海南省"]},
{"name": "宁夏回族自治区", "value": [2752.0, 0.4, "宁夏回族自治区"]},
{"name": "青海省", "value": [2303.0, 0.34, "青海省"]},
{"name": "西藏自治区", "value": [921.0, 0.13, "西藏自治区"]},
],
},
{
"time": "2015年",
"data": [
{"name": "广东省", "value": [72813.0, 10.07, "广东省"]},
{"name": "江苏省", "value": [70116.0, 9.7, "江苏省"]},
{"name": "山东省", "value": [63002.0, 8.72, "山东省"]},
{"name": "浙江省", "value": [42886.0, 5.93, "浙江省"]},
{"name": "河南省", "value": [37002.0, 5.12, "河南省"]},
{"name": "四川省", "value": [30053.0, 4.16, "四川省"]},
{"name": "河北省", "value": [29806.0, 4.12, "河北省"]},
{"name": "湖北省", "value": [29550.0, 4.09, "湖北省"]},
{"name": "湖南省", "value": [28902.0, 4.0, "湖南省"]},
{"name": "辽宁省", "value": [28669.0, 3.97, "辽宁省"]},
{"name": "福建省", "value": [25980.0, 3.59, "福建省"]},
{"name": "上海市", "value": [25123.0, 3.48, "上海市"]},
{"name": "北京市", "value": [23015.0, 3.18, "北京市"]},
{"name": "安徽省", "value": [22006.0, 3.04, "安徽省"]},
{"name": "陕西省", "value": [18022.0, 2.49, "陕西省"]},
{"name": "内蒙古", "value": [17832.0, 2.47, "内蒙古"]},
{"name": "广西壮族自治区", "value": [16803.0, 2.32, "广西壮族自治区"]},
{"name": "江西省", "value": [16724.0, 2.31, "江西省"]},
{"name": "天津市", "value": [16538.0, 2.29, "天津市"]},
{"name": "重庆市", "value": [15717.0, 2.17, "重庆市"]},
{"name": "黑龙江省", "value": [15084.0, 2.09, "黑龙江省"]},
{"name": "吉林省", "value": [14063.0, 1.95, "吉林省"]},
{"name": "云南省", "value": [13619.0, 1.88, "云南省"]},
{"name": "山西省", "value": [12766.0, 1.77, "山西省"]},
{"name": "贵州省", "value": [10503.0, 1.45, "贵州省"]},
{"name": "新疆维吾尔自治区", "value": [9325.0, 1.29, "新疆维吾尔自治区"]},
{"name": "甘肃省", "value": [6790.0, 0.94, "甘肃省"]},
{"name": "海南省", "value": [3703.0, 0.51, "海南省"]},
{"name": "宁夏回族自治区", "value": [2912.0, 0.4, "宁夏回族自治区"]},
{"name": "青海省", "value": [2417.0, 0.33, "青海省"]},
{"name": "西藏自治区", "value": [1026.0, 0.14, "西藏自治区"]},
],
},
{
"time": "2016年",
"data": [
{"name": "广东省", "value": [80855.0, 10.37, "广东省"]},
{"name": "江苏省", "value": [77388.0, 9.92, "江苏省"]},
{"name": "山东省", "value": [68024.0, 8.72, "山东省"]},
{"name": "浙江省", "value": [47251.0, 6.06, "浙江省"]},
{"name": "河南省", "value": [40472.0, 5.19, "河南省"]},
{"name": "四川省", "value": [32935.0, 4.22, "四川省"]},
{"name": "湖北省", "value": [32665.0, 4.19, "湖北省"]},
{"name": "河北省", "value": [32070.0, 4.11, "河北省"]},
{"name": "湖南省", "value": [31551.0, 4.04, "湖南省"]},
{"name": "福建省", "value": [28811.0, 3.69, "福建省"]},
{"name": "上海市", "value": [28179.0, 3.61, "上海市"]},
{"name": "北京市", "value": [25669.0, 3.29, "北京市"]},
{"name": "安徽省", "value": [24408.0, 3.13, "安徽省"]},
{"name": "辽宁省", "value": [22247.0, 2.85, "辽宁省"]},
{"name": "陕西省", "value": [19400.0, 2.49, "陕西省"]},
{"name": "江西省", "value": [18499.0, 2.37, "江西省"]},
{"name": "广西壮族自治区", "value": [18318.0, 2.35, "广西壮族自治区"]},
{"name": "内蒙古", "value": [18128.0, 2.32, "内蒙古"]},
{"name": "天津市", "value": [17885.0, 2.29, "天津市"]},
{"name": "重庆市", "value": [17741.0, 2.27, "重庆市"]},
{"name": "黑龙江省", "value": [15386.0, 1.97, "黑龙江省"]},
{"name": "云南省", "value": [14788.0, 1.9, "云南省"]},
{"name": "吉林省", "value": [14777.0, 1.89, "吉林省"]},
{"name": "山西省", "value": [13050.0, 1.67, "山西省"]},
{"name": "贵州省", "value": [11777.0, 1.51, "贵州省"]},
{"name": "新疆维吾尔自治区", "value": [9650.0, 1.24, "新疆维吾尔自治区"]},
{"name": "甘肃省", "value": [7200.0, 0.92, "甘肃省"]},
{"name": "海南省", "value": [4053.0, 0.52, "海南省"]},
{"name": "宁夏回族自治区", "value": [3169.0, 0.41, "宁夏回族自治区"]},
{"name": "青海省", "value": [2572.0, 0.33, "青海省"]},
{"name": "西藏自治区", "value": [1151.0, 0.15, "西藏自治区"]},
],
},
{
"time": "2017年",
"data": [
{"name": "广东省", "value": [89705.0, 10.59, "广东省"]},
{"name": "江苏省", "value": [85870.0, 10.14, "江苏省"]},
{"name": "山东省", "value": [72634.0, 8.57, "山东省"]},
{"name": "浙江省", "value": [51768.0, 6.11, "浙江省"]},
{"name": "河南省", "value": [44553.0, 5.26, "河南省"]},
{"name": "四川省", "value": [36980.0, 4.37, "四川省"]},
{"name": "湖北省", "value": [35478.0, 4.19, "湖北省"]},
{"name": "河北省", "value": [34016.0, 4.02, "河北省"]},
{"name": "湖南省", "value": [33903.0, 4.0, "湖南省"]},
{"name": "福建省", "value": [32182.0, 3.8, "福建省"]},
{"name": "上海市", "value": [30633.0, 3.62, "上海市"]},
{"name": "北京市", "value": [28015.0, 3.31, "北京市"]},
{"name": "安徽省", "value": [27018.0, 3.19, "安徽省"]},
{"name": "辽宁省", "value": [23409.0, 2.76, "辽宁省"]},
{"name": "陕西省", "value": [21899.0, 2.59, "陕西省"]},
{"name": "江西省", "value": [20006.0, 2.36, "江西省"]},
{"name": "重庆市", "value": [19425.0, 2.29, "重庆市"]},
{"name": "天津市", "value": [18549.0, 2.19, "天津市"]},
{"name": "广西壮族自治区", "value": [18523.0, 2.19, "广西壮族自治区"]},
{"name": "云南省", "value": [16376.0, 1.93, "云南省"]},
{"name": "内蒙古", "value": [16096.0, 1.9, "内蒙古"]},
{"name": "黑龙江省", "value": [15903.0, 1.88, "黑龙江省"]},
{"name": "山西省", "value": [15528.0, 1.83, "山西省"]},
{"name": "吉林省", "value": [14945.0, 1.76, "吉林省"]},
{"name": "贵州省", "value": [13541.0, 1.6, "贵州省"]},
{"name": "新疆维吾尔自治区", "value": [10882.0, 1.28, "新疆维吾尔自治区"]},
{"name": "甘肃省", "value": [7460.0, 0.88, "甘肃省"]},
{"name": "海南省", "value": [4463.0, 0.53, "海南省"]},
{"name": "宁夏回族自治区", "value": [3444.0, 0.41, "宁夏回族自治区"]},
{"name": "青海省", "value": [2625.0, 0.31, "青海省"]},
{"name": "西藏自治区", "value": [1311.0, 0.15, "西藏自治区"]},
],
},
{
"time": "2018年",
"data": [
{"name": "广东省", "value": [97278.0, 10.63, "广东省"]},
{"name": "江苏省", "value": [92595.0, 10.12, "江苏省"]},
{"name": "山东省", "value": [76470.0, 8.36, "山东省"]},
{"name": "浙江省", "value": [56197.0, 6.14, "浙江省"]},
{"name": "河南省", "value": [48056.0, 5.25, "河南省"]},
{"name": "四川省", "value": [40678.0, 4.45, "四川省"]},
{"name": "湖北省", "value": [39367.0, 4.3, "湖北省"]},
{"name": "湖南省", "value": [36426.0, 3.98, "湖南省"]},
{"name": "河北省", "value": [36010.0, 3.94, "河北省"]},
{"name": "福建省", "value": [35804.0, 3.91, "福建省"]},
{"name": "上海市", "value": [32680.0, 3.57, "上海市"]},
{"name": "北京市", "value": [30320.0, 3.31, "北京市"]},
{"name": "安徽省", "value": [30007.0, 3.28, "安徽省"]},
{"name": "辽宁省", "value": [25315.0, 2.77, "辽宁省"]},
{"name": "陕西省", "value": [24438.0, 2.67, "陕西省"]},
{"name": "江西省", "value": [21985.0, 2.4, "江西省"]},
{"name": "重庆市", "value": [20363.0, 2.23, "重庆市"]},
{"name": "广西壮族自治区", "value": [20353.0, 2.23, "广西壮族自治区"]},
{"name": "天津市", "value": [18810.0, 2.06, "天津市"]},
{"name": "云南省", "value": [17881.0, 1.95, "云南省"]},
{"name": "内蒙古", "value": [17289.0, 1.89, "内蒙古"]},
{"name": "山西省", "value": [16818.0, 1.84, "山西省"]},
{"name": "黑龙江省", "value": [16362.0, 1.79, "黑龙江省"]},
{"name": "吉林省", "value": [15075.0, 1.65, "吉林省"]},
{"name": "贵州省", "value": [14806.0, 1.62, "贵州省"]},
{"name": "新疆维吾尔自治区", "value": [12199.0, 1.33, "新疆维吾尔自治区"]},
{"name": "甘肃省", "value": [8246.0, 0.9, "甘肃省"]},
{"name": "海南省", "value": [4832.0, 0.53, "海南省"]},
{"name": "宁夏回族自治区", "value": [3705.0, 0.41, "宁夏回族自治区"]},
{"name": "青海省", "value": [2865.0, 0.31, "青海省"]},
{"name": "西藏自治区", "value": [1478.0, 0.16, "西藏自治区"]},
],
},
]
生成2013-2018年的时间列表
代码语言:javascript复制# 生成2013-2019年的时间列表
time_list = [str(d) "年" for d in range(2013, 2019)]
time_list
生成2013-2018年的总GDP
代码语言:javascript复制total_num = [
63.4,
68.4,
72.3,
78,
84.7,
91.5,
]
total_num
设置visulmap的最大最小值范围
代码语言:javascript复制maxNum = 97300
minNum = 30
生成2013年的网格组合图
提取2013年的数据测试
代码语言:javascript复制# 提取2013年的数据
year = '2013年'
# data为每个年份的各个省份GDP数据
map_data = [
[[x["name"], x["value"]] for x in d["data"]] for d in data if d["time"] == year
][0]
print(map_data) #当输入的年份为2013时
'''
[['广东省', [62475.0, 9.85, '广东省']], ['江苏省', [59753.0, 9.42, '江苏省']], ['山东省', [55230.0, 8.71, '山东省']],
...
['青海省', [2122.0, 0.33, '青海省']], ['西藏自治区', [816.0, 0.13, '西藏自治区']]]
'''
min_data, max_data = (minNum, maxNum)
data_mark: List = []
i = 0
for x in time_list:
if x == year:
data_mark.append(total_num[i])
else:
data_mark.append("")
i = i 1
print("data_mark-->",data_mark)
# data_mark--> [63.4, '', '', '', '', ''] #当输入的年份为2013时
绘制map地图
定义绘制地图函数get_year_chart_map:
代码语言:javascript复制# 生成地图
def get_year_chart_map(year: str,map_data,min_data=0,max_data=1000):
map_chart = (
Map()
.add(
series_name="",
data_pair=map_data,
maptype='china',
zoom=1,
center=[119.5, 34.5],
is_map_symbol_show=False,
itemstyle_opts={
"normal": {"areaColor": "#323c48", "borderColor": "#404a59"},
"emphasis": {
"label": {"show": Timeline},
"areaColor": "rgba(255,255,255, 0.5)",
},
},
)
.set_global_opts(
title_opts=opts.TitleOpts(
title="" str(year) "全国分地区GPD情况(单位:亿) 数据来源:国家统计局",
subtitle="",
pos_left="center",
pos_top="top",
title_textstyle_opts=opts.TextStyleOpts(
font_size=25, color="rgba(255,255,255, 0.9)"
),
),
tooltip_opts=opts.TooltipOpts(
is_show=True,
formatter=JsCode(
"""function(params) {
if ('value' in params.data) {
return params.data.value[2] ': ' params.data.value[0];
}
}"""
),
),
visualmap_opts=opts.VisualMapOpts(
is_calculable=True,
dimension=0,
pos_left="30",
pos_top="center",
range_text=["High", "Low"],
range_color=["lightskyblue", "yellow", "orangered"],
textstyle_opts=opts.TextStyleOpts(color="#ddd"),
min_=min_data,
max_=max_data,
),
)
)
return map_chart
生成map地图测试:
代码语言:javascript复制map_chart = get_year_chart_map(year=year,map_data=map_data,min_data=min_data,max_data=max_data)
map_chart.render_notebook()
绘制折线图line
定义绘制函数get_year_chart_line:
代码语言:javascript复制def get_year_chart_line(year=year,map_data=map_data,min_data=min_data,max_data=max_data,data_mark=data_mark):
line_chart = (
Line()
.add_xaxis(time_list)
.add_yaxis("", total_num)
.add_yaxis(
"",
data_mark,
markpoint_opts=opts.MarkPointOpts(data=[opts.MarkPointItem(type_="max")]),
)
.set_series_opts(label_opts=opts.LabelOpts(is_show=False))
.set_global_opts(
title_opts=opts.TitleOpts(
title="全国GDP总量{}(单位:万亿)".format(year), pos_left="50%", pos_top="5%"
)
)
)
return line_chart
生成line测试:
代码语言:javascript复制line = get_year_chart_line(year=year,map_data=map_data,min_data=min_data,max_data=max_data,data_mark=data_mark)
line.render_notebook()
绘制折线图bar
定义绘制函数get_year_chart_bar:
代码语言:javascript复制def get_year_chart_bar(year=year,map_data=map_data,min_data=min_data,max_data=max_data):
bar_x_data = [x[0] for x in map_data]
bar_y_data = [{"name": x[0], "value": x[1][0]} for x in map_data]
bar = (
Bar()
.add_xaxis(xaxis_data=bar_x_data)
.add_yaxis(
series_name="",
y_axis=bar_y_data,
label_opts=opts.LabelOpts(
is_show=True, position="right", formatter="{b} : {c}"
),
)
.reversal_axis()
.set_global_opts(
xaxis_opts=opts.AxisOpts(
max_=maxNum, axislabel_opts=opts.LabelOpts(is_show=False)
),
yaxis_opts=opts.AxisOpts(axislabel_opts=opts.LabelOpts(is_show=False)),
tooltip_opts=opts.TooltipOpts(is_show=False),
visualmap_opts=opts.VisualMapOpts(
is_calculable=True,
dimension=0,
pos_left="10",
pos_top="top",
range_text=["High", "Low"],
range_color=["lightskyblue", "yellow", "orangered"],
textstyle_opts=opts.TextStyleOpts(color="#ddd"),
min_=min_data,
max_=max_data,
),
)
)
return bar
生成bar测试:
代码语言:javascript复制bar = get_year_chart_bar(year=year,map_data=map_data,min_data=min_data,max_data=max_data)
bar.render_notebook()
绘制折线图pie
定义绘制函数get_year_chart_pie:
代码语言:javascript复制def get_year_chart_pie(year=year,map_data=map_data,min_data=min_data,max_data=max_data):
pie_data = [[x[0], x[1][0]] for x in map_data]
pie = (
Pie()
.add(
series_name="",
data_pair=pie_data,
radius=["15%", "35%"],
center=["80%", "82%"],
itemstyle_opts=opts.ItemStyleOpts(
border_width=1, border_color="rgba(0,0,0,0.3)"
),
)
.set_global_opts(
tooltip_opts=opts.TooltipOpts(is_show=True, formatter="{b} {d}%"),
legend_opts=opts.LegendOpts(is_show=False),
)
)
return pie
生成pie测试:
代码语言:javascript复制pie = get_year_chart_pie(year=year,map_data=map_data,min_data=min_data,max_data=max_data)
pie.render_notebook()
生成2013年网格组合图
定义生成网格图函数get_year_chart:
代码语言:javascript复制def get_year_chart(year: str,data,min_data=0,max_data=1000):
# data为每个年份的各个省份GDP数据
map_data = [
[[x["name"], x["value"]] for x in d["data"]] for d in data if d["time"] == year
][0]
# print(map_data) #当输入的年份为2013时
'''
[['广东省', [62475.0, 9.85, '广东省']], ['江苏省', [59753.0, 9.42, '江苏省']], ['山东省', [55230.0, 8.71, '山东省']],
...
['青海省', [2122.0, 0.33, '青海省']], ['西藏自治区', [816.0, 0.13, '西藏自治区']]]
'''
min_data, max_data = (minNum, maxNum)
data_mark: List = []
i = 0
for x in time_list:
if x == year:
data_mark.append(total_num[i])
else:
data_mark.append("")
i = i 1
# print("data_mark-->",data_mark)
# data_mark--> [63.4, '', '', '', '', ''] #当输入的年份为2013时
map_chart = get_year_chart_map(year=year,map_data=map_data,min_data=min_data,max_data=max_data)
line = get_year_chart_line(year=year,map_data=map_data,min_data=min_data,max_data=max_data,data_mark=data_mark)
bar = get_year_chart_bar(year=year,map_data=map_data,min_data=min_data,max_data=max_data)
pie = get_year_chart_pie(year=year,map_data=map_data,min_data=min_data,max_data=max_data)
grid_chart = (
Grid()
.add(
bar,
grid_opts=opts.GridOpts(
pos_left="10", pos_right="45%", pos_top="50%", pos_bottom="5"
),
)
.add(
line,
grid_opts=opts.GridOpts(
pos_left="65%", pos_right="80", pos_top="10%", pos_bottom="50%"
),
)
.add(pie, grid_opts=opts.GridOpts(pos_left="45%", pos_top="60%"))
.add(map_chart, grid_opts=opts.GridOpts())
)
return grid_chart
测试网格图:
代码语言:javascript复制g = get_year_chart(year='2013年',data=data)
g.render_notebook()
# g.render("g.html")
生成包含timeline的网格组合图
代码语言:javascript复制if __name__ == "__main__":
timeline = Timeline(
init_opts=opts.InitOpts(width="1600px", height="900px", theme=ThemeType.DARK)
)
for y in time_list:
g = get_year_chart(year=y,data=data)
timeline.add(g, time_point=str(y))
timeline.add_schema(
orient="vertical",
is_auto_play=True,
is_inverse=True,
play_interval=5000,
pos_left="null",
pos_right="5",
pos_top="20",
pos_bottom="20",
width="60",
label_opts=opts.LabelOpts(is_show=True, color="#fff"),
)
# timeline.render_notebook()
timeline.render("china_gdp_from_2013_to_2018.html")
查看china_gdp_from_1993_to_2018.html,结果如下:
完整代码如下:
代码语言:javascript复制from typing import List
import pyecharts.options as opts
from pyecharts.globals import ThemeType
from pyecharts.commons.utils import JsCode
from pyecharts.charts import Timeline, Grid, Bar, Map, Pie, Line
"""
Gallery 使用 pyecharts 1.3.0
From pyecharts 交流分享群 -- 郭昱
"""
data = [
{
"time": "2013年",
"data": [
{"name": "广东省", "value": [62475.0, 9.85, "广东省"]},
{"name": "江苏省", "value": [59753.0, 9.42, "江苏省"]},
{"name": "山东省", "value": [55230.0, 8.71, "山东省"]},
{"name": "浙江省", "value": [37757.0, 5.95, "浙江省"]},
{"name": "河南省", "value": [32191.0, 5.07, "河南省"]},
{"name": "河北省", "value": [28443.0, 4.48, "河北省"]},
{"name": "辽宁省", "value": [27213.0, 4.29, "辽宁省"]},
{"name": "四川省", "value": [26392.0, 4.16, "四川省"]},
{"name": "湖北省", "value": [24792.0, 3.91, "湖北省"]},
{"name": "湖南省", "value": [24622.0, 3.88, "湖南省"]},
{"name": "福建省", "value": [21868.0, 3.45, "福建省"]},
{"name": "上海市", "value": [21818.0, 3.44, "上海市"]},
{"name": "北京市", "value": [19801.0, 3.12, "北京市"]},
{"name": "安徽省", "value": [19229.0, 3.03, "安徽省"]},
{"name": "内蒙古", "value": [16917.0, 2.67, "内蒙古"]},
{"name": "陕西省", "value": [16205.0, 2.55, "陕西省"]},
{"name": "黑龙江省", "value": [14455.0, 2.28, "黑龙江省"]},
{"name": "广西壮族自治区", "value": [14450.0, 2.28, "广西壮族自治区"]},
{"name": "天津市", "value": [14442.0, 2.28, "天津市"]},
{"name": "江西省", "value": [14410.0, 2.27, "江西省"]},
{"name": "吉林省", "value": [13046.0, 2.06, "吉林省"]},
{"name": "重庆市", "value": [12783.0, 2.02, "重庆市"]},
{"name": "山西省", "value": [12665.0, 2.0, "山西省"]},
{"name": "云南省", "value": [11832.0, 1.87, "云南省"]},
{"name": "新疆维吾尔自治区", "value": [8444.0, 1.33, "新疆维吾尔自治区"]},
{"name": "贵州省", "value": [8087.0, 1.27, "贵州省"]},
{"name": "甘肃省", "value": [6331.0, 1.0, "甘肃省"]},
{"name": "海南省", "value": [3178.0, 0.5, "海南省"]},
{"name": "宁夏回族自治区", "value": [2578.0, 0.41, "宁夏回族自治区"]},
{"name": "青海省", "value": [2122.0, 0.33, "青海省"]},
{"name": "西藏自治区", "value": [816.0, 0.13, "西藏自治区"]},
],
},
{
"time": "2014年",
"data": [
{"name": "广东省", "value": [67810.0, 9.91, "广东省"]},
{"name": "江苏省", "value": [65088.0, 9.51, "江苏省"]},
{"name": "山东省", "value": [59427.0, 8.68, "山东省"]},
{"name": "浙江省", "value": [40173.0, 5.87, "浙江省"]},
{"name": "河南省", "value": [34938.0, 5.11, "河南省"]},
{"name": "河北省", "value": [29421.0, 4.3, "河北省"]},
{"name": "辽宁省", "value": [28627.0, 4.18, "辽宁省"]},
{"name": "四川省", "value": [28537.0, 4.17, "四川省"]},
{"name": "湖北省", "value": [27379.0, 4.0, "湖北省"]},
{"name": "湖南省", "value": [27037.0, 3.95, "湖南省"]},
{"name": "福建省", "value": [24056.0, 3.52, "福建省"]},
{"name": "上海市", "value": [23568.0, 3.44, "上海市"]},
{"name": "北京市", "value": [21331.0, 3.12, "北京市"]},
{"name": "安徽省", "value": [20849.0, 3.05, "安徽省"]},
{"name": "内蒙古", "value": [17770.0, 2.6, "内蒙古"]},
{"name": "陕西省", "value": [17690.0, 2.58, "陕西省"]},
{"name": "天津市", "value": [15727.0, 2.3, "天津市"]},
{"name": "江西省", "value": [15715.0, 2.3, "江西省"]},
{"name": "广西壮族自治区", "value": [15673.0, 2.29, "广西壮族自治区"]},
{"name": "黑龙江省", "value": [15039.0, 2.2, "黑龙江省"]},
{"name": "重庆市", "value": [14263.0, 2.08, "重庆市"]},
{"name": "吉林省", "value": [13803.0, 2.02, "吉林省"]},
{"name": "云南省", "value": [12815.0, 1.87, "云南省"]},
{"name": "山西省", "value": [12761.0, 1.86, "山西省"]},
{"name": "新疆维吾尔自治区", "value": [9273.0, 1.36, "新疆维吾尔自治区"]},
{"name": "贵州省", "value": [9266.0, 1.35, "贵州省"]},
{"name": "甘肃省", "value": [6837.0, 1.0, "甘肃省"]},
{"name": "海南省", "value": [3501.0, 0.51, "海南省"]},
{"name": "宁夏回族自治区", "value": [2752.0, 0.4, "宁夏回族自治区"]},
{"name": "青海省", "value": [2303.0, 0.34, "青海省"]},
{"name": "西藏自治区", "value": [921.0, 0.13, "西藏自治区"]},
],
},
{
"time": "2015年",
"data": [
{"name": "广东省", "value": [72813.0, 10.07, "广东省"]},
{"name": "江苏省", "value": [70116.0, 9.7, "江苏省"]},
{"name": "山东省", "value": [63002.0, 8.72, "山东省"]},
{"name": "浙江省", "value": [42886.0, 5.93, "浙江省"]},
{"name": "河南省", "value": [37002.0, 5.12, "河南省"]},
{"name": "四川省", "value": [30053.0, 4.16, "四川省"]},
{"name": "河北省", "value": [29806.0, 4.12, "河北省"]},
{"name": "湖北省", "value": [29550.0, 4.09, "湖北省"]},
{"name": "湖南省", "value": [28902.0, 4.0, "湖南省"]},
{"name": "辽宁省", "value": [28669.0, 3.97, "辽宁省"]},
{"name": "福建省", "value": [25980.0, 3.59, "福建省"]},
{"name": "上海市", "value": [25123.0, 3.48, "上海市"]},
{"name": "北京市", "value": [23015.0, 3.18, "北京市"]},
{"name": "安徽省", "value": [22006.0, 3.04, "安徽省"]},
{"name": "陕西省", "value": [18022.0, 2.49, "陕西省"]},
{"name": "内蒙古", "value": [17832.0, 2.47, "内蒙古"]},
{"name": "广西壮族自治区", "value": [16803.0, 2.32, "广西壮族自治区"]},
{"name": "江西省", "value": [16724.0, 2.31, "江西省"]},
{"name": "天津市", "value": [16538.0, 2.29, "天津市"]},
{"name": "重庆市", "value": [15717.0, 2.17, "重庆市"]},
{"name": "黑龙江省", "value": [15084.0, 2.09, "黑龙江省"]},
{"name": "吉林省", "value": [14063.0, 1.95, "吉林省"]},
{"name": "云南省", "value": [13619.0, 1.88, "云南省"]},
{"name": "山西省", "value": [12766.0, 1.77, "山西省"]},
{"name": "贵州省", "value": [10503.0, 1.45, "贵州省"]},
{"name": "新疆维吾尔自治区", "value": [9325.0, 1.29, "新疆维吾尔自治区"]},
{"name": "甘肃省", "value": [6790.0, 0.94, "甘肃省"]},
{"name": "海南省", "value": [3703.0, 0.51, "海南省"]},
{"name": "宁夏回族自治区", "value": [2912.0, 0.4, "宁夏回族自治区"]},
{"name": "青海省", "value": [2417.0, 0.33, "青海省"]},
{"name": "西藏自治区", "value": [1026.0, 0.14, "西藏自治区"]},
],
},
{
"time": "2016年",
"data": [
{"name": "广东省", "value": [80855.0, 10.37, "广东省"]},
{"name": "江苏省", "value": [77388.0, 9.92, "江苏省"]},
{"name": "山东省", "value": [68024.0, 8.72, "山东省"]},
{"name": "浙江省", "value": [47251.0, 6.06, "浙江省"]},
{"name": "河南省", "value": [40472.0, 5.19, "河南省"]},
{"name": "四川省", "value": [32935.0, 4.22, "四川省"]},
{"name": "湖北省", "value": [32665.0, 4.19, "湖北省"]},
{"name": "河北省", "value": [32070.0, 4.11, "河北省"]},
{"name": "湖南省", "value": [31551.0, 4.04, "湖南省"]},
{"name": "福建省", "value": [28811.0, 3.69, "福建省"]},
{"name": "上海市", "value": [28179.0, 3.61, "上海市"]},
{"name": "北京市", "value": [25669.0, 3.29, "北京市"]},
{"name": "安徽省", "value": [24408.0, 3.13, "安徽省"]},
{"name": "辽宁省", "value": [22247.0, 2.85, "辽宁省"]},
{"name": "陕西省", "value": [19400.0, 2.49, "陕西省"]},
{"name": "江西省", "value": [18499.0, 2.37, "江西省"]},
{"name": "广西壮族自治区", "value": [18318.0, 2.35, "广西壮族自治区"]},
{"name": "内蒙古", "value": [18128.0, 2.32, "内蒙古"]},
{"name": "天津市", "value": [17885.0, 2.29, "天津市"]},
{"name": "重庆市", "value": [17741.0, 2.27, "重庆市"]},
{"name": "黑龙江省", "value": [15386.0, 1.97, "黑龙江省"]},
{"name": "云南省", "value": [14788.0, 1.9, "云南省"]},
{"name": "吉林省", "value": [14777.0, 1.89, "吉林省"]},
{"name": "山西省", "value": [13050.0, 1.67, "山西省"]},
{"name": "贵州省", "value": [11777.0, 1.51, "贵州省"]},
{"name": "新疆维吾尔自治区", "value": [9650.0, 1.24, "新疆维吾尔自治区"]},
{"name": "甘肃省", "value": [7200.0, 0.92, "甘肃省"]},
{"name": "海南省", "value": [4053.0, 0.52, "海南省"]},
{"name": "宁夏回族自治区", "value": [3169.0, 0.41, "宁夏回族自治区"]},
{"name": "青海省", "value": [2572.0, 0.33, "青海省"]},
{"name": "西藏自治区", "value": [1151.0, 0.15, "西藏自治区"]},
],
},
{
"time": "2017年",
"data": [
{"name": "广东省", "value": [89705.0, 10.59, "广东省"]},
{"name": "江苏省", "value": [85870.0, 10.14, "江苏省"]},
{"name": "山东省", "value": [72634.0, 8.57, "山东省"]},
{"name": "浙江省", "value": [51768.0, 6.11, "浙江省"]},
{"name": "河南省", "value": [44553.0, 5.26, "河南省"]},
{"name": "四川省", "value": [36980.0, 4.37, "四川省"]},
{"name": "湖北省", "value": [35478.0, 4.19, "湖北省"]},
{"name": "河北省", "value": [34016.0, 4.02, "河北省"]},
{"name": "湖南省", "value": [33903.0, 4.0, "湖南省"]},
{"name": "福建省", "value": [32182.0, 3.8, "福建省"]},
{"name": "上海市", "value": [30633.0, 3.62, "上海市"]},
{"name": "北京市", "value": [28015.0, 3.31, "北京市"]},
{"name": "安徽省", "value": [27018.0, 3.19, "安徽省"]},
{"name": "辽宁省", "value": [23409.0, 2.76, "辽宁省"]},
{"name": "陕西省", "value": [21899.0, 2.59, "陕西省"]},
{"name": "江西省", "value": [20006.0, 2.36, "江西省"]},
{"name": "重庆市", "value": [19425.0, 2.29, "重庆市"]},
{"name": "天津市", "value": [18549.0, 2.19, "天津市"]},
{"name": "广西壮族自治区", "value": [18523.0, 2.19, "广西壮族自治区"]},
{"name": "云南省", "value": [16376.0, 1.93, "云南省"]},
{"name": "内蒙古", "value": [16096.0, 1.9, "内蒙古"]},
{"name": "黑龙江省", "value": [15903.0, 1.88, "黑龙江省"]},
{"name": "山西省", "value": [15528.0, 1.83, "山西省"]},
{"name": "吉林省", "value": [14945.0, 1.76, "吉林省"]},
{"name": "贵州省", "value": [13541.0, 1.6, "贵州省"]},
{"name": "新疆维吾尔自治区", "value": [10882.0, 1.28, "新疆维吾尔自治区"]},
{"name": "甘肃省", "value": [7460.0, 0.88, "甘肃省"]},
{"name": "海南省", "value": [4463.0, 0.53, "海南省"]},
{"name": "宁夏回族自治区", "value": [3444.0, 0.41, "宁夏回族自治区"]},
{"name": "青海省", "value": [2625.0, 0.31, "青海省"]},
{"name": "西藏自治区", "value": [1311.0, 0.15, "西藏自治区"]},
],
},
{
"time": "2018年",
"data": [
{"name": "广东省", "value": [97278.0, 10.63, "广东省"]},
{"name": "江苏省", "value": [92595.0, 10.12, "江苏省"]},
{"name": "山东省", "value": [76470.0, 8.36, "山东省"]},
{"name": "浙江省", "value": [56197.0, 6.14, "浙江省"]},
{"name": "河南省", "value": [48056.0, 5.25, "河南省"]},
{"name": "四川省", "value": [40678.0, 4.45, "四川省"]},
{"name": "湖北省", "value": [39367.0, 4.3, "湖北省"]},
{"name": "湖南省", "value": [36426.0, 3.98, "湖南省"]},
{"name": "河北省", "value": [36010.0, 3.94, "河北省"]},
{"name": "福建省", "value": [35804.0, 3.91, "福建省"]},
{"name": "上海市", "value": [32680.0, 3.57, "上海市"]},
{"name": "北京市", "value": [30320.0, 3.31, "北京市"]},
{"name": "安徽省", "value": [30007.0, 3.28, "安徽省"]},
{"name": "辽宁省", "value": [25315.0, 2.77, "辽宁省"]},
{"name": "陕西省", "value": [24438.0, 2.67, "陕西省"]},
{"name": "江西省", "value": [21985.0, 2.4, "江西省"]},
{"name": "重庆市", "value": [20363.0, 2.23, "重庆市"]},
{"name": "广西壮族自治区", "value": [20353.0, 2.23, "广西壮族自治区"]},
{"name": "天津市", "value": [18810.0, 2.06, "天津市"]},
{"name": "云南省", "value": [17881.0, 1.95, "云南省"]},
{"name": "内蒙古", "value": [17289.0, 1.89, "内蒙古"]},
{"name": "山西省", "value": [16818.0, 1.84, "山西省"]},
{"name": "黑龙江省", "value": [16362.0, 1.79, "黑龙江省"]},
{"name": "吉林省", "value": [15075.0, 1.65, "吉林省"]},
{"name": "贵州省", "value": [14806.0, 1.62, "贵州省"]},
{"name": "新疆维吾尔自治区", "value": [12199.0, 1.33, "新疆维吾尔自治区"]},
{"name": "甘肃省", "value": [8246.0, 0.9, "甘肃省"]},
{"name": "海南省", "value": [4832.0, 0.53, "海南省"]},
{"name": "宁夏回族自治区", "value": [3705.0, 0.41, "宁夏回族自治区"]},
{"name": "青海省", "value": [2865.0, 0.31, "青海省"]},
{"name": "西藏自治区", "value": [1478.0, 0.16, "西藏自治区"]},
],
},
]
# 生成2013-2019年的时间列表
time_list = [str(d) "年" for d in range(2013, 2019)]
time_list
total_num = [
63.4,
68.4,
72.3,
78,
84.7,
91.5,
]
total_num
maxNum = 97300
minNum = 30
# 生成地图
def get_year_chart_map(year: str,map_data,min_data=0,max_data=1000):
map_chart = (
Map()
.add(
series_name="",
data_pair=map_data,
maptype='china',
zoom=1,
center=[119.5, 34.5],
is_map_symbol_show=False,
itemstyle_opts={
"normal": {"areaColor": "#323c48", "borderColor": "#404a59"},
"emphasis": {
"label": {"show": Timeline},
"areaColor": "rgba(255,255,255, 0.5)",
},
},
)
.set_global_opts(
title_opts=opts.TitleOpts(
title="" str(year) "全国分地区GPD情况(单位:亿) 数据来源:国家统计局",
subtitle="",
pos_left="center",
pos_top="top",
title_textstyle_opts=opts.TextStyleOpts(
font_size=25, color="rgba(255,255,255, 0.9)"
),
),
tooltip_opts=opts.TooltipOpts(
is_show=True,
formatter=JsCode(
"""function(params) {
if ('value' in params.data) {
return params.data.value[2] ': ' params.data.value[0];
}
}"""
),
),
visualmap_opts=opts.VisualMapOpts(
is_calculable=True,
dimension=0,
pos_left="30",
pos_top="center",
range_text=["High", "Low"],
range_color=["lightskyblue", "yellow", "orangered"],
textstyle_opts=opts.TextStyleOpts(color="#ddd"),
min_=min_data,
max_=max_data,
),
)
)
return map_chart
def get_year_chart_line(year=year,map_data=map_data,min_data=min_data,max_data=max_data,data_mark=data_mark):
line_chart = (
Line()
.add_xaxis(time_list)
.add_yaxis('', total_num)
.add_yaxis(
"",
data_mark,
markpoint_opts=opts.MarkPointOpts(data=[opts.MarkPointItem(type_="max")]),
)
.set_series_opts(label_opts=opts.LabelOpts(is_show=False))
.set_global_opts(
title_opts=opts.TitleOpts(
title="全国GDP总量{}(单位:万亿)".format(year), pos_left="50%", pos_top="5%"
)
)
)
return line_chart
def get_year_chart_bar(year=year,map_data=map_data,min_data=min_data,max_data=max_data):
bar_x_data = [x[0] for x in map_data]
bar_y_data = [{"name": x[0], "value": x[1][0]} for x in map_data]
bar = (
Bar()
.add_xaxis(xaxis_data=bar_x_data)
.add_yaxis(
series_name="",
y_axis=bar_y_data,
label_opts=opts.LabelOpts(
is_show=True, position="right", formatter="{b} : {c}"
),
)
.reversal_axis()
.set_global_opts(
xaxis_opts=opts.AxisOpts(
max_=maxNum, axislabel_opts=opts.LabelOpts(is_show=False)
),
yaxis_opts=opts.AxisOpts(axislabel_opts=opts.LabelOpts(is_show=False)),
tooltip_opts=opts.TooltipOpts(is_show=False),
visualmap_opts=opts.VisualMapOpts(
is_calculable=True,
dimension=0,
pos_left="10",
pos_top="top",
range_text=["High", "Low"],
range_color=["lightskyblue", "yellow", "orangered"],
textstyle_opts=opts.TextStyleOpts(color="#ddd"),
min_=min_data,
max_=max_data,
),
)
)
return bar
def get_year_chart_pie(year=year,map_data=map_data,min_data=min_data,max_data=max_data):
pie_data = [[x[0], x[1][0]] for x in map_data]
pie = (
Pie()
.add(
series_name="",
data_pair=pie_data,
radius=["15%", "35%"],
center=["80%", "82%"],
itemstyle_opts=opts.ItemStyleOpts(
border_width=1, border_color="rgba(0,0,0,0.3)"
),
)
.set_global_opts(
tooltip_opts=opts.TooltipOpts(is_show=True, formatter="{b} {d}%"),
legend_opts=opts.LegendOpts(is_show=False),
)
)
return pie
def get_year_chart(year: str,data,min_data=0,max_data=1000):
# data为每个年份的各个省份GDP数据
map_data = [
[[x["name"], x["value"]] for x in d["data"]] for d in data if d["time"] == year
][0]
# print(map_data) #当输入的年份为2013时
'''
[['广东省', [62475.0, 9.85, '广东省']], ['江苏省', [59753.0, 9.42, '江苏省']], ['山东省', [55230.0, 8.71, '山东省']],
...
['青海省', [2122.0, 0.33, '青海省']], ['西藏自治区', [816.0, 0.13, '西藏自治区']]]
'''
min_data, max_data = (minNum, maxNum)
data_mark: List = []
i = 0
for x in time_list:
if x == year:
data_mark.append(total_num[i])
else:
data_mark.append("")
i = i 1
# print("data_mark-->",data_mark)
# data_mark--> [63.4, '', '', '', '', ''] #当输入的年份为2013时
map_chart = get_year_chart_map(year=year,map_data=map_data,min_data=min_data,max_data=max_data)
line = get_year_chart_line(year=year,map_data=map_data,min_data=min_data,max_data=max_data,data_mark=data_mark)
bar = get_year_chart_bar(year=year,map_data=map_data,min_data=min_data,max_data=max_data)
pie = get_year_chart_pie(year=year,map_data=map_data,min_data=min_data,max_data=max_data)
grid_chart = (
Grid()
.add(
bar,
grid_opts=opts.GridOpts(
pos_left="10", pos_right="45%", pos_top="50%", pos_bottom="5"
),
)
.add(
line,
grid_opts=opts.GridOpts(
pos_left="65%", pos_right="80", pos_top="10%", pos_bottom="50%"
),
)
.add(pie, grid_opts=opts.GridOpts(pos_left="45%", pos_top="60%"))
.add(map_chart, grid_opts=opts.GridOpts())
)
return grid_chart
if __name__ == "__main__":
timeline = Timeline(
init_opts=opts.InitOpts(width="1600px", height="900px", theme=ThemeType.DARK)
)
for y in time_list:
g = get_year_chart(year=y,data=data)
timeline.add(g, time_point=str(y))
timeline.add_schema(
orient="vertical",
is_auto_play=True,
is_inverse=True,
play_interval=5000,
pos_left="null",
pos_right="5",
pos_top="20",
pos_bottom="20",
width="60",
label_opts=opts.LabelOpts(is_show=True, color="#fff"),
)
# timeline.render_notebook()
timeline.render("china_gdp_from_201313_to_2018.html")