Home Artificial Intelligence Create Beautiful Age Distribution Graphs With Seaborn and Matplotlib (Including Animation)

Create Beautiful Age Distribution Graphs With Seaborn and Matplotlib (Including Animation)

0
Create Beautiful Age Distribution Graphs With Seaborn and Matplotlib (Including Animation)

Making a grid with multiple countries

You need to use plt.subplots() to create grids, but on this tutorial, I need to create a grid of images because I believe it looks higher.

The next function takes a listing of images and creates a grid with ncols. It really works by creating an empty image with a single background color that’s large enough to suit all figures.

def create_grid(figures, pad, ncols):
nrows = int(len(figures) / ncols)
size = figures[0].size

image = Image.recent(
"RGBA",
(ncols * size[0] + (ncols - 1) * pad, nrows * size[1] + (nrows - 1) * pad),
"#ffffff00"
)

for i, figure in enumerate(figures):
col, row = i % ncols, i // ncols
image.paste(figure, (col * (size[0] + pad), row * (size[1] + pad)))

return image

In the next code, I iterate over a listing of nations, add the resulting graph to figures, and create a grid by running create_grid() at the top.

figures = []

for country in [
"United States", "China", "Japan", "Brazil", "Canada",
"Germany", "Pakistan", "Russian Federation", "Nigeria",
"Sweden", "Cambodia", "Saudi Arabia", "Iceland",
"Spain", "South Africa", "Morocco"
]:

fig = plt.figure(figsize=(10, 8))

ax = create_age_distribution(
female_df=population_ratio_female,
male_df=population_ratio_male,
country=country,
12 months="2021"
)

ax.set(xlim=(-10, 10))

# Recent functions
format_ticks(ax, xformat="percentage")
add_legend(x=0.5, y=1.09)
plt.title("Age Distribution for {} in 2021".format(country), y=1.14, fontsize=20)

image = create_image_from_figure(fig)
image = add_padding_to_chart(image, 20, 20, 20, 5, background_color)

figures.append(image)

grid = create_grid(figures, pad=20, ncols=4)

Note that I exploit ratios as an alternative of absolute numbers and set xlim=(-10, 10). Otherwise, I won’t find a way to match the countries to one another visually.

Graphs created by the writer

Let’s move on to the last a part of this tutorial — create time-lapse visualizations.

Making a time-lapse visualization

The static age distribution charts look great, but it surely’s fascinating to see how they modify over time.

Since we’ve got actual values from 1960 to 2021 and predictions to 2050, we are able to create a time-lapse animation for a comparatively long period.

Before we start, I would like to inform you that the font I exploit, PT Mono, doesn’t have the identical height for all characters. To make the visualization look good, I needed to make use of plt.text() for the 12 months as an alternative of plt.title(). In the event you use one other font, it’s not essential to accomplish that.

Here’s the code:

images = []
years = list(population_male.columns[4:])

for 12 months in years:
fig = plt.figure(figsize=(10, 8))

ax = create_age_distribution(
female_df=population_female,
male_df=population_male,
country="World",
12 months=12 months
)

# Recent functions
format_ticks(ax, xformat="thousands and thousands", xlim=(-400000000, 400000000))
add_legend(x=0.5, y=1.09)
plt.title("Age Distribution for the World in ", y=1.14, fontsize=21)
plt.text(x=0.77, y=1.15, s=str(12 months), fontsize=21, transform=ax.transAxes)

image = create_image_from_figure(fig)
image = add_padding_to_chart(image, 20, 20, 20, 5, background_color)
images.append(image)

I exploit imageio to create a GIF from the list of images.

# Duplicating the last frames so as to add a delay 
# before the animation restarts
images = images + [images[-1] for _ in range(20)]
imageio.mimwrite('./time-lapse.gif', images, duration=0.2)

Let’s take a take a look at the result.

Visualization created by the writer

Awesome! That’s all for this tutorial; let me know if you happen to liked it and learned something useful.

LEAVE A REPLY

Please enter your comment!
Please enter your name here