Wenyan Deng
Ph.D. Candidate
Massachusetts Institute of Technology
Plotly and Mapbox: Scatter Maps
August 3, 2017
The case study
This example uses a sample of Sri Lankan police station data to map which police stations were attacked by rebel groups in the 1971 revolt. The codes for this map are simple but useful for small amounts of data. The final product is here: https://plot.ly/~wdeng1/165.embed. You can also see my codes on GitHub.
Mapping
Getting ready:
import plotly.plotly as py
from plotly.graph_objs import *
mapbox_access_token = '...' # replace with your token
Import the data:
data = Data([
Scattermapbox(
lat=[8.3512634, 7.9181144, 7.198902499999999, 7.142363900000001, 7.154055199999999, 7.3573265],
lon=[81.00699279999999, 80.2404959, 80.26001, 80.10377209999999, 80.05937519999999, 80.9539475],
mode='markers', name='Attacked but not abandoned',
marker=Marker( size=6, color='black', ),
text=['Kantalai', 'Ambanpola', 'Kamburupitiya', 'Nittambuwa', 'Veyangoda', 'Hasalaka'],
),
Scattermapbox(
lat=[6.866667, 6.0025371, 7.2193917, 7.409660799999999, 6.4345732, 9.7006168],
lon=[80.716667, 80.369516, 81.8497567, 80.60979499999999, 80.0003875, 80.0026139000000],
mode='markers', name='Not attacked or abandoned',
marker=Marker( size=6, color='green', ),
text=['Kantalai', 'Ambanpola', 'Kamburupitiya', 'Nittambuwa', 'Veyangoda', 'Hasalaka'],
),
Scattermapbox(
lat=[6.349044, 6.3461, 8.1657207, 6.4142105, 7.2268033],
lon=[80.0978637, 80.5515, 80.1964443, 81.3344408, 80.1958755],
mode='markers', name = "Abandoned because overrun",
marker=Marker( size=6, color='blue', ),
text=['Uragaha', 'Deniyaya', 'Rajangana', 'Kataragama', 'Warakapola'], ),
Scattermapbox(
lat=[8.042241800000001, 8.2921338, 6.2879969000000004, 8.150533, 8.8340998, 8.032214699999999],
lon=[80.94664399999999, 80.71728370000001, 80.1596041, 80.97898199999999, 80.7607389, 80.7519272],
mode='markers', name = "Attacked and abandoned for strategic reasons",
marker=Marker( size=6, color='red', ),
text=['Hingurakgoda', 'Galenbindunuwewa', 'Elpitiya', 'Medirigiriya', 'Padaviya', 'Habarana']
),
Scattermapbox(
lat=[8.821385000000001, 8.4054142, 7.4321630999999995, 7.5156351, 7.6239144, 7.746386099999999],
lon=[81.0947808, 81.0973818, 80.4437781, 80.50418409999999, 80.2433145, 80.1316927],
mode='markers', name = "Not attacked but abandoned for strategic reasons",
marker=Marker( size=6, color='gold', ),
text=['Kuchchaveli', 'Vanela', 'Mawathagama', 'Rambodagalla', 'Wariyapola', 'Nikaweratiya'],
)
])
Mapping:
layout = Layout(title='Police Station Attacks and Abandonment in Sri Lanka (1971)',
autosize=False,
hovermode='closest', orientation="portrait",
width=1100, height=1000,
mapbox=dict(
accesstoken=mapbox_access_token, bearing=0,
center=dict( lat=7.9, lon=80.5 ),
pitch=0, zoom=7, style='streets' ),
)
fig = dict(data=data, layout=layout)
py.iplot(fig, filename='Multiple Mapbox')
The final product would look something like the picture below. If you have more data, you can also set up empty lists and then use the pandas append function to append data to those lists, then use an "if" loop to append colors to a colors list.