in

The way to Create Lovely Age Distribution Graphs With Seaborn and Matplotlib (Together with Animation) | by Oscar Leo | Jun, 2023


Making a grid with a number of nations

You need to use plt.subplots() to create grids, however on this tutorial, I need to create a grid of photographs as a result of I feel it appears to be like higher.

The next operate takes an inventory of photographs and creates a grid with ncols. It really works by creating an empty picture with a single background coloration that’s massive sufficient to suit all figures.

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

picture = Picture.new(
"RGBA",
(ncols * measurement[0] + (ncols - 1) * pad, nrows * measurement[1] + (nrows - 1) * pad),
"#ffffff00"
)

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

return picture

Within the following code, I iterate over an inventory of nations, add the ensuing graph to figures, and create a grid by operating create_grid() on the finish.

figures = []

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

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

ax = create_age_distribution(
female_df=population_ratio_female,
male_df=population_ratio_male,
nation=nation,
yr="2021"
)

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

# New capabilities
format_ticks(ax, xformat="proportion")
add_legend(x=0.5, y=1.09)
plt.title("Age Distribution for {} in 2021".format(nation), y=1.14, fontsize=20)

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

figures.append(picture)

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

Be aware that I exploit ratios as a substitute of absolute numbers and set xlim=(-10, 10). In any other case, I gained’t have the ability to evaluate the nations to one another visually.

Graphs created by the creator

Let’s transfer on to the final a part of this tutorial — The way to create time-lapse visualizations.

Making a time-lapse visualization

The static age distribution charts look nice, however it’s fascinating to see how they modify over time.

Since we now have precise values from 1960 to 2021 and predictions to 2050, we will create a time-lapse animation for a comparatively lengthy interval.

Earlier than we start, I have to inform you that the font I exploit, PT Mono, doesn’t have the identical top for all characters. To make the visualization look good, I wanted to make use of plt.textual content() for the yr as a substitute of plt.title(). Should you use one other font, it’s not vital to take action.

Right here’s the code:

photographs = []
years = listing(population_male.columns[4:])

for yr in years:
fig = plt.determine(figsize=(10, 8))

ax = create_age_distribution(
female_df=population_female,
male_df=population_male,
nation="World",
yr=yr
)

# New capabilities
format_ticks(ax, xformat="hundreds of 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.textual content(x=0.77, y=1.15, s=str(yr), fontsize=21, rework=ax.transAxes)

picture = create_image_from_figure(fig)
picture = add_padding_to_chart(picture, 20, 20, 20, 5, background_color)
photographs.append(picture)

I exploit imageio to create a GIF from the listing of photographs.

# Duplicating the final frames so as to add a delay 
# earlier than the animation restarts
photographs = photographs + [images[-1] for _ in vary(20)]
imageio.mimwrite('./time-lapse.gif', photographs, length=0.2)

Let’s check out the outcome.

Visualization created by the creator

Superior! That’s all for this tutorial; let me know for those who preferred it and realized one thing helpful.


PatchTST: A Breakthrough in Time Collection Forecasting | by Marco Peixeiro | Jun, 2023

Retro Information Science: Testing the First Variations of YOLO | by Dmitrii Eliuseev | Jun, 2023