{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "#### New to Plotly?\n", "Plotly's Python library is free and open source! [Get started](https://plotly.com/python/getting-started/) by downloading the client and [reading the primer](https://plotly.com/python/getting-started/).\n", "
You can set up Plotly to work in [online](https://plotly.com/python/getting-started/#initialization-for-online-plotting) or [offline](https://plotly.com/python/getting-started/#initialization-for-offline-plotting) mode, or in [jupyter notebooks](https://plotly.com/python/getting-started/#start-plotting-online).\n", "
We also have a quick-reference [cheatsheet](https://images.plot.ly/plotly-documentation/images/python_cheat_sheet.pdf) (new!) to help you get started!" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Basic Population Pyramid Chart\n", "If you're starting with binned data, use a `go.Bar` trace." ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "data": { "text/html": [ "" ], "text/plain": [ "" ] }, "execution_count": 1, "metadata": {}, "output_type": "execute_result" } ], "source": [ "import plotly.plotly as py\n", "import plotly.graph_objs as go\n", "\n", "import numpy as np\n", "\n", "women_bins = np.array([-600, -623, -653, -650, -670, -578, -541, -411, -322, -230])\n", "men_bins = np.array([600, 623, 653, 650, 670, 578, 541, 360, 312, 170])\n", "\n", "y = list(range(0, 100, 10))\n", "\n", "layout = go.Layout(yaxis=go.layout.YAxis(title='Age'),\n", " xaxis=go.layout.XAxis(\n", " range=[-1200, 1200],\n", " tickvals=[-1000, -700, -300, 0, 300, 700, 1000],\n", " ticktext=[1000, 700, 300, 0, 300, 700, 1000],\n", " title='Number'),\n", " barmode='overlay',\n", " bargap=0.1)\n", "\n", "data = [go.Bar(y=y,\n", " x=men_bins,\n", " orientation='h',\n", " name='Men',\n", " hoverinfo='x',\n", " marker=dict(color='powderblue')\n", " ),\n", " go.Bar(y=y,\n", " x=women_bins,\n", " orientation='h',\n", " name='Women',\n", " text=-1 * women_bins.astype('int'),\n", " hoverinfo='text',\n", " marker=dict(color='seagreen')\n", " )]\n", "\n", "py.iplot(dict(data=data, layout=layout), filename='EXAMPLES/bar_pyramid') " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Stacked Population Pyramid" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "data": { "text/html": [ "" ], "text/plain": [ "" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "import plotly.plotly as py\n", "import plotly.graph_objs as go\n", "\n", "import numpy as np\n", "\n", "women_bins = np.array([-600, -623, -653, -650, -670, -578, -541, -411, -322, -230])\n", "men_bins = np.array([600, 623, 653, 650, 670, 578, 541, 360, 312, 170])\n", "women_with_dogs_bins = np.array([-0, -3, -308, -281, -245, -231, -212, -132, -74, -76])\n", "men_with_dogs_bins = np.array([0, 1, 300, 273, 256, 211, 201, 170, 145, 43])\n", "\n", "y = list(range(0, 100, 10))\n", "\n", "layout = go.Layout(yaxis=go.layout.YAxis(title='Age'),\n", " xaxis=go.layout.XAxis(\n", " range=[-1200, 1200],\n", " tickvals=[-1000, -700, -300, 0, 300, 700, 1000],\n", " ticktext=[1000, 700, 300, 0, 300, 700, 1000],\n", " title='Number'),\n", " barmode='overlay',\n", " bargap=0.1)\n", "\n", "data = [go.Bar(y=y,\n", " x=men_bins,\n", " orientation='h',\n", " name='Men',\n", " hoverinfo='x',\n", " marker=dict(color='powderblue')\n", " ),\n", " go.Bar(y=y,\n", " x=women_bins,\n", " orientation='h',\n", " name='Women',\n", " text=-1 * women_bins.astype('int'),\n", " hoverinfo='text',\n", " marker=dict(color='seagreen')\n", " ),\n", " go.Bar(y=y,\n", " x=men_with_dogs_bins,\n", " orientation='h',\n", " hoverinfo='x',\n", " showlegend=False,\n", " opacity=0.5,\n", " marker=dict(color='teal')\n", " ),\n", " go.Bar(y=y,\n", " x=women_with_dogs_bins,\n", " orientation='h',\n", " text=-1 * women_bins.astype('int'),\n", " hoverinfo='text',\n", " showlegend=False,\n", " opacity=0.5,\n", " marker=dict(color='darkgreen')\n", " )]\n", "\n", "py.iplot(dict(data=data, layout=layout), filename='EXAMPLES/stacked_bar_pyramid')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Population Pyramid with Binning\n", "If you want to quickly create a Population Pyramid from raw data, try `go.Histogram`." ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "data": { "text/html": [ "" ], "text/plain": [ "" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "import plotly.plotly as py\n", "import plotly.graph_objs as go\n", "\n", "import numpy as np\n", "\n", "layout = go.Layout(barmode='overlay',\n", " yaxis=go.layout.YAxis(range=[0, 90], title='Age'),\n", " xaxis=go.layout.XAxis(\n", " tickvals=[-150, -100, -50, 0, 50, 100, 150],\n", " ticktext=[150, 100, 50, 0, 50, 100, 150],\n", " title='Number'))\n", "\n", "data = [go.Histogram(\n", " y=np.random.exponential(50, 1000),\n", " orientation='h',\n", " name='Men',\n", " marker=dict(color='plum'),\n", " hoverinfo='skip'\n", "),\n", " go.Histogram(\n", " y=np.random.exponential(55, 1000),\n", " orientation='h',\n", " name='Women',\n", " marker=dict(color='purple'),\n", " hoverinfo='skip',\n", " x=-1 * np.ones(1000),\n", " histfunc=\"sum\"\n", " )\n", "]\n", "\n", "py.iplot(dict(data=data, layout=layout), filename='EXAMPLES/histogram_pyramid')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### More Bar and Histogram Examples\n", "See more examples of [horizontal bar charts](https://plotly.com/python/horizontal-bar-charts/), [bar charts](https://plotly.com/python/bar-charts/) and [histograms](https://plotly.com/python/histograms/)." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Reference\n", "See https://plotly.com/python/reference/#bar and https://plotly.com/python/reference/#histogram for more information and chart attribute options!" ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "scrolled": true }, "outputs": [ { "data": { "text/html": [ "" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [ "" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "name": "stdout", "output_type": "stream", "text": [ "Collecting git+https://github.com/plotly/publisher.git\n", " Cloning https://github.com/plotly/publisher.git to /tmp/pip-req-build-yN4EUd\n", "Building wheels for collected packages: publisher\n", " Running setup.py bdist_wheel for publisher ... \u001b[?25ldone\n", "\u001b[?25h Stored in directory: /tmp/pip-ephem-wheel-cache-4E5A3k/wheels/99/3e/a0/fbd22ba24cca72bdbaba53dbc23c1768755fb17b3af0f33966\n", "Successfully built publisher\n", "Installing collected packages: publisher\n", " Found existing installation: publisher 0.11\n", " Uninstalling publisher-0.11:\n", " Successfully uninstalled publisher-0.11\n", "Successfully installed publisher-0.11\n", "\u001b[33mYou are using pip version 10.0.1, however version 18.0 is available.\n", "You should consider upgrading via the 'pip install --upgrade pip' command.\u001b[0m\n" ] } ], "source": [ "from IPython.display import display, HTML\n", "\n", "display(HTML(''))\n", "display(HTML(''))\n", "\n", "! pip install git+https://github.com/plotly/publisher.git --upgrade\n", "import publisher\n", "publisher.publish(\n", " 'pyramid-charts.ipynb', 'python/population-pyramid-charts/', 'Python Population Pyramid Charts | Plotly',\n", " 'How to make Population Pyramid Charts in Python with Plotly.',\n", " title = 'Population Pyramid Charts | Plotly',\n", " name = 'Population Pyramid Charts',\n", " thumbnail='thumbnail/pyramid.jpg', language='python',\n", " has_thumbnail='true', display_as='basic', order=5.01,\n", " ipynb= '~notebook_demo/221')" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.6.5" } }, "nbformat": 4, "nbformat_minor": 2 }