File:Animierte Altersstruktur Deutschland.gif

From Wikimedia Commons, the free media repository
Jump to navigation Jump to search

Animierte_Altersstruktur_Deutschland.gif(700 × 500 pixels, file size: 595 KB, MIME type: image/gif, looped, 252 frames, 16 s)

Captions

Captions

Add a one-line explanation of what this file represents

Summary[edit]

Description
Deutsch: Animierte Altersstruktur von Deutschland (1970 - 2020). Links Männer, Rechts Frauen. X-Achse ist der prozentuale Anteil (somit bleibt die farbige Fläche immer gleich groß), Y-Achse das Alter.

Daten: Statistisches Bundesamt 12411-0007 (Flatfile)

Lineare Interpolation von Werten innerhalb eines Jahres (zur flüssigeren Anzeige).

Bis 1989: Früheres Bundesgebiet

Ab 2011: Ergebnisse auf Grundlage des Zensus 2011.
Date
Source Own work
Author Laserlicht
Other versions
Absolute Werte

Code[edit]

# %%
import pandas as pd
from datetime import datetime

df = pd.read_csv("12411-0007_flat.csv", sep = ";", encoding="cp1252", parse_dates=['Zeit'], date_parser=lambda x: datetime.strptime(x, "%d.%m.%Y"))

df

# %%
import numpy as np

df_geschl = df.pivot_table(values='BEVSTD__Bevoelkerungsstand__Anzahl', index=['Zeit', '3_Auspraegung_Label', '4_Auspraegung_Label'], aggfunc=np.sum)

df_geschl.columns = ["Anzahl"]

df_geschl.index = df_geschl.index.set_levels(df_geschl.index.levels[2].str.replace("-Jährige" , "").str.replace("unter 1 Jahr" , "0").str.replace("85 Jahre und mehr" , "100"), level=2)

df_geschl = df_geschl.drop('Insgesamt', axis=0, level=2)
df_geschl.index = df_geschl.index.remove_unused_levels()

df_geschl.index.names = ["Zeit", "Geschlecht", "Altersgruppe"]

df_geschl.index = df_geschl.index.set_levels(df_geschl.index.levels[2].astype(int), level=2)

df_geschl = df_geschl.sort_index(axis=0, level=[0, 1, 2], ascending=[True, True, True])

df_geschl

# %%
male = {x.year:df_geschl.xs("männlich", level='Geschlecht', drop_level=True).xs(x, level='Zeit', drop_level=True)["Anzahl"].tolist() for x in list(dict.fromkeys(df_geschl.index.get_level_values(0)))}
female = {x.year:df_geschl.xs("weiblich", level='Geschlecht', drop_level=True).xs(x, level='Zeit', drop_level=True)["Anzahl"].tolist() for x in list(dict.fromkeys(df_geschl.index.get_level_values(0)))}
groups = list(dict.fromkeys(df_geschl.index.get_level_values(2)))
years = list(male.keys())

# %%
import plotly.graph_objs as go
import plotly.offline as pyo
from scipy import interpolate
import imageio as iio
import os
from IPython.display import Image

images = []

print(len(years))
print(len(female))
aa=[v[0] for k, v in female.items()]

#Interpolation Beginn (auskommentieren zum deaktivieren)
years_new = np.arange(years[0], years[-1], 0.2)
female_new = {k:[] for k in years_new}
male_new = {k:[] for k in years_new}
for i, _ in enumerate(female[2000]):
    f_female = interpolate.interp1d(years, [v[i] for k, v in female.items()])
    female_new_tmp = dict(zip(years_new, f_female(years_new)))
    f_male = interpolate.interp1d(years, [v[i] for k, v in male.items()])
    male_new_tmp = dict(zip(years_new, f_male(years_new)))
    female_new = {k:v + [female_new_tmp[k]] for k, v in female_new.items()}
    male_new = {k:v + [male_new_tmp[k]] for k, v in male_new.items()}
years = years_new
male = male_new
female = female_new
#Interpolation Ende

for year in years:
    female_np = np.array(female[year])
    male_np = np.array(male[year])

    female_pct = (female_np / (male_np.sum() + female_np.sum())) * 100
    male_pct = (male_np / (male_np.sum() + female_np.sum())) * 100
    y = groups[:-1]

    xtick = list([])

    layout = go.Layout(yaxis=go.layout.YAxis(title='Alter'),
                    xaxis=go.layout.XAxis(
                        range=[-1, 1],
                        tickvals=xtick,
                        ticktext=[str(abs(x)) for x in xtick],
                        title=int(year+0.2)),
                    barmode='overlay',
                    bargap=0.0,
                    margin=go.layout.Margin(
                        l=10, #left margin
                        r=0, #right margin
                        b=10, #bottom margin
                        t=50, #top margin
                    ),
                    showlegend=False,
                    paper_bgcolor='rgba(255,255,255,255)',
                    plot_bgcolor='rgba(0,0,0,0)',
                    font_size=15,
                    title_text="Alterspyramide (prozentual) in (West-)Deutschland 1970 - 2020", title_x=0.52)

    data = [go.Bar(y=y,
                x=-male_pct[:-1],
                orientation='h',
                name='Männer',
                hovertext=male_pct[1:-1],
                hoverinfo='x',
                marker=dict(color='royalblue', line=dict(width=0))
                ),
            go.Bar(y=y,
                x=female_pct[:-1],
                orientation='h',
                name='Frauen',
                hovertext=female_pct[1:-1],
                hoverinfo='text',
                marker=dict(color='crimson', line=dict(width=0))
            )]

    fig = go.Figure(dict(data=data, layout=layout))
    fig.update_yaxes(showgrid=True, gridwidth=1, gridcolor='#eeeeee')
    img_bytes = fig.to_image(format="png")
    images.append(iio.imread(img_bytes, '.png'))
    #open("img_alter/" + str(year) + ".png", 'wb').write(img_bytes)

#fig.show()
iio.mimsave('vid.gif', images, duration=0.2 / 5)
os.system("convert -delay 200 'vid.gif[0]' -delay " + str(20 / 5) + " vid.gif -delay 400 'vid.gif[" + str(len(images)-1) + "]' -coalesce -layers Optimize video.gif")

#Image(url='vid.gif')

Licensing[edit]

I, the copyright holder of this work, hereby publish it under the following license:
w:en:Creative Commons
attribution share alike
This file is licensed under the Creative Commons Attribution-Share Alike 4.0 International license.
You are free:
  • to share – to copy, distribute and transmit the work
  • to remix – to adapt the work
Under the following conditions:
  • attribution – You must give appropriate credit, provide a link to the license, and indicate if changes were made. You may do so in any reasonable manner, but not in any way that suggests the licensor endorses you or your use.
  • share alike – If you remix, transform, or build upon the material, you must distribute your contributions under the same or compatible license as the original.

File history

Click on a date/time to view the file as it appeared at that time.

Date/TimeThumbnailDimensionsUserComment
current13:25, 13 February 2022Thumbnail for version as of 13:25, 13 February 2022700 × 500 (595 KB)Laserlicht (talk | contribs)optimize size
13:23, 13 February 2022Thumbnail for version as of 13:23, 13 February 2022700 × 500 (2.76 MB)Laserlicht (talk | contribs)Männlich -> links
13:18, 13 February 2022Thumbnail for version as of 13:18, 13 February 2022700 × 500 (598 KB)Laserlicht (talk | contribs)optimize size
13:16, 13 February 2022Thumbnail for version as of 13:16, 13 February 2022700 × 500 (2.76 MB)Laserlicht (talk | contribs)raw
02:59, 13 February 2022Thumbnail for version as of 02:59, 13 February 2022700 × 500 (595 KB)Laserlicht (talk | contribs)Getauscht; Bessere prozentuale Berechnung
02:46, 13 February 2022Thumbnail for version as of 02:46, 13 February 2022700 × 500 (597 KB)Laserlicht (talk | contribs)Uploaded own work with UploadWizard

File usage on other wikis

The following other wikis use this file: