Skip to content

Instantly share code, notes, and snippets.

@mtavkhelidze
Last active April 9, 2020 19:28
Show Gist options
  • Save mtavkhelidze/b5b82460e4f3484632cd669093d38cf3 to your computer and use it in GitHub Desktop.
Save mtavkhelidze/b5b82460e4f3484632cd669093d38cf3 to your computer and use it in GitHub Desktop.
Number of veterans in Russia (blue) with annual change (red).
# Number of veterans in Russia
import matplotlib.pyplot as plt
import pandas as pd
from matplotlib.axes import Axes
from matplotlib.figure import Figure
raw_data_source = "https://twower.livejournal.com/2408991.html"
raw_data = """
2007 - 1 044 091
2008 - 1 110 326
2009 - 1 149 896
2010 - 1 184 920
2011 - 1 215 497
2012 - 1 247 231
2013 - 1 274 903
2014 - 1 296 235
2015 - 1 312 071
2016 - 1 323 472
2017 - 1 335 776
2018 - 1 368 156
2019 - 1 438 201
2020 - 1 488 285
"""
data = (
(int(y), int(n.replace(" ", "")))
for y, n in (line.split(" - ") for line in raw_data.split("\n") if line is not "")
)
df = pd.DataFrame(data, columns=["Year", "Number"])
df["Difference"] = df["Number"] - df["Number"].shift(1)
fig: Figure
ax1: Axes
fig, ax1 = plt.subplots()
ax2: Axes = ax1.twinx()
color = "tab:blue"
ax1.set_xlabel("")
ax1.set_ylabel("Всего (тыс. человек)", color=color, fontsize=12, fontweight="bold")
ax1.bar(df["Year"], df["Number"] / 1000, color=color)
ax1.tick_params(axis="y", labelcolor=color)
ax1.autoscale(True)
color = "tab:red"
ax2.set_ylabel(
"Прирост по сравнению с\nпредыдущим годом",
color=color,
fontsize=12,
fontweight="bold",
)
ax2.plot(df["Year"], df["Difference"], color=color, lw=4)
ax2.tick_params(axis="y", labelcolor=color)
ax2.autoscale(True)
plt.suptitle("Количество ветеранов в России", fontsize=14, fontweight="bold")
plt.annotate(
"Источник: Росстат, ЖЖ юзер @twower",
xy=(1, 0),
xycoords=("axes fraction", "figure fraction"),
xytext=(0, 10),
textcoords="offset points",
ha="right",
va="bottom",
)
fig.tight_layout(rect=[0, 0.03, 1, 0.95])
plt.show()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment