地图绘制入门#
备注
为了保证 Sphinx 渲染输出 HTML,需要创建空的 go.FigureWidget
。
from plotly import graph_objects as go
go.FigureWidget(); # 用于 Sphinx 初始化渲染
基础地图 WGS84#
基础数据:
data = []
scattermapbox = go.Scattermapbox()
data.append(scattermapbox)
布局:
mapbox_kargs = {'zoom': 10,
'center': {'lat': 30.272934, 'lon': 120.147376},
'style': "open-street-map",
'accesstoken': '!!! replace with your mapbox ak !!!'}
layout_kargs = {'autosize': False,
'width': 1000,
'height': 600,
'margin': {'r': 0, 't': 0, 'l': 0, 'b': 0, 'pad': 0}}
layout = go.Layout(
mapbox=mapbox_kargs,
**layout_kargs
)
渲染:
fig = go.Figure(
data=data,
layout=layout
)
fig
可选的地图样式:(无需 ak)"open-street-map"
,"carto-positron"
,"stamen-terrain"
等 ;(需要 ak)"basic"
,"streets"
,"outdoors"
等。
使用高德地图 GCJ-02#
import plotly.graph_objects as go
data = []
scattermapbox = go.Scattermapbox()
data.append(scattermapbox)
basemap_layer = [{'below': 'traces',
'sourcetype': 'raster',
'sourceattribution': '高德地图',
'source': ['http://wprd01.is.autonavi.com/appmaptile?x={x}&y={y}&z={z}&lang=zh_cn&size=1&scl=1&style=7']}]
mapbox_kargs = {'zoom': 10,
'center': {'lat': 30.272934, 'lon': 120.147376},
'style': 'white-bg',
'layers': basemap_layer}
layout_kargs = {'autosize': False,
'width': 1000,
'height': 500,
'margin': {'r': 0, 't': 38, 'l': 0, 'b': 0, 'pad': 0}}
layout = go.Layout(
mapbox=mapbox_kargs,
**layout_kargs
)
fig = go.Figure(
data=data,
layout=layout
)
fig
使用天地图 CGCS2000#
import plotly.graph_objects as go
data = []
scattermapbox = go.Scattermapbox()
data.append(scattermapbox)
tk = "!!! replace with your tianditu tk !!!"
layer = "vec"
basemap_layer = [{'below': 'traces',
'sourcetype': 'raster',
'sourceattribution': '天地图',
'source': ['https://t0.tianditu.gov.cn/vec_w/wmts?SERVICE=WMTS&REQUEST=GetTile&VERSION=1.0.0&LAYER=vec&STYLE=default&TILEMATRIXSET=w&FORMAT=tiles&TILECOL={x}&TILEROW={y}&TILEMATRIX={z}&tk=!!! replace with your tianditu tk !!!']},
{'below': 'traces',
'sourcetype': 'raster',
'source': ['https://t3.tianditu.gov.cn/cva_w/wmts?SERVICE=WMTS&REQUEST=GetTile&VERSION=1.0.0&LAYER=cva&STYLE=default&TILEMATRIXSET=w&FORMAT=tiles&TILECOL={x}&TILEROW={y}&TILEMATRIX={z}&tk=!!! replace with your tianditu tk !!!']}]
mapbox_kargs = {
'zoom': 10,
'center': {'lat': 30.272934, 'lon': 120.147376},
'style': 'carto-positron',
'layers': basemap_layer
}
layout_kargs = {'autosize': False,
'width': 1200,
'height': 500,
'margin': {'r': 0, 't': 38, 'l': 0, 'b': 0, 'pad': 0}}
layout = go.Layout(
mapbox=mapbox_kargs,
**layout_kargs
)
fig = go.Figure(
data=data,
layout=layout
)
fig