Mapbox 是新一代的地圖資料,足以媲美 Google Map,免費額度相當的多,可以完全取代Google 地圖。
Mapbox圖層
Mapbox共分為三個圖層
1. 底層 : base map,就是繪制地圖的地方。由 layout.mapbox.style定義
2. 軌跡層 : trace,繪製軌跡圖層,位於底層上方
3. 其它圖層 : 位於軌跡層上方,由 layout.mapbox.layers 定義更多圖層,為陣列格式。
存取權杖
plotly-express 及 plotly 新的版本已經不再支援需要權杖的地圖樣式,所以不用申請權杖。
不過還是可到官網 https://mapbox.com/ 註冊帳號。
OpenStreetMap
底下為簡易範例
import pandas as pd import plotly us_cities = pd.read_csv("https://raw.githubusercontent.com/plotly/datasets/master/us-cities-top-1k.csv") import plotly.express as px fig = px.scatter_map(us_cities, lat="lat", lon="lon", hover_name="City", hover_data=["State", "Population"], color_discrete_sequence=["fuchsia"], zoom=4) fig.update_layout(mapbox_style="open-street-map") fig.update_layout(margin={"r":0,"t":0,"l":0,"b":0}) plotly.offline.plot(fig,filename="plotly30.html", auto_open=False) fig.show()
COVID-19分佈圖
上面的資料,是plotly官方提供的,不太適合台灣學生的需求。所以本人提供一組台灣地圖的經緯度給大家測試,不過裏面的資料是假的啦,共100 多組經緯度。
請注意,註明編碼可用如下二種。不過本例已改成由資料庫提取,所以不需加此編碼方式。
# -*- coding:utf-8 -*-
或
# coding=utf-8
import pandas as pd
import plotly.express as px
import mysql.connector as mysql
conn=mysql.connect(host="mahaljsp.asuscomm.com",
user="xxx",
password="xxxxx",
database="資料庫")
cursor=conn.cursor()
cursor.execute("select * from covid19")
rs=cursor.fetchall()
columns=[d[0] for d in cursor.description]
df=pd.DataFrame(data=rs, columns=columns)
fig=px.scatter_map(
data_frame=df,
lat="lat",
lon="lng",
hover_name="cov_date",
hover_data=["area","address"],
color_discrete_sequence=["green"],
zoom=12
)
#open-street-map : 免費,不用權杖,較不美觀
fig.update_traces(marker=dict(size=13))
fig.update_layout(mapbox_style='open-street-map')
fig.update_layout(margin=dict(l=0, t=0, r=0, b=0))
plotly.offline.plot(fig,filename="plotly31.html", auto_open=False)
fig.show()
plotly map
plotly 的 go.Scattermapbox 可以支援有權杖的 “streets” 地圖,但有 bug,無法縮放地圖,而且已被棄用。所以要改用 go.Scattermap,不過不支援需權杖的 “streets”。看來 plotly 已放棄需要權杖的地圖樣式了。
完整代碼及範例如下
import pandas as pd import mysql.connector as mysql import plotly import plotly.graph_objects as go pd.set_option('display.max_columns', None) pd.set_option('display.max_rows', None) pd.set_option('display.width', None) pd.set_option('display.max_colwidth', None) conn=mysql.connect(host="ip", user='帳號', password='密碼', database='cloud') cursor=conn.cursor() cursor.execute("select * from covid19") rs=cursor.fetchall() columns = [d[0] for d in cursor.description] df=pd.DataFrame(data=rs, columns=columns) lat = df["lat"] lon = df["lng"] hover_text = df["area"] + "
" + df["address"] fig = go.Figure(go.Scattermap( lat=lat, lon=lon, mode='markers', text=hover_text, hoverinfo='text' )) fig.update_layout( map=dict( style="satellite", # 自訂樣式 zoom=12, center=dict(lat=lat.mean(), lon=lon.mean()) ), margin={"r":0,"t":0,"l":0,"b":0} ) fig.update_traces(marker={"size":13, "color":"red"}) plotly.offline.plot(fig,filename="plotly32.html", auto_open=False) fig.show()
底下的地圖,是不是精美多了呢!! 比較麻煩的是要申請權杖,但是免費的!!
有關可使用的圖示請參照官網的說明 https://labs.mapbox.com/maki-icons/。官網所列出的圖示,有些能用,有些不能用。
''' airfield airport alcohol-shop amusement-park aquarium art-gallery attraction bakery bank bar beer bicycle bicycle-share bus cafe campsite car castle cemetery cinema circle circle-stroked clothing-store college dentist doctor dog-park drinking-water embassy entrance fast-food ferry fire-station fuel garden golf grocery harbor heliport hospital ice-cream information laundry library lodging marker monument mountain museum music park pharmacy picnic-site place-of-worship playground police post prison rail rail-light rail-metro religious-christian religious-jewish religious-muslim restaurant rocket school shop stadium star suitcase swimming theatre toilet town-hall triangle triangle-stroked veterinary volcano zoo '''