Skip to content

Instantly share code, notes, and snippets.

@imrehg
Created February 25, 2016 08:23
Show Gist options
  • Save imrehg/1d6a62395880bd67a53c to your computer and use it in GitHub Desktop.
Save imrehg/1d6a62395880bd67a53c to your computer and use it in GitHub Desktop.
Leisure vs Money plotting
#!/usr/bin/env python
"""
Leasure vs Money
Inspiration
Tweet by Stanford Econ Policy https://twitter.com/SIEPR/status/702637668883152896
Data sources
* GDP per capita PPP: : World Bank http://data.worldbank.org/indicator/NY.GDP.PCAP.PP.CD?display=default
* GDP per capita current USD: World Bank http://data.worldbank.org/indicator/NY.GDP.PCAP.CD/countries?display=default
* Working hours: OECD.stat https://stats.oecd.org/Index.aspx?DataSetCode=ANHRS
"""
import csv
from matplotlib import pyplot as pl
############
### Settings
############
year = 2014
workdatafile = "ANHRS_25022016071232431.csv"
## GDP/capita in USD
#gdpdatafile = "9e7e9b0e-aa8e-4354-bbd1-e7dbe8c49f17_v2.csv"
## PPP corrected GDP/capita in USD
gdpdatafile = "3b5b9aa4-2235-4a76-b91a-839614e2870b_v2.csv"
normalizecountry = "USA"
#############
## Helper vars
countrydata = {}
normalizer = 1
## Work hours
with open(workdatafile, 'r') as csvfile:
workreader = csv.reader(csvfile, delimiter=',', quotechar='"')
for row in workreader:
try:
if row[3] == "Total employment" and int(row[6]) == year:
countrydata[row[0]] = [float(row[14])]
except:
pass
## GDP
with open(gdpdatafile, 'r') as csvfile:
gdpreader = csv.reader(csvfile, delimiter=',', quotechar='"')
for row in gdpreader:
columnnum = -1
if len(row)> 5 and row[0] == "Country Name":
for i, c in enumerate(row):
try:
if int(c) == year:
columnum = i
break
except:
continue
try:
if row[1] in countrydata:
countrydata[row[1]] += [float(row[i])]
if row[1] == normalizecountry:
normalizer = float(row[i])
except:
pass
## Double-check results by printing them
print(countrydata)
# Plotting data
fig = pl.figure()
ax = fig.add_subplot(111)
for country in countrydata:
try:
x, y, t = countrydata[country][1]/normalizer, countrydata[country][0], country
ax.plot(x, y, 'ko')
ax.annotate(t, xy=(x, y), xytext=(x+0.02, y))
except IndexError:
pass
pl.title('Leisure vs. Money (quick redux)')
pl.ylabel('Hours worked per year [OECD]')
pl.xlabel('GDP per capita (normalized to {}) [World Bank]'.format(normalizecountry))
pl.grid()
pl.savefig("Leasure_vs_Money.png")
pl.show()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment