pyecharts: how to plot scatter plots/heat maps on the map based on latitude, longitude and magnitude

created at 07-02-2021 views: 1

Function: Map the latitude and longitude to the map to view the distribution of the number of businesses near each latitude and longitude.

Mapping type: map + scatter plot, map + heat map

Type of application: Geo

py version: python2+, pyhton3+

ECharts is a pure Javascript chart library that can run smoothly on PCs and mobile devices and is compatible with most current browsers. The bottom layer relies on the lightweight Canvas library ZRender, which provides intuitive, vivid, interactive, and highly personalized Customized data visualization charts. ECharts provides regular line charts, histograms, scatter charts, pie charts, K-line charts, box charts for statistics, maps, heat maps, and line charts for geographic data visualization, and visualizations for relational data Relationship graphs, treemaps, parallel coordinates for multi-dimensional data visualization, funnel graphs and dashboards for BI, and support for mashups between graphs

Install packages

To use the map library, you need to install a third-party package (pip/pip3):

pip install echarts-countries-pypkg # Global country map
pip install echarts-cities-pypkg # Global city map
pip install echarts-china-provinces-pypkg # China provincial map
pip install echarts-china-cities-pypkg # China city-level map
pip install echarts-china-misc-pypkg
pip install echarts-united-kingdom-pypkg

Plot

from pyecharts.charts import Geo
from pyecharts import options as opts
from pyecharts.globals import GeoType


def test_geo():
    city = 'changsha'
    g = Geo()
    g.add_schema(maptype=city)

    # Define the name corresponding to the coordinate and add it to the coordinate library add_coordinate(name, lng, lat)
    g.add_coordinate('Outang, Hengshi Town, Ningxiang City, Changsha City, Hunan Province', 112.21369756169062, 28.211359706637378)
    g.add_coordinate('Xianfengling, Tiaoma Town, Yuhua District, Changsha City, Hunan Province', 113.16921879037058, 28.039877432448428)
    g.add_coordinate('Changsha Huanghua International Airport, Xintangpu, Huanghua Town, Changsha County, Changsha City, Hunan Province', 113.23212337884058, 28.19327497825815)

   # Define the data pair,
    data_pair = [('Xianfengling, Tiaoma Town, Yuhua District, Changsha City, Hunan Province', 10), ('Outang, Hengshi Town, Ningxiang City, Changsha City, Hunan Province', 5), ('Changsha Huanghua International Airport, Xintangpu, Huanghua Town, Changsha County, Changsha City, Hunan Province', 20)]

    # Geo map type, there are 4 kinds of scatter, effectScatter, heatmap, lines, it is recommended to use
    # from pyecharts.globals import GeoType
    # GeoType.GeoType.EFFECT_SCATTER,GeoType.HEATMAP,GeoType.LINES

   # Add data to the map
    g.add('', data_pair, type_=GeoType.EFFECT_SCATTER, symbol_size=5)
    # Set style
    g.set_series_opts(label_opts=opts.LabelOpts(is_show=False))
    # Custom segment color can use color picker to pick color
    pieces = [
        {'max': 1, 'label': '0以下', 'color': '#50A3BA'},
        {'min': 1, 'max': 10, 'label': '1-10', 'color': '#3700A4'},
        {'min': 10, 'max': 20, 'label': '10-20', 'color': '#81AE9F'},
        {'min': 20, 'max': 30, 'label': '20-30', 'color': '#E2C568'},
        {'min': 30, 'max': 50, 'label': '30-50', 'color': '#FCF84D'},
        {'min': 50, 'max': 100, 'label': '50-100', 'color': '#DD0200'},
        {'min': 100, 'max': 200, 'label': '100-200', 'color': '#DD675E'},
        {'min': 200, 'label': '200以上', 'color': '#D94E5D'}  # There is a lower limit and no upper limit
    ]
    # is_piecewise: Whether to customize the segmentation, it will take effect when it becomes true
    g.set_global_opts(
        visualmap_opts=opts.VisualMapOpts(is_piecewise=True, pieces=pieces),
        title_opts=opts.TitleOpts(title="{}-store distribution".format(city)),
    )
    return g

g = test_geo()
# Rendered into html, you can open it directly with a browser
g.render('test_render.html')

map

plot store distribution

Please log in to leave a comment.