Testing out dygraphs in Python...
import pandas as pd
import wbdata as wb
import pydygraphs
over65ind = {"SP.POP.65UP.TO.ZS": "pctover65"}
countries = ["IT", "NL", "ES"]
df_all = wb.get_dataframe(over65ind, country = countries, data_date = (pd.datetime(1960, 1, 1), pd.datetime(2012, 1, 1)))
df_all = df_all.reset_index()
df_all.head()
country | date | pctover65 | |
---|---|---|---|
0 | Spain | 2012 | 17.510246 |
1 | Spain | 2011 | 17.286287 |
2 | Spain | 2010 | 17.104019 |
3 | Spain | 2009 | 16.963305 |
4 | Spain | 2008 | 16.857691 |
Now we pivot the data frame to wide format...
wide = pd.pivot_table(df_all, values = 'pctover65', index = 'date', columns = 'country').reset_index()
wide['date'] = wide['date'].astype(float)
wide.head()
country | date | Italy | Netherlands | Spain |
---|---|---|---|---|
0 | 1960 | 9.512792 | 8.931693 | 8.201297 |
1 | 1961 | 9.637497 | 9.049868 | 8.299286 |
2 | 1962 | 9.756697 | 9.168340 | 8.405020 |
3 | 1963 | 9.876381 | 9.287049 | 8.521242 |
4 | 1964 | 10.004657 | 9.405879 | 8.651111 |
Time to make the plot!
## Dataframe example
fig2 = pydygraphs.figure()
xaxis = 'date'
fig2.plotDataFrame(wide, xaxis)
fig2.title("Percent of population over age 65")
fig2.xlabel("Year")
fig2.ylabel("Percent over 65")
fig2.show()