Add plotly figures to scripts

This commit is contained in:
Marty Oehme 2022-08-20 16:44:25 +02:00
parent aa91164623
commit 7fae45af94
Signed by: Marty
GPG Key ID: B7538B8F50A1C800
7 changed files with 107 additions and 1 deletions

View File

@ -20,6 +20,12 @@ At the same time, the country's poverty rate, even measured based on the interna
from a relative rate of households in poverty at 18.8% in 2019, to 18.7% in 2020 and 18.3% at the end of 2021,
with the reduction threatened to be slowed further through increased prices on food and energy [@WorldBank2022b].
```{python}
#| label: gini-ben
#| fig-cap: "Gini index of consumption per capita for Benin. Source: Author's elaboration based on UNU-WIDER WIID (2022)."
plot_consumption_gini_percapita(ben)
```
<!-- poverty -->
Based on its national poverty line, Benin's overall poverty rate is 38.5%,
though it hides a strong spatial disparity between rural and urban households with 44.2% to 31.4% households in poverty respectively [@WorldBank2022b].

View File

@ -17,6 +17,12 @@ The country's GDP has averaged roughly 6% per year before the Covid-19 pandemic
However, the country's inequality levels are some of the highest in the world and its poverty rates are extreme.
Additionally in many cases there is a lack of data or the data itself are lacking in several dimensions which hinders creating a cohesive picture or plan.
```{python}
#| label: gini-dji
#| fig-cap: "Gini index of consumption per capita for Djibouti. Source: Author's elaboration based on UNU-WIDER WIID (2022)."
plot_consumption_gini_percapita(dji)
```
<!-- poverty -->
Poverty in Djibouti is both very high and marked by high deprivation.
Using the national poverty line of around 2.18USD (2011 PPP) the poverty rate for the overall country by consumption is estimated at 21.1% in 2017,

View File

@ -23,6 +23,12 @@ Additionally, he sees quintile inequalities primarily driven by the highest quin
also finding a significantly higher coefficient for the first quintile (0.14), however.
These inequality levels remain mostly unchanged from 2012/13 to 2019/20 but hide qualitative dimensions such as the shift out of a lower-income agricultural livelihood predominantly taking place among older men who have at least some level of formal education and are from already more well-off households [@Atamanov2022].
```{python}
#| label: gini-uga
#| fig-cap: "Gini index of consumption per capita for Uganda. Source: Author's elaboration based on UNU-WIDER WIID (2022)."
plot_consumption_gini_percapita(uga)
```
<!-- poverty -->
The World Bank [-@Atamanov2022] report goes on to examine the share of people below the poverty line in Uganda:
around 30% of households are in a state of poverty in 2019/20,

View File

@ -29,6 +29,12 @@ though absolute income may be rising, with the top quintile having 9.2 times the
Economic inequality and poverty in Vietnam thus underlies an intersectional focus, between ethnic minorities, regional situations, rural-urban divides and gendered lines,
one which exogenous shocks can rapidly exacerbate as the example of the COVID-19 pandemic has recently shown [@Ebrahim2021].
```{python}
#| label: gini-vnm
#| fig-cap: "Gini index of consumption per capita for Vietnam. Source: Author's elaboration based on UNU-WIDER WIID (2022)."
plot_consumption_gini_percapita_ruralurban(vnm)
```
<!--
* estimated Gini coeff, overall income distribution: [@Le2021]
* fluctuating 0.42-0.44 (2010-2018)

View File

@ -28,7 +28,7 @@ format:
linestretch: 1.25
fontfamily: lmodern
fontsize: "12"
# toc: true
toc: true
jupyter: python3
title: "Drivers of Inequality"
author:

71
data-prep.qmd Normal file
View File

@ -0,0 +1,71 @@
---
format:
html:
code-fold: true
standalone: true
fig-cap-location: top
---
```{python}
#| echo: false
import matplotlib.pyplot as plt
def prepare_plot_colors():
# "Tableau 20" colors as RGB.
colors = [(31, 119, 180), (174, 199, 232), (255, 127, 14), (255, 187, 120),
(44, 160, 44), (152, 223, 138), (214, 39, 40), (255, 152, 150),
(148, 103, 189), (197, 176, 213), (140, 86, 75), (196, 156, 148),
(227, 119, 194), (247, 182, 210), (127, 127, 127), (199, 199, 199),
(188, 189, 34), (219, 219, 141), (23, 190, 207), (158, 218, 229)]
# Scale RGB values to the [0, 1] range for matplotlib
for i in range(len(colors)):
r, g, b = colors[i]
colors[i] = (r / 255., g / 255., b / 255.)
return colors
colors=prepare_plot_colors()
```
```{python}
#| echo: false
import openpyxl
import pandas as pd
df = pd.read_csv('data/cleaned/UNU-WIDER-WIID/WIID-30JUN2022_cty-select.csv', index_col="id", parse_dates=True)
```
```{python}
#| echo: false
df = df.loc[df['year'] > 2000]
ben = df.loc[df['c3'] == "BEN"]
dji = df.loc[df['c3'] == "DJI"]
uga = df.loc[df['c3'] == "UGA"]
vnm = df.loc[df['c3'] == "VNM"]
```
```{python}
# Set up the data extraction and figure drawing functions
import plotly.express as px
import plotly.io as pio
def gini_plot(country_df):
if svg_render:
pio.renderers.default = "png"
fig = px.line(country_df, x="year", y="gini", markers=True, labels={"year": "Year", "gini": "Gini coefficient"}, template="seaborn", range_y=[30,60])
fig.update_traces(marker_size=10)
fig.show()
def plot_consumption_gini_percapita(country_df):
gni_cnsmpt = country_df[country_df['resource'].str.contains("Consumption")]
gni_cnsmpt_percapita = gni_cnsmpt[gni_cnsmpt['scale'].str.contains("Per capita")]
gini_plot(gni_cnsmpt_percapita)
def plot_consumption_gini_percapita_ruralurban(country_df):
gni_cnsmpt = country_df[country_df['resource'].str.contains("Consumption")]
gni_cnsmpt = gni_cnsmpt[gni_cnsmpt['scale'].str.contains("Per capita")]
gni_cnsmpt = gni_cnsmpt[gni_cnsmpt['source'].str.contains("World Bank")]
gni_cnsmpt = gni_cnsmpt[gni_cnsmpt['areacovr'].str.contains("All")]
gini_plot(gni_cnsmpt)
```

View File

@ -1,3 +1,14 @@
---
nocite: |
| @UNU-WIDER2022a
| @UNU-WIDER2022
---
{{< include data-prep.qmd >}}
```{python}
svg_render = True
```
{{< include _drivers-of-inequality-vietnam.qmd >}}