pyecharts从入门到精通-地图专题Map-带时间轴与网格的复杂绘图

news/2024/12/22 0:08:18/

文章目录

    • 参考
    • 安装与查看pyecharts
    • 地图实现-Geo
      • 导入依赖
      • 生成数据集
        • 生成2013-2018年的各个省份GDP数据
        • 生成2013-2018年的时间列表
        • 生成2013-2018年的总GDP
        • 设置visulmap的最大最小值范围
      • 生成2013年的网格组合图
        • 提取2013年的数据测试
        • 绘制map地图
        • 绘制折线图line
        • 绘制折线图bar
        • 绘制折线图pie
        • 生成2013年网格组合图
      • 生成包含timeline的网格组合图
      • 完整代码如下:

参考

官方文档: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

pip install pyecharts==2.0.3
# Successfully installed prettytable-3.7.0 pyecharts-2.0.3 simplejson-3.19.1

查看版本

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

导入依赖

from typing import Listimport 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数据,数据集如下:

"""
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年的时间列表

# 生成2013-2019年的时间列表
time_list = [str(d) + "年" for d in range(2013, 2019)] 
time_list

在这里插入图片描述

生成2013-2018年的总GDP

total_num = [63.4,68.4,72.3,78,84.7,91.5,
]
total_num

在这里插入图片描述

设置visulmap的最大最小值范围

maxNum = 97300
minNum = 30

生成2013年的网格组合图

提取2013年的数据测试

# 提取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:

#  生成地图 
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地图测试:

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:

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测试:

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:

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测试:

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:

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测试:

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:

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 = 0for 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

测试网格图:

g = get_year_chart(year='2013年',data=data)
g.render_notebook()
# g.render("g.html")

在这里插入图片描述

生成包含timeline的网格组合图

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,结果如下:
在这里插入图片描述

完整代码如下:

from typing import Listimport 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_listtotal_num = [63.4,68.4,72.3,78,84.7,91.5,
]
total_nummaxNum = 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_chartdef 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_chartdef 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 bardef 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 piedef 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 = 0for 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_chartif __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")

在这里插入图片描述


http://www.ppmy.cn/news/47059.html

相关文章

ChatGPT/大模型+零代码,给中小企业带来哪些机会?

ChatGPT让2023年成了AI之年。正如iPhone在2007年开启了智能手机时代,我们现在正在进入人工智能时代。 新形势下,零代码应如何借势发力?伙伴云“AI零代码”给出了答案。 作为零代码领域的头部平台,伙伴云全量发布【AI零代码应用搭…

第四十章 文本渲染总结(未完结)

OpenGL本身没有包含任何的文本处理能力,需要自己定义一套全新的系统让OpenGL绘制文本到屏幕上,但是文本字符没有图元。 可以通过GL_LINES来绘制自行,创建文本的3D网格,或者是将3D环境中字符纹理渲染到2D四边形上。 常用的方法是:将字符纹理绘制到四边形上。 经典文本渲染…

手势控制的机器人手臂

将向你展示如何构建机械手臂并使用手势和计算机视觉来控制它。下面有一个在开发阶段的机械手臂的演示视频。 展示开发中的手臂的演示视频:https://youtu.be/KwiwetZGv0s 如图所示,该过程首先用摄像头捕捉我的手及其标志。通过跟踪特定的界标,…

(7)(7.5) 电机推力比例

文章目录 前言 1 推力曲线 2 推力支架 3 参数计算 前言 Copter 包括电机推力比例,补偿了大多数电调和电机的非线性推力输出。

短轮询、长轮询、SSE 和 WebSocket 间的区别

即时通讯的实现:短轮询、长轮询、SSE 和 WebSocket 间的区别 学习内容: 短轮询:短轮询是一种客户端与服务器之间的通讯方式,客户端定期向服务器发送请求,以检查是否有新消息。如果没有新消息,服务器会返回…

迷宫问题-DFS-BFS

迷宫问题 迷宫问题简介BFS解决迷宫最短路径问题DFS记录迷宫路径DFS解决迷宫所有路径问题 迷宫问题简介 🚀学习过算法程序设计的应该都学习过迷宫这个问题,迷宫问题主要设计的算法就是DFS-深度优先遍历和BFS-广度优先遍历。 🚀在一个二维数组…

gpt4all保姆级使用教程! 不用联网! 本地就能跑的GPT

原文:gpt4all保姆级使用教程! 不用联网! 本地就能跑的GPT 什么是gpt4all gpt4all是在大量干净数据上训练的一个开源聊天机器人的生态系统。它不用科学上网!甚至可以不联网!本地就能用,像这样↓: 如何使用&#xff…

Transactional事务失效场景汇总

文章目录 1、前言2、失效场景2.1、Service没有被Spring管理2.2、事务方法被final、static关键字修饰2.3、同一个类中,方法内部调用2.4、方法的访问权限不是public2.5、数据库的存储引擎不支持事务2.6、Transactional 注解配置错误2.7、使用了错误的事务传播机制2.8、…