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!
import plotly.graph_objs as go
import numpy as np
x = np.random.rand(100)
y = np.random.rand(100)
f = go.FigureWidget([go.Scatter(x=x, y=y, mode='markers')])
scatter = f.data[0]
colors = ['#a3a7e4'] * 100
scatter.marker.color = colors
scatter.marker.size = [10] * 100
f.layout.hovermode = 'closest'
# create our callback function
def update_point(trace, points, selector):
c = list(scatter.marker.color)
s = list(scatter.marker.size)
for i in points.point_inds:
c[i] = '#bae2be'
s[i] = 20
scatter.marker.color = c
scatter.marker.size = s
scatter.on_click(update_point)
f
See these Jupyter notebooks for even more FigureWidget examples.
help(scatter.on_click)
Help on method on_click in module plotly.basedatatypes: on_click(self, callback, append=False) method of plotly.graph_objs._scatter.Scatter instance Register function to be called when the user clicks on one or more points in this trace. Note: Callbacks will only be triggered when the trace belongs to a instance of plotly.graph_objs.FigureWidget and it is displayed in an ipywidget context. Callbacks will not be triggered on figures that are displayed using plot/iplot. Parameters ---------- callback Callable function that accepts 3 arguments - this trace - plotly.callbacks.Points object - plotly.callbacks.InputDeviceState object append : bool If False (the default), this callback replaces any previously defined on_click callbacks for this trace. If True, this callback is appended to the list of any previously defined callbacks. Returns ------- None Examples -------- >>> from plotly.callbacks import Points, InputDeviceState >>> points, state = Points(), InputDeviceState() >>> def click_fn(trace, points, state): ... inds = points.point_inds ... # Do something >>> trace.on_click(click_fn) Note: The creation of the `points` and `state` objects is optional, it's simply a convenience to help the text editor perform completion on the arguments inside `click_fn`
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(
'figurewidget-click-event.ipynb', 'python/click-events/', 'Click Events with go.FigureWidget',
'Click Events With FigureWidget',
title = 'Click Events',
name = 'Click Events',
has_thumbnail='true', thumbnail='thumbnail/figurewidget-click-events.gif',
language='python', page_type='example_index',
display_as='chart_events', order=24,
ipynb= '~notebook_demo/240')
Collecting git+https://github.com/plotly/publisher.git
Cloning https://github.com/plotly/publisher.git to /tmp/pip-req-build-fyqWMB
Building wheels for collected packages: publisher
Running setup.py bdist_wheel for publisher ... done
Stored in directory: /tmp/pip-ephem-wheel-cache-HH5VVg/wheels/99/3e/a0/fbd22ba24cca72bdbaba53dbc23c1768755fb17b3af0f33966
Successfully built publisher
Installing collected packages: publisher
Found existing installation: publisher 0.11
Uninstalling publisher-0.11:
Successfully uninstalled publisher-0.11
Successfully installed publisher-0.11
You are using pip version 10.0.1, however version 18.0 is available.
You should consider upgrading via the 'pip install --upgrade pip' command.