[Python Learning] Drawing Maps in the latest version of pyecharts

1. Download the pyecharts package in the command prompt:

pip install pyecharts

2. Some changes in the new version

In the new version of pyecharts, you cannot directly use from pyecharts import Map to reference the Map package. Instead, you need from pyecharts.charts import Map to reference the
Map constructor. In the old version: Map ("Kaifeng City Map", "Kaifeng"), new There have also been changes in the version.
The new version constructs Map variables: Map(), and then uses .add() to set the specific content.

3. Commonly used parameters of .add()

Map()
.add(series_name: str,##坐标系列名称(根据自己需要取名)
     data_pair: types.Sequence[types.Union[types.Sequence, opts.MapItem, dict]],##数据对,即(地区名,数量),例:(开封,100)
     maptype: str = "china"##地图类型,有world,国家名,省份名,市名四个等级
     )

4. Sample, quantitative map of Kaifeng urban area:

# 需要引用的库
from pyecharts import options as opts
from pyecharts.charts import Map

# 设置不同的系列,和系列中区域对应的数量值
pair_data1= [
	['龙亭区', 100],
	['顺河回族区', 200],
	['鼓楼区', 300],
	['禹王台区', 400],
	['祥符区',500]
]

pair_data2=[
	['杞县',100],
	['兰考县',200],
	['尉氏县',300],
	['通许县',400]
]

def create_map():
    ''' 
     作用:生成地图
    '''
    (   # 大小设置
        Map()
        .add(
            series_name="开封市市区", 
            data_pair=pair_data1, 
            maptype="开封"
        )
        .add(
            series_name="开封市县区", 
            data_pair=pair_data2, 
            maptype="开封"
        )
       
        # 全局配置项
        .set_global_opts(
            # 设置标题
            title_opts=opts.TitleOpts(title="开封地图"),
            # 设置标准显示
            visualmap_opts=opts.VisualMapOpts(max_=500, is_piecewise=False)
        )
        # 系列配置项
        .set_series_opts(
            # 标签名称显示,默认为True
            label_opts=opts.LabelOpts(is_show=True, color="blue")
        )
        # 生成本地html文件
        .render("省份地图.html")
    )

create_map()

5. For world-level maps, just set maptype='world', and then change the series of point names to country names.
For national-level maps, take China as an example, just set maptype='china', and then change the series of point names to province names
. For level-level maps, taking Henan as an example, you only need to set maptype='Henan', and then change the series of point names to city
names. For municipal-level maps, taking Kaifeng as an example, you only need to set maptype='Kaifeng', and then change the series of point names to District and county name

Guess you like

Origin blog.csdn.net/qq_45972928/article/details/124373889