pyecharts从入门到精通-地图专题GEO-世界地图和中国城市地图

参考

官方文档:https://pyecharts.org/#/zh-cn/quickstart
Python使用Pyecharts画Geo地图,如何显示具体地点名称
pyecharts世界地图用:国家中英文对照表.xlsx
Map中的地理坐标问题

安装与查看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:百度地图

完成Geo地理坐标系实现。

0、导入相关模块
1、首先是实例化对象:可传入图表宽度、名称、背景颜色
2、add_schema() :传入要显示的区域名称、是否显示下辖区的名称、图表颜色、边界线颜色
3、add(): 传入图例名称、数据(二级列表)、图类型(ChartType.EFFECT_SCATTER动态散点
图,ChartType.HEATMAP热力图,scatter, effectScatter, heatmap, lines)
4、set_series_opts() :是否显示每个区域的数据大小
5、set_global_opts() : 设置图标标题
6、render_notebook() : 在notebook中渲染显示图表

案例演示

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()

在这里插入图片描述

拓展-pyecharts中GEO源码

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 新增一个坐标点

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

    # 经度
    longitude: Numeric,

    # 纬度
    latitude: Numeric,
)

func pyecharts.charts.Geo.add_coordinate_json
以 JOSN 文件格式新增多个坐标点

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

func pyecharts.charts.Geo.get_coordinate
查询指定地点的坐标

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

Geo 图的坐标引用自 pyecharts.datasets.COORDINATES,COORDINATES 是一个支持模糊匹配的字典类。可设置匹配的阈值。

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,
)

在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/m0_38139250/article/details/130216535
今日推荐