A linked streams example demonstrating how to use the BoxDraw stream.
streams, linked, position, interactive
In [ ]:
import numpy as np
import holoviews as hv
from holoviews import streams

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

Add box

Hold shift then click and drag anywhere on the plot.

Move box

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

Delete box

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

As a very straightforward example we will create a Polygons element containing multiple boxes, then attach it as a source to a BoxEdit stream instance. When we now plot the boxes Polygons instance it will add the tool, letting us draw, drag and delete the box polygons:

In [ ]:
%%opts Polygons [width=400 height=400 default_tools=[]] (fill_alpha=0.5)
boxes = hv.Polygons([hv.Box(0, 0, 1), hv.Box(2, 1, 1.5), hv.Box(0.5, 1.5, 1)])
box_stream = streams.BoxEdit(source=boxes)

Whenever an action is executed the geometry data will be synced with Python, both in the notebook and when deployed on the bokeh server. We can access the data directly as columns of the corners of each box:

In [ ]:

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

In [ ]:

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