From 8923f9eb6c902dbaad854c6234de27f2f4a60972 Mon Sep 17 00:00:00 2001 From: kp992 <145801876+kp992@users.noreply.github.com> Date: Thu, 21 Dec 2023 05:07:44 +0530 Subject: [PATCH] long_run_growth: styling and label fixes (#328) * styling and label fixes * fix --- lectures/long_run_growth.md | 100 +++++++++++++++--------------------- 1 file changed, 41 insertions(+), 59 deletions(-) diff --git a/lectures/long_run_growth.md b/lectures/long_run_growth.md index 91e20357..8abbaecc 100644 --- a/lectures/long_run_growth.md +++ b/lectures/long_run_growth.md @@ -4,14 +4,13 @@ jupytext: extension: .md format_name: myst format_version: 0.13 - jupytext_version: 1.14.5 + jupytext_version: 1.15.2 kernelspec: display_name: Python 3 (ipykernel) language: python name: python3 --- -+++ {"user_expressions": []} # Economic Growth Evidence @@ -68,16 +67,12 @@ First let's import the packages needed to explore what the data says about long ```{code-cell} ipython3 import pandas as pd -import os -import matplotlib as mpl import matplotlib.pyplot as plt import matplotlib.cm as cm import numpy as np from collections import namedtuple -from matplotlib.lines import Line2D ``` -+++ {"user_expressions": []} ## Setting up @@ -95,7 +90,6 @@ data = pd.read_excel("datasets/mpd2020.xlsx", sheet_name='Full data') data ``` -+++ {"user_expressions": []} We can see that this dataset contains GDP per capita (gdppc) and population (pop) for many countries and years. @@ -105,7 +99,6 @@ Let's look at how many and which countries are available in this dataset len(data.country.unique()) ``` -+++ {"user_expressions": []} We can now explore some of the 169 countries that are available. @@ -117,21 +110,20 @@ for cntry in data.country.unique(): cy_data = data[data.country == cntry]['year'] ymin, ymax = cy_data.min(), cy_data.max() cntry_years.append((cntry, ymin, ymax)) -cntry_years = pd.DataFrame(cntry_years, columns=['country', 'Min Year', 'Max Year']).set_index('country') +cntry_years = pd.DataFrame(cntry_years, + columns=['country', 'Min Year', 'Max Year']).set_index('country') cntry_years ``` -+++ {"user_expressions": []} Let's now reshape the original data into some convenient variables to enable quicker access to countries time series data. We can build a useful mapping between country codes and country names in this dataset ```{code-cell} ipython3 -code_to_name = data[['countrycode','country']].drop_duplicates().reset_index(drop=True).set_index(['countrycode']) +code_to_name = data[['countrycode', 'country']].drop_duplicates().reset_index(drop=True).set_index(['countrycode']) ``` -+++ {"user_expressions": []} Then we can quickly focus on GDP per capita (gdp) @@ -140,7 +132,7 @@ data ``` ```{code-cell} ipython3 -gdppc = data.set_index(['countrycode','year'])['gdppc'] +gdppc = data.set_index(['countrycode', 'year'])['gdppc'] gdppc = gdppc.unstack('countrycode') ``` @@ -148,7 +140,6 @@ gdppc = gdppc.unstack('countrycode') gdppc ``` -+++ {"user_expressions": []} We create a color mapping between country codes and colors for consistency @@ -179,14 +170,14 @@ mystnb: fig, ax = plt.subplots(dpi=300) cntry = 'GBR' _ = gdppc[cntry].plot( - ax = fig.gca(), - ylabel = 'International $\'s', - xlabel = 'Year', - linestyle='-', - color=color_mapping['GBR']) + ax=fig.gca(), + ylabel='International $\'s', + xlabel='Year', + linestyle='-', + color=color_mapping['GBR'] + ) ``` -+++ {"user_expressions": []} :::{note} [International Dollars](https://en.wikipedia.org/wiki/International_dollar) are a hypothetical unit of currency that has the same purchasing power parity that the U.S. Dollar has in the United States at any given time. They are also known as Geary–Khamis dollars (GK Dollars). @@ -219,7 +210,6 @@ ax.set_xlabel('Year') plt.show() ``` -+++ {"user_expressions": []} We can now put this into a function to generate plots for a list of countries @@ -257,7 +247,6 @@ def draw_interp_plots(series, ylabel, xlabel, color_mapping, code_to_name, lw, l return ax ``` -+++ {"user_expressions": []} As you can see from this chart, economic growth started in earnest in the 18th century and continued for the next two hundred years. @@ -280,8 +269,8 @@ fig, ax = plt.subplots(dpi=300, figsize=(10, 6)) cntry = ['CHN', 'GBR', 'USA'] ax = draw_interp_plots(gdppc[cntry].loc[1500:], - 'International $\'s','Year', - color_mapping, code_to_name, 2, False, ax) + 'International $\'s','Year', + color_mapping, code_to_name, 2, False, ax) # Define the parameters for the events and the text ylim = ax.get_ylim()[1] @@ -322,16 +311,14 @@ def draw_events(events, ax): event.y_text, event.text, color=event.color, **t_params) ax.axvspan(*event.year_range, color=event.color, alpha=0.2) - ax.axvline(event_mid, ymin=1, - ymax=event.ymax, color=event.color, - linestyle='-', clip_on=False, alpha=0.15) - + ax.axvline(event_mid, ymin=1, ymax=event.ymax, color=event.color, + linestyle='-', clip_on=False, alpha=0.15) + # Draw events draw_events(events, ax) plt.show() ``` -+++ {"user_expressions": []} The preceding graph of per capita GDP strikingly reveals how the spread of the industrial revolution has over time gradually lifted the living standards of substantial groups of people @@ -365,8 +352,8 @@ fig, ax = plt.subplots(dpi=300, figsize=(10, 6)) cntry = ['CHN'] ax = draw_interp_plots(gdppc[cntry].loc[1600:2000], - 'International $\'s','Year', - color_mapping, code_to_name, 2, True, ax) + 'International $\'s','Year', + color_mapping, code_to_name, 2, True, ax) ylim = ax.get_ylim()[1] @@ -402,7 +389,6 @@ draw_events(events, ax) plt.show() ``` -+++ {"user_expressions": []} We can also look at the United States (USA) and United Kingdom (GBR) in more detail @@ -425,8 +411,8 @@ fig, ax = plt.subplots(dpi=300, figsize=(10, 6)) cntry = ['GBR', 'USA'] ax = draw_interp_plots(gdppc[cntry].loc[1500:2000], - 'International $\'s','Year', - color_mapping, code_to_name, 2, True, ax) + 'International $\'s','Year', + color_mapping, code_to_name, 2, True, ax) ylim = ax.get_ylim()[1] @@ -463,7 +449,6 @@ draw_events(events, ax) plt.show() ``` -+++ {"user_expressions": []} ## The industrialized world @@ -478,7 +463,6 @@ data['gdp'] = data['gdppc'] * data['pop'] gdp = data['gdp'].unstack('countrycode') ``` -+++ {"user_expressions": []} ### Early industrialization (1820 to 1940) @@ -502,11 +486,10 @@ ax = fig.gca() cntry = ['CHN', 'SUN', 'JPN', 'GBR', 'USA'] start_year, end_year = (1820, 1945) ax = draw_interp_plots(gdp[cntry].loc[start_year:end_year], - 'International $\'s','Year', - color_mapping, code_to_name, 2, False, ax) + 'International $\'s', 'Year', + color_mapping, code_to_name, 2, False, ax) ``` -+++ {"user_expressions": []} ## Constructing a plot similar to Tooze's In this section we describe how we have constructed a version of the striking figure from chapter 1 of {cite}`Tooze_2014` that we discussed at the start of this lecture. @@ -515,22 +498,23 @@ Let's first define a collection of countries that consist of the British Empire ```{code-cell} ipython3 BEM = ['GBR', 'IND', 'AUS', 'NZL', 'CAN', 'ZAF'] -gdp['BEM'] = gdp[BEM].loc[start_year-1:end_year].interpolate(method='index').sum(axis=1) # Interpolate incomplete time-series +# Interpolate incomplete time-series +gdp['BEM'] = gdp[BEM].loc[start_year-1:end_year].interpolate(method='index').sum(axis=1) ``` -+++ {"user_expressions": []} Let's take a look at the aggregation that represents the British Empire. ```{code-cell} ipython3 -gdp['BEM'].plot() # The first year is np.nan due to interpolation +# The first year is np.nan due to interpolation +gdp['BEM'].plot(ylabel="International $'s") +plt.show() ``` ```{code-cell} ipython3 code_to_name ``` -+++ {"user_expressions": []} Now let's assemble our series and get ready to plot them. @@ -549,13 +533,14 @@ ax = fig.gca() cntry = ['DEU', 'USA', 'SUN', 'BEM', 'FRA', 'JPN'] start_year, end_year = (1821, 1945) ax = draw_interp_plots(gdp[cntry].loc[start_year:end_year], - 'Real GDP in 2011 $\'s','Year', - color_mapping, code_to_name, 2, False, ax) -plt.savefig("./_static/lecture_specific/long_run_growth/tooze_ch1_graph.png", dpi=300, bbox_inches='tight') + 'Real GDP in 2011 $\'s', 'Year', + color_mapping, code_to_name, 2, False, ax) + +plt.savefig("./_static/lecture_specific/long_run_growth/tooze_ch1_graph.png", dpi=300, + bbox_inches='tight') plt.show() ``` -+++ {"user_expressions": []} At the start of this lecture, we noted how US GDP came from "nowhere" at the start of the 19th century to rival and then overtake the GDP of the British Empire by the end of the 19th century, setting the geopolitical stage for the "American (twentieth) century". @@ -580,11 +565,10 @@ ax = fig.gca() cntry = ['CHN', 'SUN', 'JPN', 'GBR', 'USA'] start_year, end_year = (1950, 2020) ax = draw_interp_plots(gdp[cntry].loc[start_year:end_year], - 'International $\'s','Year', - color_mapping, code_to_name, 2, False, ax) + 'International $\'s', 'Year', + color_mapping, code_to_name, 2, False, ax) ``` -+++ {"user_expressions": []} It is tempting to compare this graph with figure {numref}`gdp1` that showed the US overtaking the UK near the start of the "American Century", a version of the graph featured in chapter 1 of {cite}`Tooze_2014`. @@ -595,11 +579,11 @@ We often want to study historical experiences of countries outside the club of " Fortunately, the [Maddison Historical Statistics](https://www.rug.nl/ggdc/historicaldevelopment/maddison/) dataset also includes regional aggregations ```{code-cell} ipython3 -data = pd.read_excel("datasets/mpd2020.xlsx", sheet_name='Regional data', header=(0,1,2), index_col=0) +data = pd.read_excel("datasets/mpd2020.xlsx", sheet_name='Regional data', header=(0,1,2), + index_col=0) data.columns = data.columns.droplevel(level=2) ``` -+++ {"user_expressions": []} We can save the raw data in a more convenient format to build a single table of regional GDP per capita @@ -608,7 +592,6 @@ regionalgdppc = data['gdppc_2011'].copy() regionalgdppc.index = pd.to_datetime(regionalgdppc.index, format='%Y') ``` -+++ {"user_expressions": []} Let's interpolate based on time to fill in any gaps in the dataset for the purpose of plotting @@ -616,7 +599,6 @@ Let's interpolate based on time to fill in any gaps in the dataset for the purpo regionalgdppc.interpolate(method='time', inplace=True) ``` -+++ {"user_expressions": []} and record a dataset of world GDP per capita @@ -634,13 +616,12 @@ mystnb: fig = plt.figure(dpi=300) ax = fig.gca() ax = worldgdppc.plot( - ax = ax, + ax=ax, xlabel='Year', ylabel='2011 US$', ) ``` -+++ {"user_expressions": []} Looking more closely, let's compare the time series for `Western Offshoots` and `Sub-Saharan Africa` and more broadly at a number of different regions around the world. @@ -656,9 +637,10 @@ mystnb: fig = plt.figure(dpi=300) ax = fig.gca() line_styles = ['-', '--', ':', '-.', '.', 'o', '-', '--', '-'] -ax = regionalgdppc.plot(ax = ax, style=line_styles) +ax = regionalgdppc.plot(ax=ax, xlabel='Year', + ylabel='2011 US$', style=line_styles) ax.set_yscale('log') -plt.legend(loc='lower center', -ncol=3, bbox_to_anchor=[0.5, -0.4]) +plt.legend(loc='lower center', + ncol=3, bbox_to_anchor=[0.5, -0.4]) plt.show() ```