Created
January 25, 2021 12:05
-
-
Save Hugovdberg/3ff36aedaa7b9ed5d869e3076d18faf5 to your computer and use it in GitHub Desktop.
Aantal zelfdodingen per jaar in Nederland
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import matplotlib.pyplot as plt | |
import pandas as pd | |
import numpy as np | |
zelfdodingen = ( | |
pd.read_excel( | |
"https://www.cbs.nl/-/media/_excel/2020/27/aanvullende-statistiek-zelfdoding-2019.xls", | |
usecols=[0, 3, 7], # Alleen kolommen met jaartal en totaal aantal zelfdodingen | |
skiprows=4, # Eerste rijen met kolomkoppen overslaan | |
names=["Jaar", "Totaal", "Totaal per 100.000"], | |
) | |
.dropna(how="any") | |
.assign( | |
Jaar=lambda df: pd.to_numeric( | |
df["Jaar"].astype(str).str.replace("*", "", regex=False) | |
) | |
) | |
) | |
cc = iter(plt.rcParams["axes.prop_cycle"]) | |
fig, ax = plt.subplots(figsize=(12, 8), constrained_layout=True) | |
ax2 = ax.twinx() | |
ax.plot(zelfdodingen["Jaar"], zelfdodingen["Totaal"], **next(cc), label="Absoluut") | |
ax2.plot(zelfdodingen["Jaar"], zelfdodingen["Totaal per 100.000"], **next(cc), label="Per 100.000 inwoners") | |
lines, labels = ax.get_legend_handles_labels() | |
lines2, labels2 = ax2.get_legend_handles_labels() | |
ax2.legend(lines + lines2, labels + labels2, loc='upper left') | |
ax.set_ylim(800, 2000) | |
ax.set_ylabel("Aantal zelfdodingen per jaar, absoluut") | |
ax2.set_ylim(7,13) | |
ax2.set_ylabel("Aantal zelfdodingen per jaar, per 100.000 inwoners") | |
ax.set_xticks(np.arange(1970, 2021, 5)); | |
ax.set_xlabel("Jaar") | |
ax.grid() | |
fig.savefig('D:/temp/zelfdoding.png') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment