Répartition géographique des membres de l'AFPY en 2016 à partir données récoltées lors de la PyCon-fr de Rennes (voir le mail d'Arthur).
In [1]:
# plotly
import plotly
import plotly.graph_objs as go
import colorlover as cl
from IPython.display import HTML
# pandas
import pandas as pd
# plotly offline mode
plotly.__version__
plotly.offline.init_notebook_mode()
In [2]:
df = pd.read_csv("membres_par_pays_dep.csv", sep=";", index_col="Pays", usecols=(0, 1))
df.rename(columns={"Nombre de membres": "membres"}, inplace=True)
df.head()
Out[2]:
In [3]:
codes = pd.read_csv("https://commondatastorage.googleapis.com/ckannet-storage/2011-11-25T132653/iso_3166_2_countries.csv",
usecols=(1, 10, 11), index_col=1)
df = df.join(codes, how="inner").fillna(0)
df.rename(columns={"ISO 3166-1 3 Letter Code": "CODE"}, inplace=True)
df.head(10)
Out[3]:
In [4]:
bypays = df.groupby(by="CODE").aggregate({"membres": "sum", "Common Name": "first"})
bypays.sort_values(by="membres", inplace=True, ascending=False)
bypays.head(10)
Out[4]:
In [5]:
HTML(cl.to_html(cl.scales["6"]["seq"]["YlGnBu"]))
Out[5]:
In [6]:
ncolor = 4
colorscale = [[10**(i-ncolor+1), color] for i, color in enumerate(cl.scales[str(ncolor)]["seq"]["YlGnBu"])]
colorscale[0] = [0, colorscale[0][1]]
colorscale
Out[6]:
Chloropleth
(voir ici d'autres exemples)
In [7]:
macarte = go.Choropleth(
locations = bypays.index,
z = bypays["membres"],
text = bypays["Common Name"],
hoverinfo="text+z",
colorscale = colorscale,
autocolorscale = False,
marker = go.Marker(
line = go.Line(
color = 'rgb(180,180,180)',
width = 0.5
)
),
colorbar = go.ColorBar(
title="membres",
tickmode="array",
tickvals=[0, 10, 100, 500, 900]
),
)
In [8]:
layout = go.Layout(
title = "Membres de l'AFPY dans le monde en 2016",
geo = dict(
showframe = False,
coastlinecolor="#888",
showocean=True,
oceancolor="#75BEDB",
showland=True,
landcolor="#EEE",
showcountries=True,
countrywidth=0.2,
projection = dict(type="orthographic")
)
)
In [9]:
fig = go.Figure(data=[macarte], layout=layout)
plotly.offline.iplot(fig)
plotly.offline.plot(fig, filename="afpy_monde.html", auto_open=False)
Out[9]:
In [10]:
layout_eu = go.Layout(
title = "Membres de l'AFPY dans le monde en 2016",
geo = dict(
showframe = False,
coastlinecolor="#888",
showocean=True,
oceancolor="#75BEDB",
showland=True,
landcolor="#EEE",
showcountries=True,
countrywidth=0.2,
projection = dict(type="mercator"),
lonaxis=dict(range=[-30, 30]),
lataxis=dict(range=[0, 60])
)
)
fig_eu = go.Figure(data=[macarte], layout=layout_eu)
plotly.offline.iplot(fig_eu)
plotly.offline.plot(fig_eu, filename="afpy_europe.html", auto_open=False)
Out[10]: