| 1 |
%% Copyright 2021-2026 Tobias Enderle |
| 2 |
%% |
| 3 |
%% This work may be distributed and/or modified under the |
| 4 |
%% conditions of the LaTeX Project Public License, either version 1.3c |
| 5 |
%% of this license or (at your option) any later version. |
| 6 |
%% The latest version of this license is in |
| 7 |
%% http://www.latex-project.org/lppl.txt |
| 8 |
%% and version 1.3c or later is part of all distributions of LaTeX |
| 9 |
%% version 2005/12/01 or later. |
| 10 |
|
| 11 |
% DESCRIPTION |
| 12 |
% Demonstrates the visualization of data using *pgfplots* and *pandas* |
| 13 |
|
| 14 |
\documentclass{article} |
| 15 |
|
| 16 |
\usepackage{pgfplots} |
| 17 |
\pgfplotsset{compat=1.15} |
| 18 |
\usepackage{pyluatex} |
| 19 |
\usepackage{booktabs} |
| 20 |
|
| 21 |
\usepackage{url} |
| 22 |
\urldef\dataurl\url{https://population.un.org/wpp/Download/Files/1_Indicators%20(Standard)/CSV_FILES/WPP2019_TotalPopulationBySex.csv} |
| 23 |
|
| 24 |
\begin{python} |
| 25 |
import pandas as pd |
| 26 |
|
| 27 |
def pgf_coords(x, y): |
| 28 |
return ' '.join(f'({a},{b})' for a, b in zip(x, y)) |
| 29 |
|
| 30 |
file = 'population.csv' |
| 31 |
df = pd.read_csv(file) |
| 32 |
data = df.set_index(['Location', 'Time']).sort_index() |
| 33 |
world_data = data.loc['World'] |
| 34 |
|
| 35 |
year = 2026 |
| 36 |
current_pop = world_data.loc[year].PopTotal |
| 37 |
|
| 38 |
diffs = world_data.PopTotal.diff() |
| 39 |
highest_increase_year = diffs.idxmax() |
| 40 |
highest_increase = diffs.max() / 1e3 |
| 41 |
\end{python} |
| 42 |
|
| 43 |
\begin{document} |
| 44 |
|
| 45 |
This document demonstrates how data can be visualized flexibly in a \LaTeX{} |
| 46 |
document with the help of the Python package \emph{pandas} and the \LaTeX{} |
| 47 |
package \emph{pgfplots}. |
| 48 |
|
| 49 |
We show some statistics of the United Nations' \emph{World Population Prospects}% |
| 50 |
\footnote{United Nations, Department of Economic and Social Affairs, Population |
| 51 |
Division (2019). World Population Prospects 2019, Online Edition. Rev.\ 1.} in the |
| 52 |
form of plots, tables or individual values. |
| 53 |
The data file \texttt{\py{file}} is a subset of the file |
| 54 |
\texttt{WPP2019\_TotalPopulationBySex.csv}\footnote{\dataurl{} (last accessed: |
| 55 |
2021/07/04)}. |
| 56 |
|
| 57 |
The following plot shows the estimated world population between \py{df.Time.iloc[0]} |
| 58 |
and \py{df.Time.iloc[-1]}. The red lines mark the current year and population. |
| 59 |
|
| 60 |
\begin{center} |
| 61 |
\begin{tikzpicture} |
| 62 |
\begin{axis}[ |
| 63 |
width=\textwidth, |
| 64 |
height=0.7\textwidth, |
| 65 |
enlarge x limits=false, |
| 66 |
ymin=0, |
| 67 |
ylabel={Estimated world population in billions}, |
| 68 |
xlabel={Year}, |
| 69 |
xticklabel style={/pgf/number format/1000 sep=}, |
| 70 |
grid=both, |
| 71 |
] |
| 72 |
\addplot[no marks,black,thick] coordinates { |
| 73 |
\py{pgf_coords(world_data.index, world_data.PopTotal / 1e6)} |
| 74 |
}; |
| 75 |
\draw[red,thick] (axis cs:\py{year},0) |- (axis cs:0,\py{current_pop / 1e6}); |
| 76 |
\end{axis} |
| 77 |
\end{tikzpicture} |
| 78 |
\end{center} |
| 79 |
|
| 80 |
The year with the highest increase of \py{round(highest_increase)} million people |
| 81 |
was \py{highest_increase_year}. The world population is expected to reach a value |
| 82 |
of \py{round(world_data.loc[2100].PopTotal / 1e6, 2)} billion by 2100. |
| 83 |
|
| 84 |
The following table shows the total as well as the male and female population at |
| 85 |
the beginning of each decade. The values are given in thousands. |
| 86 |
\begin{center} |
| 87 |
\begin{python} |
| 88 |
table_data = world_data.iloc[::10].reset_index() |
| 89 |
table_data = table_data[['Time', 'PopTotal', 'PopMale', 'PopFemale']] |
| 90 |
table_data.columns = ['Year', 'Total (thousands)', 'Male (thousands)', 'Female (thousands)'] |
| 91 |
table = table_data.style.hide(axis='index').format( |
| 92 |
formatter={ |
| 93 |
'Total (thousands)': '{:,.0f}', |
| 94 |
'Male (thousands)': '{:,.0f}', |
| 95 |
'Female (thousands)': '{:,.0f}' |
| 96 |
} |
| 97 |
).to_latex(hrules=True) |
| 98 |
print(table) |
| 99 |
\end{python} |
| 100 |
\end{center} |
| 101 |
|
| 102 |
The plot below shows the estimated population of China and the United States |
| 103 |
of America (USA). The numbers are given in millions. |
| 104 |
\begin{center} |
| 105 |
\begin{python} |
| 106 |
marker_year = 2030 |
| 107 |
plots = [ |
| 108 |
('China', data.loc['China'].PopTotal / 1e3), |
| 109 |
('USA', data.loc['United States of America'].PopTotal / 1e3) |
| 110 |
] |
| 111 |
\end{python} |
| 112 |
\begin{tikzpicture} |
| 113 |
\begin{axis}[ |
| 114 |
width=\textwidth, |
| 115 |
height=0.55\textwidth, |
| 116 |
enlarge x limits=false, |
| 117 |
ymin=0, |
| 118 |
ylabel={Estimated population in millions}, |
| 119 |
xlabel={Year}, |
| 120 |
xticklabel style={/pgf/number format/1000 sep=}, |
| 121 |
grid=both, |
| 122 |
] |
| 123 |
\pgfplotsinvokeforeach{0,1}{ |
| 124 |
\addplot[no marks,black,thick] coordinates { |
| 125 |
\py{pgf_coords(plots[#1][1].index, plots[#1][1])} |
| 126 |
}; |
| 127 |
\node[below] at (axis cs:\py{marker_year},\py{plots[#1][1].loc[marker_year]}) |
| 128 |
{\py{plots[#1][0]}}; |
| 129 |
} |
| 130 |
\end{axis} |
| 131 |
\end{tikzpicture} |
| 132 |
\end{center} |
| 133 |
|
| 134 |
\end{document} |