{
"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": [
"#### Version Check\n",
"Note: `Bullet Charts` are available in version 2.2.2+
\n",
"Run `pip install plotly --upgrade` to update your Plotly version"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'2.2.2'"
]
},
"execution_count": 1,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"import plotly\n",
"plotly.__version__"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### Simple Bullet Chart\n",
"Stephen Few's Bullet Chart was invented to replace dashboard gauges and meters, combining both types of charts into simple bar charts with qualitative bars (`ranges`), quantitiative bars (`measures`) and performance points (`markers`) all into one simple layout. `ranges` typically are broken into three values: bad, okay, good, the `measures` are the darker bars that represent the actual values that a particular variable reached, and the points or `markers` usually indicate a goal point relative to the value achieved by the measure bar.\n",
"\n",
"To use this figure factory, you can input either a pandas DataFrame as your data, or a sequence (ex. list, tuple, np.array, etc.) of dictionaries. You must map the column to the name of the particular column or key in your data. For example, if you want column `A` in your DataFrame to be the `measures` column, your function call will look like:\n",
"\n",
"```\n",
"ff.create_bullet(data, measures='A', ...)\n",
"```\n",
"\n",
"The valid params to set your DataFrame columns or dictionary keys to are `titles`, `subtitles`, `ranges`, `measures` and `markers`. The variable for `titles` and `subtitles` must contain strings as its elements and the rest lists."
]
},
{
"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.figure_factory as ff\n",
"\n",
"import pandas as pd\n",
"\n",
"data = pd.read_json('https://cdn.rawgit.com/plotly/datasets/master/BulletData.json')\n",
"\n",
"fig = ff.create_bullet(\n",
" data, markers='markers', measures='measures',\n",
" ranges='ranges', subtitles='subtitle', titles='title',\n",
")\n",
"py.iplot(fig, filename='bullet chart from a dataframe')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### Vertical Bullet Chart\n",
"This example uses a tuple of dictionaries as its data input."
]
},
{
"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.figure_factory as ff\n",
"\n",
"data = (\n",
" {\"label\": \"Revenue\", \"sublabel\": \"US$, in thousands\",\n",
" \"range\": [150, 225, 300], \"performance\": [220,270], \"point\": [250]},\n",
" {\"label\": \"Profit\", \"sublabel\": \"%\", \"range\": [20, 25, 30],\n",
" \"performance\": [21, 23], \"point\": [26]},\n",
" {\"label\": \"Order Size\", \"sublabel\":\"US$, average\",\"range\": [350, 500, 600],\n",
" \"performance\": [100,320],\"point\": [550]},\n",
" {\"label\": \"New Customers\", \"sublabel\": \"count\", \"range\": [1400, 2000, 2500],\n",
" \"performance\": [1000, 1650],\"point\": [2100]},\n",
" {\"label\": \"Satisfaction\", \"sublabel\": \"out of 5\",\"range\": [3.5, 4.25, 5],\n",
" \"performance\": [3.2, 4.7], \"point\": [4.4]}\n",
")\n",
"\n",
"fig = ff.create_bullet(\n",
" data, titles='label', subtitles='sublabel', markers='point',\n",
" measures='performance', ranges='range', orientation='v',\n",
")\n",
"py.iplot(fig, filename='bullet chart from dict')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### Use Your Own Colors\n",
"You can use different colors for the `range` and the `measure` columns. Set `range_colors` and `measure_colors` to a 2-item list of two colors to interpolate between."
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
""
],
"text/plain": [
""
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"import plotly.plotly as py\n",
"import plotly.figure_factory as ff\n",
"\n",
"import pandas as pd\n",
"\n",
"data = pd.read_json('https://cdn.rawgit.com/plotly/datasets/master/BulletData.json')\n",
"\n",
"measure_colors=['rgb(63,102,153)', 'rgb(120,194,195)']\n",
"range_colors=['rgb(245,225,218)', 'rgb(241,241,241)']\n",
"\n",
"\n",
"fig = ff.create_bullet(\n",
" data, orientation='h', markers='markers', measures='measures',\n",
" ranges='ranges', subtitles='subtitle', titles='title',\n",
" range_colors=range_colors,\n",
" measure_colors=measure_colors\n",
")\n",
"py.iplot(fig, filename='bullet chart - custom colors')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### Custom Kwargs"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
""
],
"text/plain": [
""
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"import plotly.plotly as py\n",
"import plotly.figure_factory as ff\n",
"\n",
"import pandas as pd\n",
"\n",
"data = pd.read_json('https://cdn.rawgit.com/plotly/datasets/master/BulletData.json')\n",
"\n",
"fig = ff.create_bullet(\n",
" data, orientation='v', markers='markers', measures='measures',\n",
" ranges='ranges', subtitles='subtitle', titles='title',\n",
" title='lots of kwargs', width=600, showlegend=True,\n",
" scatter_options={'marker': {'size': 30,\n",
" 'color': 'rgb(21, 166, 20)',\n",
" 'symbol': 'hourglass'}}\n",
" \n",
")\n",
"\n",
"# group legends\n",
"chart_elements = 6 # 3 grey bars, 2 blue bars, 1 marker\n",
"for cols, title in enumerate(data['title']):\n",
" for ele in range(chart_elements):\n",
" if ele == 0:\n",
" showlegend = True\n",
" else:\n",
" showlegend = False\n",
" fig['data'][cols * 6 + ele].update(\n",
" {\n",
" 'legendgroup': '{}'.format(title),\n",
" 'name': title,\n",
" 'showlegend': showlegend\n",
" }\n",
" )\n",
"\n",
"py.iplot(fig, filename='bullet chart - custom kwargs')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### Reference"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Help on function create_bullet in module plotly.figure_factory._bullet:\n",
"\n",
"create_bullet(data, markers=None, measures=None, ranges=None, subtitles=None, titles=None, orientation='h', range_colors=('rgb(200, 200, 200)', 'rgb(245, 245, 245)'), measure_colors=('rgb(31, 119, 180)', 'rgb(176, 196, 221)'), horizontal_spacing=None, vertical_spacing=None, scatter_options={'marker': {'color': 'rgb(0, 0, 0)', 'size': 12, 'symbol': 'diamond-tall'}}, **layout_options)\n",
" Returns figure for bullet chart.\n",
" \n",
" :param (pd.DataFrame | list | tuple) data: either a list/tuple of\n",
" dictionaries or a pandas DataFrame.\n",
" :param (str) markers: the column name or dictionary key for the markers in\n",
" each subplot.\n",
" :param (str) measures: the column name or dictionary key for the measure\n",
" bars in each subplot. This bar usually represents the quantitative\n",
" measure of performance, usually a list of two values [a, b] and are\n",
" the blue bars in the foreground of each subplot by default.\n",
" :param (str) ranges: the column name or dictionary key for the qualitative\n",
" ranges of performance, usually a 3-item list [bad, okay, good]. They\n",
" correspond to the grey bars in the background of each chart.\n",
" :param (str) subtitles: the column name or dictionary key for the subtitle\n",
" of each subplot chart. The subplots are displayed right underneath\n",
" each title.\n",
" :param (str) titles: the column name or dictionary key for the main label\n",
" of each subplot chart.\n",
" :param (bool) orientation: if 'h', the bars are placed horizontally as\n",
" rows. If 'v' the bars are placed vertically in the chart.\n",
" :param (list) range_colors: a tuple of two colors between which all\n",
" the rectangles for the range are drawn. These rectangles are meant to\n",
" be qualitative indicators against which the marker and measure bars\n",
" are compared.\n",
" Default=('rgb(200, 200, 200)', 'rgb(245, 245, 245)')\n",
" :param (list) measure_colors: a tuple of two colors which is used to color\n",
" the thin quantitative bars in the bullet chart.\n",
" Default=('rgb(31, 119, 180)', 'rgb(176, 196, 221)')\n",
" :param (float) horizontal_spacing: see the 'horizontal_spacing' param in\n",
" plotly.tools.make_subplots. Ranges between 0 and 1.\n",
" :param (float) vertical_spacing: see the 'vertical_spacing' param in\n",
" plotly.tools.make_subplots. Ranges between 0 and 1.\n",
" :param (dict) scatter_options: describes attributes for the scatter trace\n",
" in each subplot such as name and marker size. Call\n",
" help(plotly.graph_objs.Scatter) for more information on valid params.\n",
" :param layout_options: describes attributes for the layout of the figure\n",
" such as title, height and width. Call help(plotly.graph_objs.Layout)\n",
" for more information on valid params.\n",
" \n",
" Example 1: Use a Dictionary\n",
" ```\n",
" import plotly\n",
" import plotly.plotly as py\n",
" import plotly.figure_factory as ff\n",
" \n",
" data = [\n",
" {\"label\": \"Revenue\", \"sublabel\": \"US$, in thousands\",\n",
" \"range\": [150, 225, 300], \"performance\": [220,270], \"point\": [250]},\n",
" {\"label\": \"Profit\", \"sublabel\": \"%\", \"range\": [20, 25, 30],\n",
" \"performance\": [21, 23], \"point\": [26]},\n",
" {\"label\": \"Order Size\", \"sublabel\":\"US$, average\",\"range\": [350, 500, 600],\n",
" \"performance\": [100,320],\"point\": [550]},\n",
" {\"label\": \"New Customers\", \"sublabel\": \"count\", \"range\": [1400, 2000, 2500],\n",
" \"performance\": [1000, 1650],\"point\": [2100]},\n",
" {\"label\": \"Satisfaction\", \"sublabel\": \"out of 5\",\"range\": [3.5, 4.25, 5],\n",
" \"performance\": [3.2, 4.7], \"point\": [4.4]}\n",
" ]\n",
" \n",
" fig = ff.create_bullet(\n",
" data, titles='label', subtitles='sublabel', markers='point',\n",
" measures='performance', ranges='range', orientation='h',\n",
" title='my simple bullet chart'\n",
" )\n",
" py.iplot(fig)\n",
" ```\n",
" \n",
" Example 2: Use a DataFrame with Custom Colors\n",
" ```\n",
" import plotly.plotly as py\n",
" import plotly.figure_factory as ff\n",
" \n",
" import pandas as pd\n",
" \n",
" data = pd.read_json('https://cdn.rawgit.com/plotly/datasets/master/BulletData.json')\n",
" \n",
" fig = ff.create_bullet(\n",
" data, titles='title', markers='markers', measures='measures',\n",
" orientation='v', measure_colors=['rgb(14, 52, 75)', 'rgb(31, 141, 127)'],\n",
" scatter_options={'marker': {'symbol': 'circle'}}, width=700\n",
" \n",
" )\n",
" py.iplot(fig)\n",
" ```\n",
"\n"
]
}
],
"source": [
"help(ff.create_bullet)"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Collecting git+https://github.com/plotly/publisher.git\n",
" Cloning https://github.com/plotly/publisher.git to /private/var/folders/tc/bs9g6vrd36q74m5t8h9cgphh0000gn/T/pip-UhEHKw-build\n",
"Installing collected packages: publisher\n",
" Found existing installation: publisher 0.11\n",
" Uninstalling publisher-0.11:\n",
" Successfully uninstalled publisher-0.11\n",
" Running setup.py install for publisher ... \u001b[?25ldone\n",
"\u001b[?25hSuccessfully installed publisher-0.11\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
"/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/IPython/nbconvert.py:13: ShimWarning:\n",
"\n",
"The `IPython.nbconvert` package has been deprecated since IPython 4.0. You should import from nbconvert instead.\n",
"\n",
"/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/publisher/publisher.py:53: UserWarning:\n",
"\n",
"Did you \"Save\" this notebook before running this command? Remember to save, always save.\n",
"\n"
]
}
],
"source": [
"! pip install git+https://github.com/plotly/publisher.git --upgrade\n",
"import publisher\n",
"publisher.publish(\n",
" 'bullet-chart.ipynb', 'python/bullet-charts/', 'Bullet Charts',\n",
" \"How to create Stephen Few Bullet Charts in Python with Plotly.\",\n",
" title = 'Python Bullet Charts | plotly',\n",
" has_thumbnail='true', thumbnail='thumbnail/bullet_charts.jpg',\n",
" language='python',\n",
" display_as='statistical', order=9)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": []
}
],
"metadata": {
"anaconda-cloud": {},
"kernelspec": {
"display_name": "Python 2",
"language": "python",
"name": "python2"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 2
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython2",
"version": "2.7.12"
}
},
"nbformat": 4,
"nbformat_minor": 1
}