Rectangles

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


Title: Rectangles Element

Dependencies: Bokeh

Backends: Matplotlib, Bokeh

In [1]:
import numpy as np
import holoviews as hv

from holoviews import dim
hv.extension('bokeh')

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:

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

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

In [3]:
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')
Out[3]:

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

In [4]:
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')
Out[4]:

For full documentation and the available style and plot options, use hv.help(hv.Rectangles).


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