Pointdraw

Title
PointDraw
Description
A linked streams example demonstrating how to use the BoxDraw stream.
Backends
Bokeh
Tags
streams, linked, position, interactive
In [ ]:
import numpy as np
import holoviews as hv
from holoviews import streams
hv.extension('bokeh')

The PointDraw stream adds a bokeh tool to the source plot, which allows drawing, dragging and deleting points and making the drawn data available to Python. The tool supports the following actions:

Add point

Tap anywhere on the plot

Move point

Tap and drag an existing point, the point will be dropped once you let go of the mouse button.

Delete point

Tap a point to select it then press BACKSPACE key while the mouse is within the plot area.

As a simple example we will create a PointDraw stream and attach it to a set of Points with a color dimension. By declaring the data as an OrderedDict and enabling the shared_datasource option on the Layout we can additionally link the Points to a Table . We can now add drag and delete points, see the x/y position change in the table and edit the a color for each point in the table. Additionally the empty_value parameter on the PointDraw stream lets us define the value that will be inserted on columns other than the x/y position, which we can use here to set new points to 'black':

In [ ]:
%%opts Points (color='color' size=10) [tools=['hover'] width=400 height=400] 
%%opts Layout [shared_datasource=True] Table (editable=True)
data = hv.OrderedDict({'x': [0, 0.5, 1], 'y': [0, 0.5, 0], 'color': ['red', 'green', 'blue']})
points = hv.Points(data, vdims=['color']).redim.range(x=(-.1, 1.1), y=(-.1, 1.1))
point_stream = streams.PointDraw(data=points.columns(), source=points, empty_value='black')
points + hv.Table(data, ['x', 'y'], 'color')

Whenever the data source is edited the data is synced with Python, both in the notebook and when deployed on the bokeh server. The data is made available as a dictionary of columns:

In [ ]:
point_stream.data

Alternatively we can use the element property to get an Element containing the returned data:

In [ ]:
point_stream.element

Download this notebook from GitHub (right-click to download).