Skip to content

Latest commit

 

History

History
198 lines (166 loc) · 6.65 KB

population-pyramid-charts.md

File metadata and controls

198 lines (166 loc) · 6.65 KB
jupyter
jupytext kernelspec plotly
notebook_metadata_filter text_representation
all
extension format_name format_version jupytext_version
.md
markdown
1.1
1.1.1
display_name language name
Python 3
python
python3
description display_as language layout name order page_type permalink thumbnail
How to make Population Pyramid Charts in Python with Plotly.
basic
python
base
Population Pyramid Charts
5.01
u-guide
python/population-pyramid-charts/
thumbnail/pyramid.jpg

New to Plotly?

Plotly's Python library is free and open source! Get started by downloading the client and reading the primer.
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.

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')

Stacked Population Pyramid

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')

Population Pyramid with Binning

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

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')

More Bar and Histogram Examples

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

Reference

See https://plot.ly/python/reference/#bar and https://plot.ly/python/reference/#histogram for more information and chart attribute options!

from IPython.display import display, HTML

display(HTML('<link href="//fonts.googleapis.com/css?family=Open+Sans:600,400,300,200|Inconsolata|Ubuntu+Mono:400,700" rel="stylesheet" type="text/css" />'))
display(HTML('<link rel="stylesheet" type="text/css" href="http://help.plot.ly/documentation/all_static/css/ipython-notebook-custom.css">'))

! pip install git+https://github.com/plotly/publisher.git --upgrade
import publisher
publisher.publish(
    'pyramid-charts.ipynb', 'python/population-pyramid-charts/', 'Python Population Pyramid Charts | Plotly',
    'How to make Population Pyramid Charts in Python with Plotly.',
    title = 'Population Pyramid Charts | Plotly',
    name = 'Population Pyramid Charts',
    thumbnail='thumbnail/pyramid.jpg', language='python',
    has_thumbnail='true', display_as='basic', order=5.01,
    ipynb= '~notebook_demo/221')