pyecharts from entry to mastery - map theme GEO - world map and Chinese city map

reference

Official document: https://pyecharts.org/#/zh-cn/quickstart
Python uses Pyecharts to draw Geo maps, how to display specific location names
pyecharts world map: country Chinese-English comparison table.
Geographical coordinates in xlsx Map

Install and view pyecharts

Install pyecharts

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

view version

import pyecharts
print(pyecharts.__version__) 
# 2.0.3

Map implementation-Geo

The map implementation of pyecharts includes:

Geo: geographic coordinate system
Map: map
BMap: Baidu map

Complete the implementation of the Geo geographic coordinate system.

0. Import related modules
1. First, instantiate the object: you can pass in the chart width, name, and background color
2. add_schema(): pass in the name of the area to be displayed, whether to display the name of the jurisdiction, the color of the chart, and the color of the boundary line
3. add(): Pass in legend name, data (secondary list), graph type (ChartType.EFFECT_SCATTER dynamic scatter
diagram, ChartType.HEATMAP heat map, scatter, effectScatter, heatmap, lines)
4. set_series_opts(): Whether Display the data size of each area
5, set_global_opts(): Set the icon title
6, render_notebook(): Render and display the chart in the notebook

Case presentation

from pyecharts import options as opts
from pyecharts.charts import Geo
from pyecharts.faker import Faker
from pyecharts.globals import ChartType

c = (
    Geo()
    .add_schema(maptype="广东")
    .add(
        "geo",
        [list(z) for z in zip(Faker.guangdong_city, Faker.values())],
        type_=ChartType.HEATMAP,
    )
    .add(
        "geo",
        [list(z) for z in zip(Faker.provinces, Faker.values())],
        type_=ChartType.EFFECT_SCATTER,
    )
    .add(
        "",
        [("汕头市", 55), ("汕尾市", 66), ("广州市", 77), ("惠州市", 88)],
        type_=ChartType.EFFECT_SCATTER,
        color="white",
    )
    .add(
        "geo",
        [("汕头市", "汕尾市"), ("广州市", "惠州市"), ("汕头市", "广州市"), ("广州市", "肇庆市")],
        type_=ChartType.LINES,
        effect_opts=opts.EffectOpts(
            symbol=SymbolType.ARROW, symbol_size=6, color="blue"
        ),
        linestyle_opts=opts.LineStyleOpts(curve=0.2),
    )
    .set_series_opts(label_opts=opts.LabelOpts(is_show=False))
    .set_global_opts(
        visualmap_opts=opts.VisualMapOpts(), title_opts=opts.TitleOpts(title="Geo-广东地图")
    )

)
c.render_notebook()

insert image description here

Expand-GEO source code in pyecharts

class pyecharts.charts.Geo

class Geo(
    # 初始化配置项,参考 `global_options.InitOpts`
    init_opts: opts.InitOpts = opts.InitOpts()

    # 是否忽略不存在的坐标,默认值为 False,即不忽略
    is_ignore_nonexistent_coord: bool = False
)

func pyecharts.charts.Geo.add_schema

def add_schema(
    # 地图类型,具体参考 pyecharts.datasets.map_filenames.json 文件
    maptype: str = "china",

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

    # 当前视角的缩放比例。默认为 1
    zoom: Optional[Numeric] = None,

    # 当前视角的中心点,用经纬度表示。例如:center: [115.97, 29.71]
    center: Optional[Sequence] = None,

    # 参数用于 scale 地图的长宽比。
    aspect_scale: types.Numeric = 0.75,

    # 二维数组,定义定位的左上角以及右下角分别所对应的经纬度。
    bounding_coords: types.Optional[types.Sequence[types.Numeric]] = None,

    # 最小的缩放值。
    min_scale_limit: types.Optional[types.Numeric] = None,

    # 最大的缩放值。
    max_scale_limit: types.Optional[types.Numeric] = None,

    # 默认是 'name',针对 GeoJSON 要素的自定义属性名称,作为主键用于关联数据点和 GeoJSON 地理要素。
    name_property: str = "name",

    # 选中模式,表示是否支持多个选中,默认关闭,支持布尔值和字符串。
    # 字符串取值可选'single'表示单选,或者'multiple'表示多选。
    selected_mode: types.Union[bool, str] = False,

    # pyecharts 暂时没有提供 left/top/right/bottom 的配置
    # layoutCenter 和 layoutSize 提供了除了 left/right/top/bottom/width/height 之外的布局手段。
    # 在使用 left/right/top/bottom/width/height 的时候
    # 可能很难在保持地图高宽比的情况下把地图放在某个盒形区域的正中间,并且保证不超出盒形的范围。
    # 此时可以通过 layoutCenter 属性定义地图中心在屏幕中的位置,layoutSize 定义地图的大小。
    # 如下示例
    # layoutCenter: ['30%', '30%'],
    # // 如果宽高比大于 1 则宽度为 100,如果小于 1 则高度为 100,保证了不超过 100x100 的区域
    # layoutSize: 100
    layout_center: types.Optional[types.Sequence[str]] = None,

    # 地图的大小,见 layoutCenter。支持相对于屏幕宽高的百分比或者绝对的像素大小。
    layout_size: types.Union[str, types.Numeric] = None,

    # # 标签配置项,参考 `series_options.LabelOpts`
    label_opts: Union[opts.LabelOpts, dict, None] = None,

    # 地图区域的多边形 图形样式。
    itemstyle_opts: Union[opts.ItemStyleOpts, dict, None] =None,

    # 高亮状态下的多边形样式
    emphasis_itemstyle_opts: Union[opts.ItemStyleOpts, dict,None] = None,

    # 高亮状态下的标签样式。
    emphasis_label_opts: Union[opts.LabelOpts, dict, None] =None,

    # 在地图中对特定的区域配置样式。具体配置参考 `charts_options.GeoRegionsOpts`
    regions_opts: types.Union[types.Sequence[types.GeoRegions], types.Sequence[dict]] = None,
):

func pyecharts.charts.Geo.add

def add(
    # 系列名称,用于 tooltip 的显示,legend 的图例筛选。
    series_name: str,

    # 数据项 (坐标点名称,坐标点值)
    data_pair: Sequence,

    # Geo 图类型,有 scatter, effectScatter, heatmap, lines 4 种,建议使用
    # from pyecharts.globals import GeoType
    # GeoType.GeoType.EFFECT_SCATTER,GeoType.HEATMAP,GeoType.LINES
    type_: str = "scatter",

    # 是否选中图例
    is_selected: bool = True,

    # 标记图形形状
    symbol: Optional[str] = None,

    # 标记的大小
    symbol_size: Numeric = 12,

    # 每个点的大小,在地理坐标系(coordinateSystem: 'geo')上有效。
    blur_size: types.Numeric = 20,

    # 每个点模糊的大小,在地理坐标系(coordinateSystem: 'geo')上有效。
    point_size: types.Numeric = 20,

    # 系列 label 颜色
    color: Optional[str] = None,

    # 是否是多段线,在画 lines 图情况下
    is_polyline: bool = False,

    # 是否启用大规模线图的优化,在数据图形特别多的时候(>=5k)可以开启
    is_large: bool = False,

    # 特效尾迹的长度。取从 0 到 1 的值,数值越大尾迹越长。默认值 0.2
    trail_length: Numeric = 0.2,

    # 开启绘制优化的阈值。
    large_threshold: Numeric = 2000,

    # 配置该系列每一帧渲染的图形数
    progressive: types.Numeric = 400,

    # 启用渐进式渲染的图形数量阈值,在单个系列的图形数量超过该阈值时启用渐进式渲染。
    progressive_threshold: types.Numeric = 3000,

    # 标签配置项,参考 `series_options.LabelOpts`
    label_opts: Union[opts.LabelOpts, dict] = opts.LabelOpts(),

    # 涟漪特效配置项,参考 `series_options.EffectOpts`
    effect_opts: Union[opts.EffectOpts, dict] = opts.EffectOpts(),

    # 线样式配置项,参考 `series_options.LineStyleOpts`
    linestyle_opts: Union[opts.LineStyleOpts, dict] = opts.LineStyleOpts(),

    # 提示框组件配置项,参考 `series_options.TooltipOpts`
    tooltip_opts: Union[opts.TooltipOpts, dict, None] = None,

    # 图元样式配置项,参考 `series_options.ItemStyleOpts`
    itemstyle_opts: Union[opts.ItemStyleOpts, dict, None] = None,

    # 这个配置相对非常复杂(参照地址: https://www.echartsjs.com/zh/option.html#series-custom.renderItem)
    render_item: types.JsCode = None,

    # 这个配置相对非常复杂(参照地址: https://www.echartsjs.com/zh/option.html#series-custom.encode)
    encode: types.Union[types.JsCode, dict] = None,
)

func pyecharts.charts.Geo.add_coordinate add a coordinate point

def add_coordinate(
    # 坐标地点名称
    name: str,

    # 经度
    longitude: Numeric,

    # 纬度
    latitude: Numeric,
)

func pyecharts.charts.Geo.add_coordinate_json
adds multiple coordinate points in JOSN file format

def add_coordinate_json(
    # json 文件格式的坐标数据
    # 格式如下
    # {
    
    
    #   "阿城": [126.58, 45.32],
    #   "阿克苏": [80.19, 41.09]
    # }
  ],
    json_file: str
)

func pyecharts.charts.Geo.get_coordinate
query the coordinates of the specified location

def get_coordinate(
    # 地点名称
    name: str
) -> Sequence

The coordinates of the Geo map are referenced from pyecharts.datasets.COORDINATES, COORDINATES is a dictionary class that supports fuzzy matching. The matching threshold can be set.

from pyecharts.datasets import COORDINATES
# cutoff 为匹配阈值,阈值越高相似性越高,1 为完全相同。默认为 0.6
COORDINATES.cutoff = 0.75

func pyecharts.options.GeoRegionsOpts

class GeoRegionsOpts(
    # 地图区域的名称,例如 '广东','浙江'。
    name: Optional[str] = None,

    # 该区域是否选中。
    is_selected: bool = False,

    # 该区域的多边形样式设置。
    itemstyle_opts: Union[ItemStyleOpts, dict, None] = None,

    # 图形上的文本标签,可用于说明图形的一些数据信息,比如值,名称等。
    label_opts: Union[LabelOpts, dict, None] = None,

    # 高亮状态的样式设置。
    emphasis_itemstyle_opts: Union[ItemStyleOpts, dict, None] = None,

    # 高亮状态的标签设置。
    emphasis_label_opts: Union[LabelOpts, dict, None] = None,

    # 选中状态的样式设置。
    select_itemstyle_opts: Union[ItemStyleOpts, dict, None] = None,

    # 选中状态的标签设置。
    select_label_opts: Union[LabelOpts, dict, None] = None,

    # 淡出状态的样式设置。
    blur_itemstyle_opts: Union[ItemStyleOpts, dict, None] = None,

    # 淡出状态的标签设置。
    blur_label_opts: Union[LabelOpts, dict, None] = None,

    # 本 region 中特定的 tooltip 设定。
    tooltip_opts: Union[TooltipOpts, dict, None] = None,

    # 图形是否不响应和触发鼠标事件,默认为 false,即响应和触发鼠标事件。
    is_silent: bool = False,
)

insert image description here

Guess you like

Origin blog.csdn.net/m0_38139250/article/details/130216535
map
Map
Map
map