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

Title: Rectangles Element#

Dependencies: Bokeh

Backends: Matplotlib, Bokeh

import numpy as np
import holoviews as hv

from holoviews import dim

Rectangles represent a collection of axis-aligned rectangles in 2D space. Unlike most 2D elements Rectangles have four key dimensions representing the bottom-left (x0, y0) and top-right (x1, y1) corners of each box.

Rectangles are a convenient and efficient way of drawing multiple boxes:

hv.Rectangles([(0, 0, 1, 1), (2, 3, 4, 6), (0.5, 2, 1.5, 4), (2, 1, 3.5, 2.5)])

Like other elements Rectangles support style mapping, making it possible to map value dimensions to the color, alpha and a variety of other options:

hv.Rectangles([(0, 0, 1, 1, 1), (2, 3, 4, 6, 2), (0.5, 2, 1.5, 4, 3), (2, 1, 3.5, 2.5, 4)], vdims='value').opts(color='value')

Since Rectangles is a low level geometry it can be used to generate complex plot types by composing it with other elements:

xs = np.arange(100)
ys = np.random.randn(101).cumsum()

O = ys[1:]
C = ys[:-1]
H = np.max([O, C], axis=0) + np.random.rand(100)
L = np.min([O, C], axis=0) - np.random.rand(100)

boxes = hv.Rectangles((xs-0.25, O, xs+0.25, C))
segments = hv.Segments((xs, L, xs, H))

# Color boxes where price decreased red and where price increased green
color_exp = (dim('y0')>dim('y1')).categorize({True: 'green', False: 'red'})

boxes.opts(width=1000, color=color_exp, xlabel='Time', ylabel='Price') * segments.opts(color='black')

For full documentation and the available style and plot options, use

This web page was generated from a Jupyter notebook and not all interactivity will work on this website. Right click to download and run locally for full Python-backed interactivity.

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