You are data scientist who uses Python almost daily for data cleaning and modelling. Sometimes you need to visualize results of your work. This time you need to plot your data on the map. You do not do it routinely (and you don't have background in geography), but you want to create quickly nice interactive visualization.
import numpy as np
import pandas as pd
from plotly.offline import iplot, init_notebook_mode, plot
import plotly.graph_objs as go
init_notebook_mode()
x = np.random.randn(500)
data = [go.Histogram(x=x)]
layout = go.Layout(title='Example histogram', xaxis=dict(title='Random data'))
fig = go.Figure(data=data, layout=layout)
iplot(fig)
plotly.graph_objs
library contains graphical objectsHistogram
, Bar
, Scatter
, Choropleth
Figure
and Layout
dict()
plotly.offline
enables showing plots without sending data to plotlyinit_notebook_mode()
Figure
cntains data to plot (must be a list) and layout # dict() syntax
x = np.random.randn(500)
data = [dict(type='histogram', x=x)]
layout = dict(title='Example histogram', xaxis=dict(title='Random data'))
fig = dict(data=data, layout=layout)
iplot(fig)
df = pd.read_csv('https://raw.githubusercontent.com/plotly/datasets/master/2014_world_gdp_with_codes.csv')
data = [go.Choropleth(
locations = df['CODE'],
z = df['GDP (BILLIONS)'],
text = df['COUNTRY'],
colorscale = 'Viridis',
autocolorscale = False,
reversescale = True,
marker = go.choropleth.Marker(
line = go.choropleth.marker.Line(
color = 'rgb(180,180,180)',
width = 0.5
))
)]
layout = go.Layout(
title = go.layout.Title(
text = '2014 Global GDP'
),
geo = go.layout.Geo(
showframe = False,
showcoastlines = False,
projection = go.layout.geo.Projection(
type = 'equirectangular'
)
)
)
fig = go.Figure(data = data, layout = layout)
iplot(fig)
import folium
file_url = 'http://www2.census.gov/geo/docs/maps-data/data/gazetteer/2016_Gazetteer/2016_Gaz_zcta_national.zip'
df = pd.read_csv(file_url, sep='\t', dtype={'GEOID' : object})
#some column names have some extra padding
df.columns = df.columns.str.strip()
subset_df = df.sample(n=500)
example_map = folium.Map(
location=[subset_df['INTPTLAT'].mean(), subset_df['INTPTLONG'].mean()], zoom_start=4
)
for row in subset_df.itertuples():
example_map.add_child(folium.Marker(location=[row.INTPTLAT,row.INTPTLONG],popup=row.GEOID))
example_map
Map
objectMap
by creating Marker
object for each pointMarkerCluster
instead of Map
directlyMap
using add_child
methodMap
using folium.LayerControl().add_to(example_map)
import folium.plugins
file_url = 'http://www2.census.gov/geo/docs/maps-data/data/gazetteer/2016_Gazetteer/2016_Gaz_zcta_national.zip'
df = pd.read_csv(file_url, sep='\t', dtype={'GEOID' : object})
#some column names have some extra padding
df.columns = df.columns.str.strip()
subset_df = df.sample(n=500)
mc = folium.plugins.MarkerCluster()
example_map = folium.Map(
location=[subset_df['INTPTLAT'].mean(), subset_df['INTPTLONG'].mean()], zoom_start=4
)
for row in subset_df.itertuples():
mc.add_child(folium.Marker(location=[row.INTPTLAT,row.INTPTLONG],
popup=row.GEOID))
example_map.add_child(mc)
example_map
url = 'https://raw.githubusercontent.com/python-visualization/folium/master/examples/data'
state_geo = f'{url}/us-states.json'
state_unemployment = f'{url}/US_Unemployment_Oct2012.csv'
state_data = pd.read_csv(state_unemployment)
example_map = folium.Map(location=[48, -102], zoom_start=3)
folium.Choropleth(
geo_data=state_geo,
name='choropleth',
data=state_data,
columns=['State', 'Unemployment'],
key_on='feature.id',
fill_color='YlGn',
fill_opacity=0.7,
line_opacity=0.2,
legend_name='Unemployment Rate (%)'
).add_to(example_map)
folium.LayerControl().add_to(example_map)
example_map