Create Interactive Maps with pyecharts: A Step‑by‑Step Python Guide
This tutorial introduces pyecharts, the Python wrapper for Baidu's Echarts library, explains how it generates a render.html file, and provides complete code examples for drawing various map visualizations—including basic maps, maps without labels, visual‑mapped maps, world maps, and regional maps—while also showing how to replace the dummy data with your own dataset.
pyecharts is a Python library that wraps Baidu's open‑source JavaScript visualization library Echarts, allowing you to generate charts directly from Python code. After running the code, pyecharts creates a render.html file in the current directory, which can be opened in a browser to display the chart.
pyecharts can draw many chart types such as line charts, scatter plots, box plots, candlestick charts, and more. This article focuses on the official map demo.
Below are example results of the generated maps:
from pyecharts import options as opts
from pyecharts.charts import Map, Page
from pyecharts.faker import Collector, Faker
C = Collector()
@C.funcs
def map_base() -> Map:
c = (
Map()
.add("商家A", [list(z) for z in zip(Faker.provinces, Faker.values())], "china")
.set_global_opts(title_opts=opts.TitleOpts(title="Map-基本示例"))
)
return c
@C.funcs
def map_without_label() -> Map:
c = (
Map()
.add("商家A", [list(z) for z in zip(Faker.provinces, Faker.values())], "china")
.set_series_opts(label_opts=opts.LabelOpts(is_show=False))
.set_global_opts(title_opts=opts.TitleOpts(title="Map-不显示Label"))
)
return c
@C.funcs
def map_visualmap() -> Map:
c = (
Map()
.add("商家A", [list(z) for z in zip(Faker.provinces, Faker.values())], "china")
.set_global_opts(
title_opts=opts.TitleOpts(title="Map-VisualMap(连续型)"),
visualmap_opts=opts.VisualMapOpts(max_=200),
)
)
return c
@C.funcs
def map_visualmap_piecewise() -> Map:
c = (
Map()
.add("商家A", [list(z) for z in zip(Faker.provinces, Faker.values())], "china")
.set_global_opts(
title_opts=opts.TitleOpts(title="Map-VisualMap(分段型)"),
visualmap_opts=opts.VisualMapOpts(max_=3, is_piecewise=True),
)
)
return c
@C.funcs
def map_world() -> Map:
c = (
Map()
.add("商家A", [list(z) for z in zip(Faker.country, Faker.values())], "world")
.set_series_opts(label_opts=opts.LabelOpts(is_show=False))
.set_global_opts(
title_opts=opts.TitleOpts(title="Map-世界地图"),
visualmap_opts=opts.VisualMapOpts(max_=200),
)
)
return c
@C.funcs
def map_guangdong() -> Map:
c = (
Map()
.add("商家A", [list(z) for z in zip(Faker.guangdong_city, Faker.values())], "广东")
.set_global_opts(
title_opts=opts.TitleOpts(title="Map-广东地图"),
visualmap_opts=opts.VisualMapOpts(),
)
)
return c
Page().add(*[fn() for fn, _ in C.charts]).render()The above code uses synthetic data to draw six different maps.
To plot your own data, replace the .add line with something like:
.add("商家A", [["湖北",1],["广东",0]], "china")Also adjust the max parameter in opts.VisualMapOpts() accordingly.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
MaGe Linux Operations
Founded in 2009, MaGe Education is a top Chinese high‑end IT training brand. Its graduates earn 12K+ RMB salaries, and the school has trained tens of thousands of students. It offers high‑pay courses in Linux cloud operations, Python full‑stack, automation, data analysis, AI, and Go high‑concurrency architecture. Thanks to quality courses and a solid reputation, it has talent partnerships with numerous internet firms.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
