{
"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": [
"#### Linear Gauge Chart Shell\n",
"Note the following tutorial shows how to create a linear-gauge chart with 4 gauges. It's recommended to use a `width` between 600-1000px and `ticklen` should be `width/20`. These variables are definied in the code below."
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
""
],
"text/plain": [
""
]
},
"execution_count": 2,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"from plotly import tools\n",
"import plotly.plotly as py\n",
"import plotly.graph_objs as go\n",
"\n",
"# Define Titles and Labels for Each Scale\n",
"scales = ['Tension', 'Energy',\n",
" 'Valence', 'Prefer']\n",
"scale1 = ['Very
Calm ', 'Moderately
Calm ',\n",
" 'Slightly
Calm ', 'Neutral ',\n",
" 'Slightly
Tense ', 'Moderately
Tense ',\n",
" 'Very
Tense ']\n",
"scale2 = ['Very
Tired ', 'Moderately
Tired ',\n",
" 'Slightly
Tired ', 'Neutral ',\n",
" 'Slightly
Awake ', 'Moderately
Awake ',\n",
" 'Very
Awake ']\n",
"scale3 = ['Very
Displeased ', 'Moderately
Displeased ',\n",
" 'Slightly
Displeased ', 'Neutral ',\n",
" 'Slightly
Pleased ', 'Moderately
Pleased ',\n",
" 'Very
Pleased ']\n",
"scale4 = ['Strongly
Dislike ', 'Moderately
Dislike ',\n",
" 'Slightly
Dislike ', 'Neutral ',\n",
" 'Slightly
Like ', 'Moderately
Like ',\n",
" 'Strongly
Like ']\n",
"scale_labels = [scale1, scale2, scale3, scale4]\n",
"\n",
"# Add Scale Titles to the Plot\n",
"traces = []\n",
"for i in range(len(scales)):\n",
" traces.append(go.Scatter(\n",
" x=[0.6], # Pad the title - a longer scale title would need a higher value \n",
" y=[6.25],\n",
" text=scales[i],\n",
" mode='text',\n",
" hoverinfo='none',\n",
" showlegend=False,\n",
" xaxis='x'+str(i+1),\n",
" yaxis='y'+str(i+1)\n",
" ))\n",
"\n",
"# Create Scales\n",
"## Since we have 7 lables, the scale will range from 0-6\n",
"shapes = []\n",
"for i in range(len(scales)):\n",
" shapes.append({'type': 'rect',\n",
" 'x0': .02, 'x1': 1.02,\n",
" 'y0': 0, 'y1': 6,\n",
" 'xref':'x'+str(i+1), 'yref':'y'+str(i+1)})\n",
"\n",
"x_domains = [[0, .25], [.25, .5], [.5, .75], [.75, 1]] # Split for 4 scales\n",
"chart_width = 800\n",
"\n",
"# Define X-Axes\n",
"xaxes = []\n",
"for i in range(len(scales)):\n",
" xaxes.append({'domain': x_domains[i], 'range':[0, 4],\n",
" 'showgrid': False, 'showline': False,\n",
" 'zeroline': False, 'showticklabels': False})\n",
"\n",
"# Define Y-Axes (and set scale labels)\n",
"## ticklen is used to create the segments of the scale,\n",
"## for more information see: https://plotly.com/python/reference/#layout-yaxis-ticklen\n",
"yaxes = []\n",
"for i in range(len(scales)):\n",
" yaxes.append({'anchor':'x'+str(i+1), 'range':[-.5,6.5],\n",
" 'showgrid': False, 'showline': False, 'zeroline': False,\n",
" 'ticks':'inside', 'ticklen': chart_width/20,\n",
" 'ticktext':scale_labels[i], 'tickvals':[0., 1., 2., 3., 4., 5., 6.]\n",
" })\n",
"\n",
"# Put all elements of the layout together\n",
"layout = {'shapes': shapes,\n",
" 'xaxis1': xaxes[0],\n",
" 'xaxis2': xaxes[1],\n",
" 'xaxis3': xaxes[2],\n",
" 'xaxis4': xaxes[3],\n",
" 'yaxis1': yaxes[0],\n",
" 'yaxis2': yaxes[1],\n",
" 'yaxis3': yaxes[2],\n",
" 'yaxis4': yaxes[3],\n",
" 'autosize': False,\n",
" 'width': chart_width,\n",
" 'height': 600\n",
"}\n",
"\n",
"### ADD RATING DATA HERE ###\n",
"\n",
"fig = dict(data=traces, layout=layout)\n",
"py.iplot(fig, filename='linear-gauge-layout')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### Add Rating Data\n",
"Ratings should be scaled between 0 - 6 to fit the y-values of the scales created above."
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
""
],
"text/plain": [
""
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"ratings = [4.5, 5, 1, 2.75]\n",
"\n",
"for i in range(len(ratings)):\n",
" traces.append(go.Scatter(\n",
" x=[0.5], y=[ratings[i]],\n",
" xaxis='x'+str(i+1), yaxis='y'+str(i+1),\n",
" mode='markers', marker={'size': 16, 'color': '#29ABD6'},\n",
" text=ratings[i], hoverinfo='text', showlegend=False\n",
" ))\n",
"\n",
"fig = dict(data=traces, layout=layout)\n",
"py.iplot(fig, filename='linear-gauge')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### Reference\n",
"See https://plotly.com/python/reference/ for more information and chart attribute options!"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"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 /private/var/folders/tc/bs9g6vrd36q74m5t8h9cgphh0000gn/T/pip-req-build-6A2FRF\n",
"Building wheels for collected packages: publisher\n",
" Running setup.py bdist_wheel for publisher ... \u001b[?25ldone\n",
"\u001b[?25h Stored in directory: /private/var/folders/tc/bs9g6vrd36q74m5t8h9cgphh0000gn/T/pip-ephem-wheel-cache-IlLSlR/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"
]
},
{
"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": [
"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",
" 'linear-gauge.ipynb', 'python/linear-gauge-chart/', 'Python Linear-Gauge Chart | plotly',\n",
" 'How to make interactive linear-guage charts in Python with Plotly. ',\n",
" title = 'Python Linear-Gauge Chart | plotly',\n",
" name = 'Linear-Gauge Chart',\n",
" thumbnail='thumbnail/linear-gauge.jpg', language='python',\n",
" has_thumbnail='true', display_as='basic', order=12,\n",
" ipynb='~notebook_demo/12')"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": []
}
],
"metadata": {
"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
}