1. pyechart绘制map地图
使用pyecharts可以轻松绘制中国地图和世界地图,具体步骤如下:
- 安装pyecharts和pyecharts中的地图插件
pip install pyecharts
pip install echarts-countries-pypkg # 世界地图插件
pip install echarts-china-provinces-pypkg # 中国省级地图插件
pip install echarts-china-cities-pypkg # 中国市级地图插件
- 导入pyecharts和需要的地图插件
from pyecharts import options as opts
from pyecharts.charts import Map
from pyecharts.globals import ChartType, ThemeType
from echarts_countries_pypkg import provinces # 世界地图插件
from echarts_china_provinces_pypkg import provinces # 中国省级地图插件
from echarts_china_cities_pypkg import cities # 中国市级地图插件
- 创建地图实例并设置地图样式
# 中国地图
map = Map(init_opts=opts.InitOpts(theme=ThemeType.LIGHT, width='1000px', height='600px'))
map.set_global_opts(
title_opts=opts.TitleOpts(title="中国地图", subtitle="地图示例"),
visualmap_opts=opts.VisualMapOpts(max_=200, is_piecewise=True),
)
# 世界地图
map = Map(init_opts=opts.InitOpts(theme=ThemeType.LIGHT, width='1000px', height='600px'))
map.set_global_opts(
title_opts=opts.TitleOpts(title="世界地图", subtitle="地图示例"),
visualmap_opts=opts.VisualMapOpts(max_=200, is_piecewise=True, range_color=['#CEF0D9','#006666']),
)
- 添加数据和标记点
# 中国地图
data_pair = [('北京', 50),('上海', 100),('广东', 80),('四川', 120),('新疆', 30),('西藏', 10),('内蒙古', 40),('云南', 60),('贵州', 50),('江苏', 90),('浙江', 70),('福建', 40),('湖南', 85),('湖北', 65),('重庆', 75),('天津', 30),('辽宁', 80),('吉林', 20),('山西', 25),('河南', 55),('河北', 45),('山东', 85),('江西', 35),('广西', 65),('海南', 5),('台湾', 20),('香港', 10),('澳门', 5)]
map.add("", data_pair, maptype="china")
# 世界地图
data_pair = [('China', 50), ('United States', 100), ('Russia', 80), ('Japan', 120), ('Germany', 30), ('India', 70), ('Australia', 40), ('United Kingdom', 60), ('France', 50), ('Canada', 90), ('Brazil', 75), ('South Africa', 35), ('Mexico', 65), ('Argentina', 85), ('Iran', 20), ('Sweden', 85), ('Norway', 25), ('Finland', 55), ('Italy', 45), ('Spain', 85), ('Portugal', 35), ('Greece', 65), ('Turkey', 5), ('Egypt', 20), ('Morocco', 10), ('Algeria', 5), ('Nigeria', 40), ('Ethiopia', 20), ('Kenya', 15), ('Tanzania', 30), ('DR Congo', 10), ('South Sudan', 8), ('Sudan', 18), ('Ukraine', 22), ('Poland', 24), ('Indonesia', 12), ('Philippines', 17), ('Vietnam', 14), ('Thailand', 27), ('Malaysia', 16), ('North Korea', 8), ('South Korea', 13), ('Pakistan', 11), ('Afghanistan', 6), ('Saudi Arabia', 26), ('Iraq', 21), ('Iran', 20), ('Syria', 8), ('Yemen', 5), ('Oman', 3), ('United Arab Emirates', 9), ('Qatar', 7), ('Kuwait', 7), ('Congo', 4), ('Gabon', 3), ('Cameroon', 3), ('Ivory Coast', 5), ('Madagascar', 4), ('Mali', 2), ('Niger', 2), ('Burkina Faso', 3), ('Zambia', 4), ('Zimbabwe', 2), ('Angola', 7), ('Mozambique', 2), ('Uzbekistan', 7), ('Kazakhstan', 7), ('Turkmenistan', 4), ('Afghanistan', 6), ('Pakistan', 11), ('India', 70), ('Nepal', 2), ('Bhutan', 1), ('Bangladesh', 5), ('Myanmar', 3), ('Laos', 3), ('Cambodia', 3), ('Malaysia', 16), ('Indonesia', 12), ('Philippines', 17), ('Australia', 40), ('New Zealand', 5)]
map.add("", data_pair, maptype="world")
# 标记点
mark_point = [{'name': '上海', 'value': 100}, {'name': '广州', 'value': 120}, {'name': '黑龙江省', 'value': 80}]
map.add("", data_pair, maptype="china", is_roam=True)
map.add(series_name="标记点", data_pair=mark_point, type_=ChartType.EFFECT_SCATTER, symbol_size=10)
- 显示图表
map.render('map.html')
使用pyecharts绘制地图非常简单,只需要几行代码就可以了。可以根据自己的需求来选择地图类型和样式,同时也可以添加标记点等元素以丰富地图内容。
赛文市场营销