Population Pyramid in Python [Easy Guide]

Filed Under: Python
Population Pyramid Python

In this tutorial, we will be studying how can we create a population pyramid using the Python programming language.

Also Read: Data Visualization with Python Seaborn and Pandas


Introduction to Population Pyramid

A population pyramid is a plot that shows the age and gender distribution of a population and is useful for understanding the composition and trend in population growth.

Population Pyramid Demonstration
Population Pyramids Demonstration

The pyramid is useful to compare differences between male and female populations. They can also show the number of children and, sometimes, elderly people. Its name is population pyramid because of its graphical shape that resembles a pyramid.


Code Implementation of Population Pyramids in Python

We will start by importing all the necessary modules into the program using the code below. We will be importing Numpy, Pandas, and Matplotlib libraries into the program.

import numpy as np
import pandas as pd 
import matplotlib.pyplot as plt

Next, we will create a dataframe for the pyramid to plot later using the DataFrame function of the pandas module and pass the data along with columns using the code below.

population_data = pd.DataFrame({'Age Group': ['0-9','10-19','20-29','30-39',
                           '40-49','50-59','60-69','70-79','80-89','90+'], 
                    'Male': [9000, 14000, 22000, 26000, 34000, 
                             32000, 29000, 22000, 14000, 3000], 
                    'Female': [8000, 15000, 19000, 28000, 
                               35000, 34000, 28000, 24000, 17000, 5000]})
population_data.head()

To know more about how the dataset looks, we will make use of the head function. The image below displays how the first 5 rows of the final dataset look.

Population Pyramid Dataset
Population Pyramid Dataset

Next, we create the pyramid using the code below. Initially, the code aims to separate out the male and female data and create a subplot to plot inner plots for male and females respectively in the same plot.

We then make use of the barh plots to plot horizontal bar graphs and along with this, we set some other properties for the resulting plots.

y = range(0, len(population_data))
x_male = population_data['Male']
x_female = population_data['Female']

fig, axes = plt.subplots(ncols=2, sharey=True, figsize=(10, 8))
fig.patch.set_facecolor('xkcd:light grey')
plt.figtext(.5,.9,"Population Pyramid", fontsize=15, ha='center')
axes[0].barh(y, x_male, align='center', color='maroon')
axes[0].set(title='Males')
axes[1].barh(y, x_female, align='center', color='magenta')
axes[1].set(title='Females')
axes[1].grid()
axes[0].set(yticks=y, yticklabels = population_data['Age Group'])
axes[0].invert_xaxis()
axes[0].grid()
plt.show()
Population Pyramid Output 1
Population Pyramid Output 1

From the plot, we can see that the distribution of males and females is fairly symmetrical over the dataset we created earlier. We can also observe that most of the population falls in the middle-age group.

One can work with a different random dataset instead of manually entering the population values using the code below. We will make use of random.randint function and generate random population data in a certain range.

population_data = pd.DataFrame({'Age Group': ['0-9','10-19','20-29','30-39',
                           '40-49','50-59','60-69','70-79','80-89','90+'], 
                    'Male': list(np.random.randint(4000,10000,10)), 
                    'Female': list(np.random.randint(4000,10000,10))})
population_data.head()

The dataset looks somewhat like this.

Population Pyramid Random Dataset 1
Population Pyramid Random Dataset 1

Now, we can plot the pyramid in the same way as we did above. The code for the same is below.

y = range(0, len(population_data))
x_male = population_data['Male']
x_female = population_data['Female']

fig, axes = plt.subplots(ncols=2, sharey=True, figsize=(10, 8))
fig.patch.set_facecolor('xkcd:light grey')
plt.figtext(.5,.9,"Population Pyramid", fontsize=15, ha='center')
axes[0].barh(y, x_male, align='center', color='maroon')
axes[0].set(title='Males')
axes[1].barh(y, x_female, align='center', color='magenta')
axes[1].set(title='Females')
axes[1].grid()
axes[0].set(yticks=y, yticklabels = population_data['Age Group'])
axes[0].invert_xaxis()
axes[0].grid()
plt.show()
Population Pyramid Output 2
Population Pyramid Output 2

Conclusion

Congratulations! This tutorial covers all about population pyramids and we also learn about the implementation of the same using the Python programming language. You are free to modify the color scheme based on what you think looks best or work on a different dataset altogether!

Thank you for reading! Hope you like it!


close
Generic selectors
Exact matches only
Search in title
Search in content