# Population Pyramid in Python [Easy Guide]

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

## 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.

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]})
```

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.

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.barh(y, x_male, align='center', color='maroon')
axes.set(title='Males')
axes.barh(y, x_female, align='center', color='magenta')
axes.set(title='Females')
axes.grid()
axes.set(yticks=y, yticklabels = population_data['Age Group'])
axes.invert_xaxis()
axes.grid()
plt.show()
```

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))})
```

The dataset looks somewhat like this.

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.barh(y, x_male, align='center', color='maroon')
axes.set(title='Males')
axes.barh(y, x_female, align='center', color='magenta')
axes.set(title='Females')
axes.grid()
axes.set(yticks=y, yticklabels = population_data['Age Group'])
axes.invert_xaxis()
axes.grid()
plt.show()
```