Skip to content

Commit 1444052

Browse files
committed
update plots
1 parent 8fa5945 commit 1444052

File tree

1 file changed

+35
-54
lines changed

1 file changed

+35
-54
lines changed

lectures/inequality.md

Lines changed: 35 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -441,15 +441,12 @@ for σ in σ_vals:
441441
442442
```{code-cell} ipython3
443443
def plot_inequality_measures(x, y, legend, xlabel, ylabel):
444-
445444
fig, ax = plt.subplots()
446445
ax.plot(x, y, marker='o', label=legend)
447-
448446
ax.set_xlabel(xlabel)
449447
ax.set_ylabel(ylabel)
450-
451448
ax.legend()
452-
plt.show()
449+
return fig, ax
453450
```
454451
455452
```{code-cell} ipython3
@@ -461,11 +458,12 @@ mystnb:
461458
image:
462459
alt: gini_simulated
463460
---
464-
plot_inequality_measures(σ_vals,
465-
ginis,
466-
'simulated',
467-
'$\sigma$',
468-
'gini coefficients')
461+
fix, ax = plot_inequality_measures(σ_vals,
462+
ginis,
463+
'simulated',
464+
'$\sigma$',
465+
'gini coefficients')
466+
plt.show()
469467
```
470468
471469
The plots show that inequality rises with $\sigma$, according to the Gini
@@ -475,9 +473,9 @@ coefficient.
475473
476474
Now let's look at Gini coefficients for US data derived from the SCF.
477475
478-
The following code creates a list called `Ginis`.
476+
The following code creates a list called `ginis`.
479477
480-
It stores data of Gini coefficients generated from the dataframe ``df_income_wealth`` and method [gini_coefficient](https://quanteconpy.readthedocs.io/en/latest/tools/inequality.html#quantecon.inequality.gini_coefficient), from [QuantEcon](https://quantecon.org/quantecon-py/) library.
478+
It stores data of Gini coefficients generated from the dataframe `df_income_wealth` and method [gini_coefficient](https://quanteconpy.readthedocs.io/en/latest/tools/inequality.html#quantecon.inequality.gini_coefficient), from [QuantEcon](https://quantecon.org/quantecon-py/) library.
481479
482480
```{code-cell} ipython3
483481
:tags: [hide-input]
@@ -489,30 +487,30 @@ varlist = ['n_wealth', # net wealth
489487
df = df_income_wealth
490488
491489
# create lists to store Gini for each inequality measure
492-
493-
Ginis = []
490+
results = {}
494491
495492
for var in varlist:
496493
# create lists to store Gini
497-
ginis = []
498-
494+
gini_yr = []
499495
for year in years:
500496
# repeat the observations according to their weights
501497
counts = list(round(df[df['year'] == year]['weights'] ))
502498
y = df[df['year'] == year][var].repeat(counts)
503499
y = np.asarray(y)
504500
505501
rd.shuffle(y) # shuffle the sequence
506-
502+
507503
# calculate and store Gini
508504
gini = qe.gini_coefficient(y)
509-
ginis.append(gini)
505+
gini_yr.append(gini)
510506
511-
Ginis.append(ginis)
507+
results[var] = gini_yr
512508
```
513509
514510
```{code-cell} ipython3
515-
ginis_nw, ginis_ti, ginis_li = Ginis
511+
ginis_nw = results['n_wealth'] # net wealth
512+
ginis_ti = results['t_income'] # total income
513+
ginis_li = results['l_income'] # labour income
516514
```
517515
518516
Let's plot the Gini coefficients for net wealth, labor income and total income.
@@ -532,16 +530,10 @@ mystnb:
532530
image:
533531
alt: gini_wealth_us
534532
---
535-
xlabel = "year"
536-
ylabel = "gini coefficient"
537-
538533
fig, ax = plt.subplots()
539-
540534
ax.plot(years, ginis_nw, marker='o')
541-
542-
ax.set_xlabel(xlabel)
543-
ax.set_ylabel(ylabel)
544-
535+
ax.set_xlabel("year")
536+
ax.set_ylabel("gini coefficient")
545537
plt.show()
546538
```
547539
@@ -554,17 +546,11 @@ mystnb:
554546
image:
555547
alt: gini_income_us
556548
---
557-
xlabel = "year"
558-
ylabel = "gini coefficient"
559-
560549
fig, ax = plt.subplots()
561-
562550
ax.plot(years, ginis_li_new, marker='o', label="labor income")
563551
ax.plot(years, ginis_ti, marker='o', label="total income")
564-
565-
ax.set_xlabel(xlabel)
566-
ax.set_ylabel(ylabel)
567-
552+
ax.set_xlabel("year")
553+
ax.set_ylabel("gini coefficient")
568554
ax.legend()
569555
plt.show()
570556
```
@@ -574,7 +560,6 @@ substantially since 1980.
574560
575561
The wealth time series exhibits a strong U-shape.
576562
577-
578563
## Top shares
579564
580565
Another popular measure of inequality is the top shares.
@@ -658,21 +643,15 @@ mystnb:
658643
image:
659644
alt: top_shares_us
660645
---
661-
xlabel = "year"
662-
ylabel = "top $10\%$ share"
663-
664646
fig, ax = plt.subplots()
665-
666647
ax.plot(years, df_topshares["topshare_l_income"],
667648
marker='o', label="labor income")
668649
ax.plot(years, df_topshares["topshare_n_wealth"],
669650
marker='o', label="net wealth")
670651
ax.plot(years, df_topshares["topshare_t_income"],
671652
marker='o', label="total income")
672-
673-
ax.set_xlabel(xlabel)
674-
ax.set_ylabel(ylabel)
675-
653+
ax.set_xlabel("year")
654+
ax.set_ylabel("top $10\%$ share")
676655
ax.legend()
677656
```
678657
@@ -742,11 +721,12 @@ mystnb:
742721
image:
743722
alt: top_shares_simulated
744723
---
745-
plot_inequality_measures(σ_vals,
746-
topshares,
747-
"simulated data",
748-
"$\sigma$",
749-
"top $10\%$ share")
724+
fig, ax = plot_inequality_measures(σ_vals,
725+
topshares,
726+
"simulated data",
727+
"$\sigma$",
728+
"top $10\%$ share")
729+
plt.show()
750730
```
751731
752732
```{code-cell} ipython3
@@ -758,11 +738,12 @@ mystnb:
758738
image:
759739
alt: gini_coef_simulated
760740
---
761-
plot_inequality_measures(σ_vals,
762-
ginis,
763-
"simulated data",
764-
"$\sigma$",
765-
"gini coefficient")
741+
fig, ax = plot_inequality_measures(σ_vals,
742+
ginis,
743+
"simulated data",
744+
"$\sigma$",
745+
"gini coefficient")
746+
plt.show()
766747
```
767748
768749
```{code-cell} ipython3

0 commit comments

Comments
 (0)