Geographical plotting with Plotly


In [1]:
import plotly.plotly as py
import plotly.graph_objs as go
import pandas as pd
from plotly.offline import download_plotlyjs, init_notebook_mode, plot, iplot
init_notebook_mode(connected=True)


Choropleth map of USA

Create some data for plotting. Plotly needs this notation


In [2]:
data = dict(type='choropleth', locations=['AZ','CA','NY'],
           locationmode='USA-states', colorscale='Greens',
           text=['text 1', 'text 2', 'text 3'],
           z=[1,2,3], colorbar={'title':'Color bar title here'})

layout = dict(geo={'scope':'usa'})

In [3]:
layout


Out[3]:
{'geo': {'scope': 'usa'}}

In [4]:
choro_map = go.Figure(data=[data], layout=layout)
choro_map


Out[4]:
{'data': [{'colorbar': {'title': 'Color bar title here'},
   'colorscale': 'Greens',
   'locationmode': 'USA-states',
   'locations': ['AZ', 'CA', 'NY'],
   'text': ['text 1', 'text 2', 'text 3'],
   'type': 'choropleth',
   'z': [1, 2, 3]}],
 'layout': {'geo': {'scope': 'usa'}}}

In [5]:
iplot(choro_map)


World choropleth maps


In [6]:
world_gdp_df = pd.read_csv('/Users/atma6951/Documents/code/pychakras/pychakras/udemy_ml_bootcamp/Python-for-Data-Visualization/Geographical Plotting/2014_World_GDP')
world_gdp_df.head()


Out[6]:
COUNTRY GDP (BILLIONS) CODE
0 Afghanistan 21.71 AFG
1 Albania 13.40 ALB
2 Algeria 227.80 DZA
3 American Samoa 0.75 ASM
4 Andorra 4.80 AND

In [7]:
data = {'type':'choropleth', 'locations':world_gdp_df['CODE'],
       'z':world_gdp_df['GDP (BILLIONS)'], 'text':world_gdp_df['COUNTRY'],
       'colorbar':{'title':'GDP in Billions USD'}}

layout={'title':'2014 Global GDP',
       'geo':{'showframe':False, 'projection':{'type':'Mercator'}}}

choromap3 = go.Figure(data=[data], layout=layout)

In [8]:
iplot(choromap3)



In [ ]: