In [1]:
import plotly
import plotly.graph_objs as go
import numpy as np
plotly.offline.init_notebook_mode()
import pandas as pd
Data are from : https://esa.un.org/unpd/wpp/Download/Standard/Population/
In [2]:
cat ../../Downloads/WPP2015_DB03_Population_Quinquennial.txt
In [23]:
!file -I ../../Downloads/WPP2015_DB03_Population_Quinquennial.csv
In [25]:
!iconv -c -f us-ascii -t utf-8 ../../Downloads/WPP2015_DB03_Population_Quinquennial.csv > ../../Downloads/data_utf-8.csv
In [3]:
df = pd.read_csv("../../Downloads/data_utf-8.csv")
#df = pd.read_csv("Downloads/WPP2015_DB03_Population_Quinquennial.csv")
In [4]:
df
Out[4]:
First we do the following selection :
In [12]:
df2015 = df[(df["Time"] == 2015) & (df["Sex"] == "Both") & (df["Variant"] == "Medium") & (200 < df["LocID"]) & (df["LocID"] <= 250)]
df2015
Out[12]:
In [13]:
df2015 = df2015.groupby(["Location", "LocID", "Time", "Sex", "Variant"], as_index=False).aggregate({"Value": np.sum})
df2015 = df2015.set_index("Location")
df2015
Out[13]:
In [7]:
df_continent = df2015.loc[["AFRICA", "ASIA", "EUROPE", "NORTHERN AMERICA", "OCEANIA"]]
df_continent
Out[7]:
In [22]:
colorscale = [[0.0, 'rgb(242,240,247)'],[0.2, 'rgb(218,218,235)'],[0.4, 'rgb(188,189,220)'],
[0.6, 'rgb(158,154,200)'],[0.8, 'rgb(117,107,177)'],[1.0, 'rgb(84,39,143)']]
data = [
dict(
type='choropleth',
colorscale = colorscale,
autocolorscale = False,
locations = df2015.index,
z = df2015['Value'],
locationmode = 'country name',
text = df2015.index.astype(str) + " " + str(df2015["Value"]),
marker = dict(
line = dict (
color = 'rgb(255,255,255)',
width = 2
) ),
colorbar = dict(
title = "Population")
) ]
layout = dict(
title = 'World population by countries',
geo = dict(
scope='world',
projection=dict( type='natural earth' )
)
)
fig = dict( data=data, layout=layout )
plotly.plotly.iplot( fig, filename='world-map' )
#plotly.plotly.image.save_as(fig, "d3-map.png")
Out[22]:
In [19]:
colorscale = [[0.0, 'rgb(242,240,247)'],[0.2, 'rgb(218,218,235)'],[0.4, 'rgb(188,189,220)'],
[0.6, 'rgb(158,154,200)'],[0.8, 'rgb(117,107,177)'],[1.0, 'rgb(84,39,143)']]
data = [
dict(
type='choropleth',
colorscale = colorscale,
autocolorscale = False,
locations = df_continent["LocID"],
z = df_continent['Value'],
locationmode = 'country name',
text = df_continent.index.astype(str) + " " + str(df_continent["Value"]),
marker = dict(
line = dict (
color = 'rgb(255,255,255)',
width = 2
) ),
colorbar = dict(
title = "Population")
) ]
layout = dict(
title = 'World population by countries',
geo = dict(
scope='world',
projection=dict( type='orthographic' )
)
)
fig = dict( data=data, layout=layout )
plotly.plotly.iplot( fig, filename='world-map' )
#plotly.plotly.image.save_as(fig, "d3-map.png")
Out[19]: