Python program to create a population pyramid using Plotly in Python?

A population pyramid is a graphical representation of data that contains two entities, namely the age and gender of a specific population. It is generally used by demographers to study the population. The age value is divided into sub-categories and the gender column contains the population of that specific gender who belong to this age group. 

It is called the population pyramid because of its graphical shape that resembles a pyramid. In which the people belonging to the youngest age-group is kept at the bottom and the oldest at the top of the graph.

In this article, we will be studying how can we create a population pyramid in Python. To achieve our purpose we will use two additional libraries Pandas and Plotly to plot our graph. If you don’t have these libraries installed, you can install them via pip commands. 

Sure, I'd be happy to help you create a population pyramid using Plotly in Python!

First, you'll need to install the Plotly library. You can do this by running the following command in your terminal or command prompt: 

 

!pip install plotly

Next, let's create a sample dataset for our population pyramid. In this example, we'll use data for the population of the United States in 2020. You can modify this data as per your requirement. 

 

import pandas as pd

# Create a sample dataset
data = pd.DataFrame({
    'age': ['0-4', '5-9', '10-14', '15-19', '20-24', '25-29', '30-34', '35-39', '40-44', '45-49', '50-54', '55-59', '60-64', '65-69', '70-74', '75-79', '80-84', '85+'],
    'male': [1902581, 1990090, 2084956, 2227549, 2393239, 2411170, 2231298, 2127386, 2238260, 2548414, 2561629, 2252336, 1811406, 1288082, 787704, 398845, 149018, 83457],
    'female': [1815816, 1896378, 1998341, 2137061, 2304037, 2340450, 2142683, 2078213, 2220190, 2545424, 2715295, 2465586, 2026956, 1494584, 906196, 486799, 221955, 164020]
})

Now, we can use Plotly to create a population pyramid. Here's the code: 

 

import plotly.graph_objects as go

# Create a figure with two subplots
fig = go.Figure()

fig.add_trace(
    go.Bar(
        y=data['age'],
        x=data['male'],
        orientation='h',
        name='Male',
        marker=dict(color='#4c78a8')
    )
)

fig.add_trace(
    go.Bar(
        y=data['age'],
        x=-data['female'],
        orientation='h',
        name='Female',
        marker=dict(color='#f58518')
    )
)

# Update layout
fig.update_layout(
    barmode='overlay',
    bargap=0.1,
    yaxis=dict(showticklabels=True, tickmode='array', tickvals=data['age'], ticktext=data['age']),
    xaxis=dict(title='Population', side='top'),
    title='Population Pyramid of the United States in 2020',
    font=dict(size=14)
)

# Show the figure
fig.show()

This code creates a bar chart for both male and female populations, with the bars for the female population being shown in the negative x-axis. This is what creates the pyramid shape. The orientation='h' argument specifies that the bars should be horizontal. We also update the layout to add a title and adjust the axes. Finally, we use fig.show() to display the figure.

That's it! You should now have a population pyramid created using Plotly in Python.

Submit Your Programming Assignment Details