How to Create an Interactive Population Pyramid in Python Using Plotly
A population pyramid is two horizontal bar charts back to back, one per sex, sharing an age axis. In Plotly you build it by plotting one side as negative values, then formatting the axis so those negatives display as positive numbers.
Install and import
The project uses pandas to load the data and Plotly to draw the chart. Install both with pip install pandas plotly, then import them:
# Imports
import pandas as pd
import plotly.graph_objs as go
Load and prepare the data
The example uses the population of Japan in 1950. PopulationPyramid.net offers a CSV download per country and year, which loads straight into a DataFrame:
df = pd.read_csv('Japan-1950.csv')
The Age, M and F columns hold the age groups and the male and female population counts. For the pyramid, one side has to become negative so its bars point the other way. I multiply the female values by -1, so the female bars run to the left and the male bars to the right.
y = df['Age']
x1 = df['M']
x2 = df['F'] * -1
Draw the pyramid
The pyramid is one figure with two horizontal bar traces, one for each sex. orientation='h' puts the age groups on the vertical axis.
# Create Figure
fig = go.Figure()
# Add Trace to figure
fig.add_trace(go.Bar(
y=y,
x=x1,
name='Male',
orientation='h'
))
# Add Trace to figure
fig.add_trace(go.Bar(
y=y,
x=x2,
name='Female',
orientation='h'
))
The layout sets a white template and the title, and barmode='relative' with no gaps lets the two sides meet in one row per age group:
# Update Figure Layout
fig.update_layout(
template = 'plotly_white',
title= 'Age Pyramid Japan 1950',
title_font_size = 24,
barmode='relative',
bargap=0.0,
bargroupgap=0
)
# Plot figure
fig.show()
Fix the negative axis labels
The first version shows negative axis labels on the female side, such as -2M and -4M. The data is right; only the labels are wrong.

The fix is to set your own tick values and give them positive labels. The Japan data counts people rather than millions, so the ticks sit at plus and minus two and four million:
fig.update_layout(
xaxis=dict(
title="Population in Mio",
tickvals=[-4000000, -2000000, 0, 2000000, 4000000],
ticktext=["4M", "2M", "0", "2M", "4M"],
)
)
Complete example
This minimal version uses a few made-up age groups, so you can run it without downloading anything. It follows the same approach as my original script: one horizontal bar trace per side, the female values multiplied by -1, and axis labels that hide the minus sign. It runs on pandas 3.1 and Plotly 7.2.
import pandas as pd
import plotly.graph_objects as go
df = pd.DataFrame({
"Age": ["0-9", "10-19", "20-29", "30-39", "40-49", "50+"],
"M": [11.0, 8.8, 7.0, 5.4, 4.6, 6.0], # millions
"F": [10.6, 8.6, 7.6, 6.2, 4.9, 7.1],
})
fig = go.Figure()
fig.add_trace(go.Bar(y=df["Age"], x=df["M"], name="Male", orientation="h"))
fig.add_trace(go.Bar(y=df["Age"], x=df["F"] * -1, name="Female", orientation="h",
customdata=df["F"], hovertemplate="%{customdata} million"))
ticks = [-10, -5, 0, 5, 10]
fig.update_layout(
title="Population pyramid",
barmode="relative",
bargap=0.05,
xaxis=dict(tickvals=ticks, ticktext=[str(abs(t)) for t in ticks],
title="Population in millions"),
)
fig.write_html("pyramid.html")
To use real data, replace the DataFrame with pd.read_csv("Japan-1950.csv") or pd.read_excel(...), as long as the file has an age column and one column per sex. The three details that make it work:
- orientation=”h” turns the bars sideways, with the age groups on the vertical axis.
- barmode=”relative” lets the negative female bars and the positive male bars share one row per age group instead of sitting next to each other.
- tickvals and ticktext fix the negative numbers on the axis: the ticks stay at -10 and -5, but their labels read 10 and 5. The
customdataandhovertemplatedo the same for the hover text, which would otherwise show the negative value.
The Christmas tree version
Because a pyramid is just a set of Plotly traces, extra traces can decorate it. My version for the video turned it into a Christmas tree with a trunk, colored lights and a star, inside a small Streamlit app.

To show how the population changes year by year, Plotly can animate the chart with an animation_frame, the same idea as in my animated bar chart tutorial. For general styling, the Plotly Express quick fixes apply here as well.
Resources
- Source code and Japan 1950 data on GitHub (a small Streamlit app, updated in November 2026 for current Streamlit and Plotly)
- PopulationPyramid.net
