Master Python Map Visualizations: Bokeh, GeoPandas, Plotly, Folium & More
This article introduces several Python libraries for creating geographic visualizations, demonstrates how to draw world and China maps with Bokeh, GeoPandas, Plotly, Cartopy/Basemap, Folium, and PyEcharts, and compares their ease of use, flexibility, and visual quality.
Today we explore a collection of Python libraries for drawing maps, from quick‑start tools to advanced visualisation frameworks, and show examples of rendering both world and China maps.
Bokeh
Bokeh can create basic map visualisations and handle geographic data.
from bokeh.plotting import figure, show
from bokeh.tile_providers import CARTODBPOSITRON, get_provider
from bokeh.io import output_notebook
output_notebook()
tile_provider = get_provider(CARTODBPOSITRON)
p = figure(x_range=(-2000000, 6000000), y_range=(-1000000, 7000000),
x_axis_type="mercator", y_axis_type="mercator")
p.add_tile(tile_provider)
show(p)Another example draws a map of China using a GeoJSON file.
from bokeh.plotting import curdoc, figure
from bokeh.models import GeoJSONDataSource
from bokeh.io import show
import json, random
with open("china.json", encoding="utf8") as f:
geo_source = GeoJSONDataSource(geojson=f.read())
p = figure(width=500, height=500)
p.patches(xs='xs', ys='ys', source=geo_source)
show(p)GeoPandas
GeoPandas extends Pandas with geographic data structures, making map drawing straightforward.
import pandas as pd
import geopandas as gpd
import matplotlib.pyplot as plt
%matplotlib inline
world = gpd.read_file(gpd.datasets.get_path('naturalearth_lowres'))
world.plot()
plt.show()Adding the nine‑dash line to a China map:
china_nine = gpd.read_file(r"geojson/九段线GS(2019)1719号.geojson")
china = gpd.read_file('china-new.json')
fig, ax = plt.subplots(figsize=(12, 8), dpi=80)
ax = china.plot(ax=ax, column='number')
ax = china_nine.plot(ax=ax)
plt.show()Plotly
Plotly offers interactive map visualisations. First install the required packages, then create a simple world map.
!pip install geopandas==0.3.0
!pip install pyshp==1.2.10
!pip install shapely==1.6.3 import plotly.graph_objects as go
fig = go.Figure(go.Scattermapbox(
mode="markers+lines",
lon=[10, 20, 30],
lat=[10, 20, 30],
marker={'size': 10}
))
fig.update_layout(
margin={'l':0,'t':0,'b':0,'r':0},
mapbox={'center':{'lon':113.65,'lat':34.76667},'style':'stamen-terrain','zoom':1}
)
fig.show()A choropleth map of China using Plotly Express:
import pandas as pd, plotly.express as px, json, numpy as np
with open(r"china_province.geojson", encoding='utf8') as f:
provinces_map = json.load(f)
df = pd.read_csv(r'data.csv')
df.确诊 = df.确诊.map(np.log)
fig = px.choropleth_mapbox(df, geojson=provinces_map, color='确诊',
locations='地区', featureidkey='properties.NL_NAME_1',
mapbox_style='carto-darkmatter', color_continuous_scale='viridis',
center={'lat':37.110573,'lon':106.493924}, zoom=3)
fig.show()Cartopy / Basemap
Cartopy builds on Matplotlib, PROJ.4, NumPy and Shapely to produce high‑quality maps.
%matplotlib inline
import cartopy.crs as ccrs
import matplotlib.pyplot as plt
ax = plt.axes(projection=ccrs.PlateCarree())
ax.coastlines()
plt.show()Adding night‑shade shading:
import datetime
import matplotlib.pyplot as plt
import cartopy.crs as ccrs
from cartopy.feature.nightshade import Nightshade
fig = plt.figure(figsize=(10,5))
ax = fig.add_subplot(1,1,1, projection=ccrs.PlateCarree())
date = datetime.datetime(2021,12,2,21)
ax.set_title(f'Night time shading for {date}')
ax.stock_img()
ax.add_feature(Nightshade(date, alpha=0.2))
plt.show()Drawing a detailed China map with Cartopy requires loading shapefiles and customizing colors, as shown in the original code.
Folium
Folium combines Python data handling with Leaflet.js to create interactive web maps.
import folium
world_map = folium.Map()
world_mapCreating a choropleth of China:
import json, pandas as pd
with open('china_province.geojson', encoding='utf8') as f:
china = json.load(f)
chn_map = folium.Map(location=[40,100], zoom_start=4)
folium.Choropleth(
geo_data=china,
name="choropleth",
data=df,
columns=["地区","确诊"],
key_on="properties.NL_NAME_1",
fill_color="YlGn",
fill_opacity=0.7,
line_opacity=0.2,
legend_name="新冠确诊"
).add_to(chn_map)
folium.LayerControl().add_to(chn_map)
chn_mapPyEcharts
PyEcharts is a Chinese‑origin visualisation library that can render maps without handling GeoJSON files.
from pyecharts import options as opts
from pyecharts.charts import Map
from pyecharts.faker import Faker
c = (
Map()
.add("测试数据", [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),
)
)
c.render_notebook()For a China map, simply change the map type to "china" and adjust visual map settings.
c = (
Map()
.add("测试数据", [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, is_piecewise=True),
)
)
c.render_notebook()Overall, PyEcharts offers the simplest syntax for beginners, while Folium and Cartopy provide greater flexibility, and Plotly and Bokeh deliver higher visual quality with richer APIs.
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.
Python Crawling & Data Mining
Life's short, I code in Python. This channel shares Python web crawling, data mining, analysis, processing, visualization, automated testing, DevOps, big data, AI, cloud computing, machine learning tools, resources, news, technical articles, tutorial videos and learning materials. Join us!
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.
