doc/unconverted/python/linear-gauge-chart.md
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!
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 defined in the code below.
from plotly import tools
import plotly.plotly as py
import plotly.graph_objs as go
# Define Titles and Labels for Each Scale
scales = ['<b>Tension</b>', '<b>Energy</b>',
'<b>Valence</b>', '<b>Prefer</b>']
scale1 = ['Very
Calm ', 'Moderately
Calm ',
'Slightly
Calm ', 'Neutral ',
'Slightly
Tense ', 'Moderately
Tense ',
'Very
Tense ']
scale2 = ['Very
Tired ', 'Moderately
Tired ',
'Slightly
Tired ', 'Neutral ',
'Slightly
Awake ', 'Moderately
Awake ',
'Very
Awake ']
scale3 = ['Very
Displeased ', 'Moderately
Displeased ',
'Slightly
Displeased ', 'Neutral ',
'Slightly
Pleased ', 'Moderately
Pleased ',
'Very
Pleased ']
scale4 = ['Strongly
Dislike ', 'Moderately
Dislike ',
'Slightly
Dislike ', 'Neutral ',
'Slightly
Like ', 'Moderately
Like ',
'Strongly
Like ']
scale_labels = [scale1, scale2, scale3, scale4]
# Add Scale Titles to the Plot
traces = []
for i in range(len(scales)):
traces.append(go.Scatter(
x=[0.6], # Pad the title - a longer scale title would need a higher value
y=[6.25],
text=scales[i],
mode='text',
hoverinfo='none',
showlegend=False,
xaxis='x'+str(i+1),
yaxis='y'+str(i+1)
))
# Create Scales
## Since we have 7 lables, the scale will range from 0-6
shapes = []
for i in range(len(scales)):
shapes.append({'type': 'rect',
'x0': .02, 'x1': 1.02,
'y0': 0, 'y1': 6,
'xref':'x'+str(i+1), 'yref':'y'+str(i+1)})
x_domains = [[0, .25], [.25, .5], [.5, .75], [.75, 1]] # Split for 4 scales
chart_width = 800
# Define X-Axes
xaxes = []
for i in range(len(scales)):
xaxes.append({'domain': x_domains[i], 'range':[0, 4],
'showgrid': False, 'showline': False,
'zeroline': False, 'showticklabels': False})
# Define Y-Axes (and set scale labels)
## ticklen is used to create the segments of the scale,
## for more information see: https://plot.ly/python/reference/#layout-yaxis-ticklen
yaxes = []
for i in range(len(scales)):
yaxes.append({'anchor':'x'+str(i+1), 'range':[-.5,6.5],
'showgrid': False, 'showline': False, 'zeroline': False,
'ticks':'inside', 'ticklen': chart_width/20,
'ticktext':scale_labels[i], 'tickvals':[0., 1., 2., 3., 4., 5., 6.]
})
# Put all elements of the layout together
layout = {'shapes': shapes,
'xaxis1': xaxes[0],
'xaxis2': xaxes[1],
'xaxis3': xaxes[2],
'xaxis4': xaxes[3],
'yaxis1': yaxes[0],
'yaxis2': yaxes[1],
'yaxis3': yaxes[2],
'yaxis4': yaxes[3],
'autosize': False,
'width': chart_width,
'height': 600
}
### ADD RATING DATA HERE ###
fig = dict(data=traces, layout=layout)
py.iplot(fig, filename='linear-gauge-layout')
Ratings should be scaled between 0 - 6 to fit the y-values of the scales created above.
ratings = [4.5, 5, 1, 2.75]
for i in range(len(ratings)):
traces.append(go.Scatter(
x=[0.5], y=[ratings[i]],
xaxis='x'+str(i+1), yaxis='y'+str(i+1),
mode='markers', marker={'size': 16, 'color': '#29ABD6'},
text=ratings[i], hoverinfo='text', showlegend=False
))
fig = dict(data=traces, layout=layout)
py.iplot(fig, filename='linear-gauge')
See https://plot.ly/python/reference/ 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(
'linear-gauge.ipynb', 'python/linear-gauge-chart/', 'Python Linear-Gauge Chart | plotly',
'How to make interactive linear-guage charts in Python with Plotly. ',
title = 'Python Linear-Gauge Chart | plotly',
name = 'Linear-Gauge Chart',
thumbnail='thumbnail/linear-gauge.jpg', language='python',
has_thumbnail='true', display_as='basic', order=12,
ipynb='~notebook_demo/12')