Skip to content

Instantly share code, notes, and snippets.

@hbshrestha
Created December 24, 2021 17:23
Show Gist options
  • Save hbshrestha/a9b46e30ea369ff30b49a71431d2fd28 to your computer and use it in GitHub Desktop.
Save hbshrestha/a9b46e30ea369ff30b49a71431d2fd28 to your computer and use it in GitHub Desktop.
Total population across SAARC in 1990 and 2020
plt.rcParams["figure.figsize"] = (12, 6)
plt.rcParams["font.size"] = 14
colors = ["red","green","gold","orange","blue","skyblue","maroon","grey"]
fig, ax = plt.subplots()
df_pop.T.plot.bar(stacked = True, ax = ax, color = colors)
ax.set_ylabel("Population")
ax.xaxis.set_ticklabels(ticklabels = [1990, 2020], rotation = 0)
for i in range(len(df_pop)):
if i==0:
ax.fill_between(x = [0.25, 0.75],
y1 = [0, 0],
y2 = [df_pop.iloc[0, 0], df_pop.iloc[0, 1]],
color = colors[i],
alpha = 0.2)
elif i!=7:
ax.fill_between(x = [0.25, 0.75],
y1 = [df_pop.iloc[:i, 0].sum(), df_pop.iloc[:i, 1].sum()],
y2 = [df_pop.iloc[:i+1, 0].sum(), df_pop.iloc[:i+1, 1].sum()],
color = colors[i],
alpha = 0.2)
else:
ax.fill_between(x = [0.25, 0.75],
y1 = [df_pop.iloc[:i, 0].sum(), df_pop.iloc[:i, 1].sum()],
y2 = [df_pop[1990].sum(), df_pop[2020].sum()],
color = colors[i],
alpha = 0.2)
ax.annotate(text = f"{round(df_pop[1990].sum()/10**9, 2)} billion",
xy = (-0.1,df_pop[1990].sum()*1.1))
ax.annotate(text = f"{round(df_pop[2020].sum()/10**9, 2)} billion",
xy = (0.9,df_pop[2020].sum()*1.05))
ax.spines["right"].set_color("white")
ax.spines["top"].set_color("white")
plt.tick_params(color = "white")
plt.title("Total population in SAARC region\n 1990 and 2020", pad = 20, fontdict = {"weight":"bold", "size":20})
plt.legend(bbox_to_anchor = (0.85, -0.1), ncol = 4)
plt.show()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment