[ad_1]
Making a grid with a number of nations
You should use plt.subplots()
to create grids, however on this tutorial, I need to create a grid of photos as a result of I believe it appears to be like higher.
The next operate takes a listing of photos and creates a grid with ncols
. It really works by creating an empty picture with a single background coloration that’s giant sufficient to suit all figures.
def create_grid(figures, pad, ncols):
nrows = int(len(figures) / ncols)
measurement = figures[0].measurementpicture = 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 a listing of nations, add the ensuing graph to figures
, and create a grid by working 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,
12 months="2021"
)
ax.set(xlim=(-10, 10))
# New capabilities
format_ticks(ax, xformat="share")
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)
Observe that I take advantage of ratios as a substitute of absolute numbers and set xlim=(-10, 10)
. In any other case, I received’t have the ability to evaluate the nations to one another visually.
Let’s transfer on to the final a part of this tutorial — Learn how 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 alter over time.
Since we’ve got precise values from 1960 to 2021 and predictions to 2050, we are able to create a time-lapse animation for a comparatively lengthy interval.
Earlier than we start, I must let you know that the font I take advantage of, 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 12 months as a substitute of plt.title()
. If you happen to use one other font, it’s not crucial to take action.
Right here’s the code:
photos = []
years = checklist(population_male.columns[4:])for 12 months in years:
fig = plt.determine(figsize=(10, 8))
ax = create_age_distribution(
female_df=population_female,
male_df=population_male,
nation="World",
12 months=12 months
)
# New capabilities
format_ticks(ax, xformat="tens of millions", 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(12 months), fontsize=21, remodel=ax.transAxes)
picture = create_image_from_figure(fig)
picture = add_padding_to_chart(picture, 20, 20, 20, 5, background_color)
photos.append(picture)
I take advantage of imageio
to create a GIF from the checklist of photos.
# Duplicating the final frames so as to add a delay
# earlier than the animation restarts
photos = photos + [images[-1] for _ in vary(20)]
imageio.mimwrite('./time-lapse.gif', photos, period=0.2)
Let’s check out the consequence.
Superior! That’s all for this tutorial; let me know when you favored it and realized one thing helpful.
[ad_2]
Source link