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!
Plotly's python package is updated frequently. Run pip install plotly --upgrade
to use the latest version.
import plotly
plotly.__version__
'3.9.0'
import plotly.plotly as py
import plotly.graph_objs as go
data = [
go.Scatter(
x = [1,2,3,4,5],
y = [2,1,6,4,4],
text = ["Text A", "Text B", "Text C", "Text D", "Text E"],
hoverinfo = 'text',
marker = dict(
color = 'green'
),
showlegend = False
)
]
py.iplot(data, filename = "add-hover-text")
import plotly.plotly as py
import plotly.graph_objs as go
data = [
go.Scatter(
x = [1,2,3,4,5],
y = [2.02825,1.63728,6.83839,4.8485,4.73463],
hoverinfo = 'y',
marker = dict(
color = 'green'
),
showlegend = False
)
]
layout = go.Layout(
title = "Set hover text formatting<br><a href= https://github.com/d3/d3-time-format/blob/master/README.md#locale_format>https://github.com/d3/d3-time-format/blob/master/README.md#locale_format</a>",
titlefont = dict(
size = 10
),
xaxis = dict(
zeroline = False
),
yaxis = dict(
hoverformat = '.2f'
)
)
fig = go.Figure(data=data,layout=layout)
py.iplot(fig, filename = "format-hover-text")
import plotly.plotly as py
import plotly.graph_objs as go
import plotly.io as pio
data = [
go.Scatter(
x = [1,2,3,4,5],
y = [2.02825,1.63728,6.83839,4.8485,4.73463],
hovertemplate = '<i>Price</i>: $%{y:.2f}'
'<br><b>X</b>: %{x}<br>'
'<b>%{text}</b>',
text = ['Custom text {}'.format(i + 1) for i in range(5)],
showlegend = False
),
go.Scatter(
x = [1,2,3,4,5],
y = [3.02825,2.63728,4.83839,3.8485,1.73463],
hovertemplate = 'Price: %{y:$.2f}<extra></extra>',
showlegend = False
)
]
layout = go.Layout(
title = "Set hover text with hovertemplate",
template = pio.templates['plotly'],
)
fig = go.Figure(data=data,layout=layout)
py.iplot(fig, filename = "hovertemplate-basic")
import plotly.plotly as py
import plotly.graph_objs as go
import plotly.io as pio
import pandas as pd
df = pd.read_csv('https://raw.githubusercontent.com/plotly/datasets/master/job-automation-probability.csv')
data = [
dict(
type = 'scatter',
mode = 'markers',
x = dff['prob'],
y = dff['Average annual wage'],
text = dff['short occupation'],
name = education_level,
marker = dict(size = dff['numbEmployed'], sizeref = 4000, sizemode = 'area'),
hovertemplate = "<b>%{text}</b><br><br>" +
"%{yaxis.title.text}: %{y:$,.0f}<br>" +
"%{xaxis.title.text}: %{x:.0%}<br>" +
"Number Employed: %{marker.size:,}" +
"<extra></extra>"
) for dff, education_level in [(df[df.education == education_level], education_level) for education_level in df.education.unique()]
]
layout = go.Layout(
title = "Higher Risk of Job Automation in Lower Paying Jobs",
template = pio.templates['plotly'],
legend = dict(orientation = 'h', y = -0.3),
xaxis = dict(title = 'Automation Probability'),
yaxis = dict(title = 'Income')
)
fig = dict(data=data,layout=layout)
py.iplot(fig, filename = "hovertemplate-advanced")
Dash is an Open Source Python library which can help you convert plotly figures into a reactive, web-based application. Below is a simple example of a dashboard created using Dash. Its source code can easily be deployed to a PaaS.
from IPython.display import IFrame
IFrame(src= "https://dash-simple-apps.plotly.host/dash-hoverplot/", width="100%", height="920px", frameBorder="0")
from IPython.display import IFrame
IFrame(src= "https://dash-simple-apps.plotly.host/dash-hoverplot/code", width="100%", height=500, frameBorder="0")
See https://plotly.com/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(
'hover-text-and-formatting.ipynb', 'python/hover-text-and-formatting/', 'Hover Text and Formatting',
'How to use hover text and formatting in Python with Plotly.',
title = 'Hover Text and Formatting | Plotly',
has_thumbnail='true', thumbnail='thumbnail/hover-text.png',
language='python',
display_as='file_settings', order=20, ipynb='~notebook_demo/198',
uses_plotly_offline=False)
Collecting git+https://github.com/plotly/publisher.git
Cloning https://github.com/plotly/publisher.git to /private/var/folders/s5/vjqn03zs7nn8zs_fwzcf14r40000gn/T/pip-req-build-6xhb4kuc
Building wheels for collected packages: publisher
Building wheel for publisher (setup.py) ... done
Stored in directory: /private/var/folders/s5/vjqn03zs7nn8zs_fwzcf14r40000gn/T/pip-ephem-wheel-cache-xlgqz02g/wheels/99/3e/a0/fbd22ba24cca72bdbaba53dbc23c1768755fb17b3af0f33966
Successfully built publisher
Installing collected packages: publisher
Found existing installation: publisher 0.13
Uninstalling publisher-0.13:
Successfully uninstalled publisher-0.13
Successfully installed publisher-0.13
You are using pip version 19.0.3, however version 19.1.1 is available.
You should consider upgrading via the 'pip install --upgrade pip' command.