#I'm running pylab but if you're not, don't forget to import pandas and matplotlib too!
import wbdata
Let's start by setting the countries and indicators we want - in this case, I'm after the GNI per capita for Chile, Uruguay and Hungary.
countries = ["CL","UY","HU"]
indicators = {'NY.GNP.PCAP.CD':'GNI per Capita'}
OK, we now use wbdata's "get_dataframe" function to grab the data and put it into a pandas dataframe
df = wbdata.get_dataframe(indicators, country=countries, convert_date=False)
Let's just check what we've got there
df.head()
GNI per Capita | ||
---|---|---|
country | date | |
Chile | 2012 | 14280 |
2011 | 12270 | |
2010 | 10720 | |
2009 | 9940 | |
2008 | 10020 |
df.tail()
GNI per Capita | ||
---|---|---|
country | date | |
Uruguay | 1964 | 660 |
1963 | 610 | |
1962 | 580 | |
1961 | NaN | |
1960 | NaN |
OK, that looks like the data we want but it's currently "pivoted" which is no good for plotting. Luckily, the pandas unstack() function has us covered:
dfu = df.unstack(level=0)
dfu.head()
GNI per Capita | |||
---|---|---|---|
country | Chile | Hungary | Uruguay |
date | |||
1960 | NaN | NaN | NaN |
1961 | NaN | NaN | NaN |
1962 | 600 | NaN | 580 |
1963 | 640 | NaN | 610 |
1964 | 660 | NaN | 660 |
That's ready to plot, we could get rid of the missing values with dropna() but let's leave it for now - a one-liner with matplotlib should get us there:
plt.figure(); dfu.plot(); plt.legend(loc='best'); plt.title("GNI Per Capita ($USD, Atlas Method)"); plt.xlabel('Date'); plt.ylabel('GNI Per Capita ($USD, Atlas Method');
<matplotlib.figure.Figure at 0x922cdd0>
You can spend more time styling your plot than I did - matplotlib's documentation is good and you can even go all XKCD on your charts!