Show Sidebar Hide Sidebar

# Population Pyramid Charts in Python

How to make Population Pyramid Charts in Python with Plotly.

#### New to Plotly?¶

You can set up Plotly to work in online or offline mode, or in jupyter notebooks.
We also have a quick-reference cheatsheet (new!) to help you get started!

#### Basic Population Pyramid Chart¶

If you're starting with binned data, use a go.Bar trace.

In [1]:
import plotly.plotly as py
import plotly.graph_objs as go

import numpy as np

women_bins = np.array([-600, -623, -653, -650, -670, -578, -541, -411, -322, -230])
men_bins = np.array([600, 623, 653, 650, 670, 578, 541, 360, 312, 170])

y = list(range(0, 100, 10))

layout = go.Layout(yaxis=go.layout.YAxis(title='Age'),
xaxis=go.layout.XAxis(
range=[-1200, 1200],
tickvals=[-1000, -700, -300, 0, 300, 700, 1000],
ticktext=[1000, 700, 300, 0, 300, 700, 1000],
title='Number'),
barmode='overlay',
bargap=0.1)

data = [go.Bar(y=y,
x=men_bins,
orientation='h',
name='Men',
hoverinfo='x',
marker=dict(color='powderblue')
),
go.Bar(y=y,
x=women_bins,
orientation='h',
name='Women',
text=-1 * women_bins.astype('int'),
hoverinfo='text',
marker=dict(color='seagreen')
)]

py.iplot(dict(data=data, layout=layout), filename='EXAMPLES/bar_pyramid')

Out[1]:

#### Stacked Population Pyramid¶

In [2]:
import plotly.plotly as py
import plotly.graph_objs as go

import numpy as np

women_bins = np.array([-600, -623, -653, -650, -670, -578, -541, -411, -322, -230])
men_bins = np.array([600, 623, 653, 650, 670, 578, 541, 360, 312, 170])
women_with_dogs_bins = np.array([-0, -3, -308, -281, -245, -231, -212, -132, -74, -76])
men_with_dogs_bins = np.array([0, 1, 300, 273, 256, 211, 201, 170, 145, 43])

y = list(range(0, 100, 10))

layout = go.Layout(yaxis=go.layout.YAxis(title='Age'),
xaxis=go.layout.XAxis(
range=[-1200, 1200],
tickvals=[-1000, -700, -300, 0, 300, 700, 1000],
ticktext=[1000, 700, 300, 0, 300, 700, 1000],
title='Number'),
barmode='overlay',
bargap=0.1)

data = [go.Bar(y=y,
x=men_bins,
orientation='h',
name='Men',
hoverinfo='x',
marker=dict(color='powderblue')
),
go.Bar(y=y,
x=women_bins,
orientation='h',
name='Women',
text=-1 * women_bins.astype('int'),
hoverinfo='text',
marker=dict(color='seagreen')
),
go.Bar(y=y,
x=men_with_dogs_bins,
orientation='h',
hoverinfo='x',
showlegend=False,
opacity=0.5,
marker=dict(color='teal')
),
go.Bar(y=y,
x=women_with_dogs_bins,
orientation='h',
text=-1 * women_bins.astype('int'),
hoverinfo='text',
showlegend=False,
opacity=0.5,
marker=dict(color='darkgreen')
)]

py.iplot(dict(data=data, layout=layout), filename='EXAMPLES/stacked_bar_pyramid')

Out[2]:

#### Population Pyramid with Binning¶

If you want to quickly create a Population Pyramid from raw data, try go.Histogram.

In [3]:
import plotly.plotly as py
import plotly.graph_objs as go

import numpy as np

layout = go.Layout(barmode='overlay',
yaxis=go.layout.YAxis(range=[0, 90], title='Age'),
xaxis=go.layout.XAxis(
tickvals=[-150, -100, -50, 0, 50, 100, 150],
ticktext=[150, 100, 50, 0, 50, 100, 150],
title='Number'))

data = [go.Histogram(
y=np.random.exponential(50, 1000),
orientation='h',
name='Men',
marker=dict(color='plum'),
hoverinfo='skip'
),
go.Histogram(
y=np.random.exponential(55, 1000),
orientation='h',
name='Women',
marker=dict(color='purple'),
hoverinfo='skip',
x=-1 * np.ones(1000),
histfunc="sum"
)
]

py.iplot(dict(data=data, layout=layout), filename='EXAMPLES/histogram_pyramid')

Out[3]:

### More Bar and Histogram Examples¶

See more examples of horizontal bar charts, bar charts and histograms.