Python pyecharts real-time drawing custom visualization latitude and longitude heat map

Table of contents
  • background
  • Heat map based on pyecharts built-in latitude and longitude
  • Heat map based on custom latitude and longitude
  • Disadvantages of pyecharts library
  • The difference between different map coordinate systems
    • WGS-84 - World Geodetic System
    • GCJ-02 - National Bureau of Survey Coordinates
    • BD-09 - Baidu Coordinate System

background

The analysis of provinces and regions is basically involved in the statistical analysis of business data. Data visualization is a powerful tool for data analysis. The data of these provinces and regions are generally visualized with maps, so that some laws can be found clearly.

There are many visualization types of maps, such as: basic geographic map, heat map, path map, ripple map, etc. This article mainly introduces  heat map , and the tool used is Baidu open source  pyecharts

The simulated data takes the popularity of national tourist attractions during the National Eleventh as an example (fictitious data)

simulated data

Heat map based on pyecharts built-in latitude and longitude

Pyecharts comes with the latitude and longitude of some cities. When drawing a picture, you only need to list the name of the city or province, and it will be automatically displayed on the map. pyecharts will automatically extract the latitude and longitude according to the name of the city or province

After installing the pyecharts package, you can find the corresponding files in the pyecharts package folder city_coordinates.json, which saves a large number of geographic names and latitude and longitude information.
The general path is as follows: xxxxxx\Lib\site-packages\pyecharts\datasets\city_coordinates.json

城市Next, use the middle and column of simulated data  热度 to visualize the heat map.
Swipe left and right to view the complete code

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

import pandas as pd

from pyecharts import options as opts

from pyecharts.charts import BMap

from pyecharts.globals import BMapType

import json

data=pd.read_excel('热力图模拟数据.xlsx')

hotmap = (

    BMap(is_ignore_nonexistent_coord=True,    #忽略不存在的坐标

         init_opts=opts.InitOpts(width="1300px", height="600px"))

    .add_schema(baidu_ak="自己申请的key", center=[120.13066322374, 30.240018034923],

                zoom=5,   # 当前视角的缩放比例

                is_roam=True   # 是否开启鼠标缩放和平移漫游

               

    .add(

        "热度"#图例

        data_pair=[list(z) for z in zip(data['城市'].to_list(), data['热度'].to_list())],

        type_="heatmap",

        label_opts=opts.LabelOpts(formatter="{b}"),

    )

    .set_global_opts(

        title_opts=opts.TitleOpts(title="十一期间全国旅游景点热度",

                                  pos_left='center',

                                  title_textstyle_opts=opts.TextStyleOpts(font_size=32)

                                 ),

        legend_opts=opts.LegendOpts(pos_right='20%'),

        visualmap_opts=opts.VisualMapOpts()

    )

    .add_control_panel(

        copyright_control_opts=opts.BMapCopyrightTypeOpts(position=3),

        maptype_control_opts=opts.BMapTypeControlOpts(

            type_=BMapType.MAPTYPE_CONTROL_DROPDOWN

        ),

        scale_control_opts=opts.BMapScaleControlOpts(),

        overview_map_opts=opts.BMapOverviewMapControlOpts(is_open=True),

        navigation_control_opts=opts.BMapNavigationControlOpts(),

        geo_location_control_opts=opts.BMapGeoLocationControlOpts(),

    )

    .render("基于pyecharts内置经纬度的热力图.html")

)

#hotmap.render_notebook()

Built-in latitude and longitude

Heat map based on custom latitude and longitude

Because pyecharts  city_coordinates.json stores some commonly used geographic latitude and longitude, if you want to use custom latitude and longitude, pyecharts also supports

下面用模拟数据中 经度维度热度 列来进行热力图可视化

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

import pandas as pd

from pyecharts import options as opts

from pyecharts.charts import BMap

from pyecharts.globals import BMapType

import json

data=pd.read_excel('热力图模拟数据.xlsx')

data_json={}

for index,row in data.iterrows():

    data_json[row['地名']]=[float(row['经度']),float(row['维度'])]

with open("BMAP.json","w") as f:

    json.dump(data_json,f)

hotmap = (

    BMap(is_ignore_nonexistent_coord=True,    #忽略不存在的坐标

         init_opts=opts.InitOpts(width="1300px", height="600px"))

    .add_schema(baidu_ak="自己申请的key", center=[120.13066322374, 30.240018034923],

                zoom=5,   # 当前视角的缩放比例

                is_roam=True   # 是否开启鼠标缩放和平移漫游

               )

    .add_coordinate_json("BMAP.json"#加载自定义坐标

    .add(

        "热度"#图例

        data_pair=[list(z) for z in zip(data['地名'].to_list(), data['热度'].to_list())],

        type_="heatmap",

        label_opts=opts.LabelOpts(formatter="{b}"),

    )

    .set_global_opts(

        title_opts=opts.TitleOpts(title="十一期间全国旅游景点热度",

                                  pos_left='center',

                                  title_textstyle_opts=opts.TextStyleOpts(font_size=32)

                                 ),

        legend_opts=opts.LegendOpts(pos_right='20%'),

        visualmap_opts=opts.VisualMapOpts(max_=20#设置最大值,目的是为了能够精确查看自定坐标位置

    )

    .add_control_panel(

        copyright_control_opts=opts.BMapCopyrightTypeOpts(position=3),

        maptype_control_opts=opts.BMapTypeControlOpts(

            type_=BMapType.MAPTYPE_CONTROL_DROPDOWN

        ),

        scale_control_opts=opts.BMapScaleControlOpts(),

        overview_map_opts=opts.BMapOverviewMapControlOpts(is_open=True),

        navigation_control_opts=opts.BMapNavigationControlOpts(),

        geo_location_control_opts=opts.BMapGeoLocationControlOpts(),

    )

    .render("基于自定义经纬度的热力图.html")

)

#hotmap.render_notebook()

自定义经纬度

pyecharts库缺点

没有现成的方法用来直接导入自定义坐标,需要先把自定义坐标写在json文件中,然后再通过加载文件实现导入,而没有一个直接导入自定义坐标的方法,这个可以从源码中看出来,如果有一个 add_coordinate_dict 函数就完美了

缺点

不同地图坐标系区别

我们通常用经纬度来表示一个地理位置,但是由于一些原因,我们从不同渠道得到的经纬度信息可能并不是在同一个坐标系下。

  • 高德地图、腾讯地图以及谷歌中国区地图使用的是GCJ-02坐标系
  • 百度地图使用的是BD-09坐标系
  • 底层接口(HTML5 Geolocation或ios、安卓API)通过GPS设备获取的坐标使用的是WGS-84坐标系

不同的坐标系之间可能有几十到几百米的偏移,所以在开发基于地图的产品,或者做地理数据可视化时,我们需要修正不同坐标系之间的偏差。

WGS-84 - 世界大地测量系统

WGS-84(World Geodetic System, WGS)是使用最广泛的坐标系,也是世界通用的坐标系,GPS设备得到的经纬度就是在WGS84坐标系下的经纬度。通常通过底层接口得到的定位信息都是WGS84坐标系

GCJ-02 - 国测局坐标

GCJ-02(G-Guojia国家,C-Cehui测绘,J-Ju局),又被称为火星坐标系,是一种基于WGS-84制定的大地测量系统,由中国国测局制定。此坐标系所采用的混淆算法会在经纬度中加入随机的偏移。

The state stipulates that all public geographical data in mainland China must be encrypted with at least GCJ-02, which means that the data we get from the products of domestic companies must be encrypted. Most domestic Internet map providers use the GCJ-02 coordinate system, including AutoNavi Maps, Google Maps China, etc.

BD-09 - Baidu Coordinate System

BD-09 (Baidu, BD) is the geographic coordinate system used by Baidu Maps, which adds an additional transformation to GCJ-02 to protect user privacy. The coordinates obtained from Baidu products are all in the BD-09 coordinate system

Guess you like

Origin blog.csdn.net/qq_15509251/article/details/131536035