python热力图制作,保存html为图片

调用百度地图的API,使用pyecharts(一个由百度开源的数据可视化库),制作一个可视化热力图。

第一步导入包:

import pandas as pd
from pyecharts.charts import BMap
from pyecharts import options as opts
from pyecharts.globals import ChartType
from pyecharts.charts import Timeline
from pyecharts.charts import Map
import imgkit

第二步,用pandas取出数据:

python热力图制作,保存html为图片_第1张图片

data = pd.read_excel(r'C:\Users\86130\Desktop\GDP2.xlsx',index_col='time',engine='openpyxl')

 第三步对数据格式进行处理:

for i in range(n):
    row = data.iloc[i,].tolist()
    c1 = 'list%s'%i
    c1 = list(zip(province,row))#地区与数量数据打包为[(),(),...]形式
    time = format(data.index[i], "%Y-%m-%d")#时间格式定义

 第四步设置地图

    c = (
        BMap(init_opts=opts.InitOpts(width="1000px", height="600px")) #设置地图长宽
            .add_schema(baidu_ak="WiCunlcjK6L7a8Wu1g2zf5pAlNVZHrq1" #填写你的百度ak
                        , center=[120.13066322374, 30.240018034923] #中心点位置
                        , zoom=5#缩放比例
                        , is_roam=True
                        # , map_style={                        # 对地图样式进行配置
                        #     "styleJson": [
                        #         {
                        #             "featureType": "water",
                        #             "elementType": "all",
                        #             "stylers": {"color": "#044161"},
                        #         },
                        #         {
                        #             "featureType": "land",
                        #             "elementType": "all",
                        #             "stylers": {"color": "#004981"},
                        #         },
                        #         {
                        #             "featureType": "boundary",
                        #             "elementType": "geometry",
                        #             "stylers": {"color": "#064f85"},
                        #         },
                        #         {
                        #             "featureType": "railway",
                        #             "elementType": "all",
                        #             "stylers": {"visibility": "off"},
                        #         },
                        #         {
                        #             "featureType": "highway",
                        #             "elementType": "geometry",
                        #             "stylers": {"color": "#004981"},
                        #         },
                        #         {
                        #             "featureType": "highway",
                        #             "elementType": "geometry.fill",
                        #             "stylers": {"color": "#005b96", "lightness": 1},
                        #         },
                        #         {
                        #             "featureType": "highway",
                        #             "elementType": "labels",
                        #             "stylers": {"visibility": "off"},
                        #         },
                        #         {
                        #             "featureType": "arterial",
                        #             "elementType": "geometry",
                        #             "stylers": {"color": "#004981"},
                        #         },
                        #         {
                        #             "featureType": "arterial",
                        #             "elementType": "geometry.fill",
                        #             "stylers": {"color": "#00508b"},
                        #         },
                        #         {
                        #             "featureType": "poi",
                        #             "elementType": "all",
                        #             "stylers": {"visibility": "off"},
                        #         },
                        #         {
                        #             "featureType": "green",
                        #             "elementType": "all",
                        #             "stylers": {"color": "#056197", "visibility": "off"},
                        #         },
                        #         {
                        #             "featureType": "subway",
                        #             "elementType": "all",
                        #             "stylers": {"visibility": "off"},
                        #         },
                        #         {
                        #             "featureType": "manmade",
                        #             "elementType": "all",
                        #             "stylers": {"visibility": "off"},
                        #         },
                        #         {
                        #             "featureType": "local",
                        #             "elementType": "all",
                        #             "stylers": {"visibility": "off"},
                        #         },
                        #         {
                        #             "featureType": "arterial",
                        #             "elementType": "labels",
                        #             "stylers": {"visibility": "off"},
                        #         },
                        #         {
                        #             "featureType": "boundary",
                        #             "elementType": "geometry.fill",
                        #             "stylers": {"color": "#029fd4"},
                        #         },
                        #         {
                        #             "featureType": "building",
                        #             "elementType": "all",
                        #             "stylers": {"color": "#1a5787"},
                        #         },
                        #         {
                        #             "featureType": "label",
                        #             "elementType": "all",
                        #             "stylers": {"visibility": "off"},
                        #         },
                        #     ]
                        # },
                        )


        .add(
            time,
            c1,
            type_="scatter",  # scatter为散点图,heatmap为热力图,ChartType.EFFECT_SCATTER为涟漪图


            symbol_size=10,
            effect_opts=opts.EffectOpts(),
            label_opts=opts.LabelOpts(
                position="top",  # 标签位置
                is_show=True,  # is_show是否显示标签,点上面的内容
                formatter=JsCode(                  #formatter为标签内容格式器{a}:系列名;{b}:数据名;{c}:数值数组也可以是回调函数
                    """function(params) {
                    if ('value' in params.data) {
                        return params.data.value[2];
                    }
                }"""
                ),

            )



        )
        .set_global_opts(
            title_opts=opts.TitleOpts(
                title="图",

            ),
            tooltip_opts=opts.TooltipOpts(  # 标签配置,选中某一个点显示的框
                trigger="item",  # 触发类型,item主要用于散点图
                formatter=JsCode(  # 显示提示框formatter为标签内容格式器{a}:系列名;{b}:数据名;{c}:数值数组也可以是回调函数
                    """function(params) {
                    if ('value' in params.data) {
                        return params.data.name + '
' + params.data.value[2]; } }""" ), # 显示数据,可以去掉经纬度,只显示数值return params.data.value[2] + ': ' + params.data.value[0]+': ' + params.data.value[1]; ), visualmap_opts=opts.VisualMapOpts( # 视觉映射配置 pos_top="10", # 图例离顶部的距离 pos_left="10", # 图例离左边边框的距离 type_="size", # 映射过渡类型,可选,"color", "size" (颜色映射还是大小映射)(重要!!!) # 其中color参数使点的颜色随着数值进行变化,size参数使点的大小随着数值进行变化(变大变小) max_=300, # 指定 visualMapPiecewise 组件的最大值。 is_piecewise=True, # 是否分段 pieces=[ # 自定义的每一段的范围,以及每一段的文字,以及每一段的特别的样式 {"max": 149, "min": 20, "label": ">=120", "color": "#B40404"}, # label自定义的文字,color自定义的每一段的颜色 {"max": 20, "min": 10, "label": "90-119", "color": "#DF0101"}, {"max": 10, "min": 6, "label": "60-89", "color": "#F78181"}, {"max": 5, "min": 4, "label": "30-59", "color": "#F5A9A9"}, {"max": 3, "min": 1, "label": "1-29", "color": "#FFFFCC"}, {"value": 0, "label": '0', "color": 'grey'} # 专门为特定数据定义样式,例如给值为0的点定义灰色,但是0不能在上面的范围里,否则不显示定义的颜色 ] ), ) )

以上地图绘制,参考于此处:

pyecharts基于Bmap绘制地理图标,制作景点客源分布图,数据位置上标明散点,散点大小和颜色随着数值进行变化_威斯登的博客-CSDN博客

第五步打印html:

c.render(r"C:\Users\86130\Desktop\2Bmap1.html")#设置自己的存储地址

第6步:将html打印为图片:

    path_wkimg = r'C:\Users\86130\Desktop\wkhtmltox-0.12.6-1.mxe-cross-win64\wkhtmltox\bin\wkhtmltoimage.exe'  # 工具路径
    cfg = imgkit.config(wkhtmltoimage=path_wkimg)
    # 1、将html文件转为图片
    imgkit.from_file(r'C:\Users\86130\Desktop\2Bmap1.html', 'helloworld%s.jpg'%i, config=cfg)

为了有一个连续的效果,所以做个这么的玩意,运行及其缓慢

以下是整体的代码:

import pandas as pd
from pyecharts.charts import BMap
from pyecharts import options as opts
from pyecharts.globals import ChartType
from pyecharts.charts import Timeline
from pyecharts.charts import Map
from pyecharts.commons.utils import JsCode
import imgkit



data = pd.read_excel(r'C:\Users\86130\Desktop\dtsj.xlsx',index_col='time',engine='openpyxl')
#print(data)
province = data.columns.tolist()#行的索引
#province = list(data["province"])
n = len(data.index)
#gdp = list(data[1])
#print(province)
#print(n)
#print(list)

for i in range(n):
    row = data.iloc[i,].tolist()
    #print(i)
    #print(row)
    c1 = 'list%s'%i
    #c1 = [list(z) for z in zip(province,row)]
    c1 = list(zip(province,row))
    #print(c1)
    #print(data.index[i])
    #print(1)
    time = format(data.index[i], "%Y-%m-%d")

#    time = data.head(1)



    c = (
        BMap(init_opts=opts.InitOpts(width="1000px", height="600px"))
            .add_schema(baidu_ak="WiCunlcjK6L7a8Wu1g2zf5pAlNVZHrq1"
                        , center=[120.13066322374, 30.240018034923]
                        , zoom=5
                        , is_roam=True
                        # , map_style={  # 对地图样式进行配置
                        #     "styleJson": [
                        #         {
                        #             "featureType": "water",
                        #             "elementType": "all",
                        #             "stylers": {"color": "#044161"},
                        #         },
                        #         {
                        #             "featureType": "land",
                        #             "elementType": "all",
                        #             "stylers": {"color": "#004981"},
                        #         },
                        #         {
                        #             "featureType": "boundary",
                        #             "elementType": "geometry",
                        #             "stylers": {"color": "#064f85"},
                        #         },
                        #         {
                        #             "featureType": "railway",
                        #             "elementType": "all",
                        #             "stylers": {"visibility": "off"},
                        #         },
                        #         {
                        #             "featureType": "highway",
                        #             "elementType": "geometry",
                        #             "stylers": {"color": "#004981"},
                        #         },
                        #         {
                        #             "featureType": "highway",
                        #             "elementType": "geometry.fill",
                        #             "stylers": {"color": "#005b96", "lightness": 1},
                        #         },
                        #         {
                        #             "featureType": "highway",
                        #             "elementType": "labels",
                        #             "stylers": {"visibility": "off"},
                        #         },
                        #         {
                        #             "featureType": "arterial",
                        #             "elementType": "geometry",
                        #             "stylers": {"color": "#004981"},
                        #         },
                        #         {
                        #             "featureType": "arterial",
                        #             "elementType": "geometry.fill",
                        #             "stylers": {"color": "#00508b"},
                        #         },
                        #         {
                        #             "featureType": "poi",
                        #             "elementType": "all",
                        #             "stylers": {"visibility": "off"},
                        #         },
                        #         {
                        #             "featureType": "green",
                        #             "elementType": "all",
                        #             "stylers": {"color": "#056197", "visibility": "off"},
                        #         },
                        #         {
                        #             "featureType": "subway",
                        #             "elementType": "all",
                        #             "stylers": {"visibility": "off"},
                        #         },
                        #         {
                        #             "featureType": "manmade",
                        #             "elementType": "all",
                        #             "stylers": {"visibility": "off"},
                        #         },
                        #         {
                        #             "featureType": "local",
                        #             "elementType": "all",
                        #             "stylers": {"visibility": "off"},
                        #         },
                        #         {
                        #             "featureType": "arterial",
                        #             "elementType": "labels",
                        #             "stylers": {"visibility": "off"},
                        #         },
                        #         {
                        #             "featureType": "boundary",
                        #             "elementType": "geometry.fill",
                        #             "stylers": {"color": "#029fd4"},
                        #         },
                        #         {
                        #             "featureType": "building",
                        #             "elementType": "all",
                        #             "stylers": {"color": "#1a5787"},
                        #         },
                        #         {
                        #             "featureType": "label",
                        #             "elementType": "all",
                        #             "stylers": {"visibility": "off"},
                        #         },
                        #     ]
                        # },
                        )


        .add(
            time,
            c1,
            type_="scatter",  # scatter为散点图,heatmap为热力图,ChartType.EFFECT_SCATTER为涟漪图


            symbol_size=10,
            effect_opts=opts.EffectOpts(),
            label_opts=opts.LabelOpts(
                position="top",  # 标签位置
                is_show=True,  # is_show是否显示标签,点上面的内容
                formatter=JsCode(                  #formatter为标签内容格式器{a}:系列名;{b}:数据名;{c}:数值数组也可以是回调函数
                    """function(params) {
                    if ('value' in params.data) {
                        return params.data.value[2];
                    }
                }"""
                ),

            )



        )
        .set_global_opts(
            title_opts=opts.TitleOpts(
                title="图",

            ),
            tooltip_opts=opts.TooltipOpts(  # 标签配置,选中某一个点显示的框
                trigger="item",  # 触发类型,item主要用于散点图
                formatter=JsCode(  # 显示提示框formatter为标签内容格式器{a}:系列名;{b}:数据名;{c}:数值数组也可以是回调函数
                    """function(params) {
                    if ('value' in params.data) {
                        return params.data.name + '
' + params.data.value[2]; } }""" ), # 显示数据,可以去掉经纬度,只显示数值return params.data.value[2] + ': ' + params.data.value[0]+': ' + params.data.value[1]; ), visualmap_opts=opts.VisualMapOpts( # 视觉映射配置 pos_top="10", # 图例离顶部的距离 pos_left="10", # 图例离左边边框的距离 type_="size", # 映射过渡类型,可选,"color", "size" (颜色映射还是大小映射)(重要!!!) # 其中color参数使点的颜色随着数值进行变化,size参数使点的大小随着数值进行变化(变大变小) max_=300, # 指定 visualMapPiecewise 组件的最大值。 is_piecewise=True, # 是否分段 pieces=[ # 自定义的每一段的范围,以及每一段的文字,以及每一段的特别的样式 {"max": 149, "min": 20, "label": ">=120", "color": "#B40404"}, # label自定义的文字,color自定义的每一段的颜色 {"max": 20, "min": 10, "label": "90-119", "color": "#DF0101"}, {"max": 10, "min": 6, "label": "60-89", "color": "#F78181"}, {"max": 5, "min": 4, "label": "30-59", "color": "#F5A9A9"}, {"max": 3, "min": 1, "label": "1-29", "color": "#FFFFCC"}, {"value": 0, "label": '0', "color": 'grey'} # 专门为特定数据定义样式,例如给值为0的点定义灰色,但是0不能在上面的范围里,否则不显示定义的颜色 ] ), ) ) c.render(r"C:\Users\86130\Desktop\2Bmap1.html") path_wkimg = r'C:\Users\86130\Desktop\wkhtmltox-0.12.6-1.mxe-cross-win64\wkhtmltox\bin\wkhtmltoimage.exe' # 工具路径 cfg = imgkit.config(wkhtmltoimage=path_wkimg) # 1、将html文件转为图片 imgkit.from_file(r'C:\Users\86130\Desktop\2Bmap1.html', 'helloworld%s.jpg'%i, config=cfg)

你可能感兴趣的:(热力图制作,python)