Path

Title
Path Element
Dependencies
Matplotlib
Backends
Matplotlib
Bokeh
In [1]:
import numpy as np
import holoviews as hv
hv.extension('matplotlib')

A Path object is actually a collection of lines, unlike Curve where the y-axis is the dependent variable, a Path consists of lines connecting arbitrary points in two-dimensional space. The individual subpaths should be supplied as a list and will be stored as NumPy arrays, DataFrames or dictionaries for each column, i.e. any of the formats accepted by columnar data formats.

In this example we will create a Lissajous curve, which describe complex harmonic motion:

In [2]:
%%opts Path (color='black' linewidth=4)

lin = np.linspace(0, np.pi*2, 200)

def lissajous(t, a, b, delta):
    return (np.sin(a * t + delta), np.sin(b * t), t)

hv.Path([lissajous(lin, 3, 5, np.pi/2)])
Out[2]:

If you looked carefully the lissajous function actually returns three columns, respectively for the x, y columns and a third column describing the point in time. By declaring a value dimension for that third column we can also color the Path by time. Since the value is cyclical we will also use a cyclic colormap ( 'hsv' ) to represent this variable:

In [3]:
%%opts Path [color_index='time'] (linewidth=4 cmap='hsv')
hv.Path([lissajous(lin, 3, 5, np.pi/2)], vdims='time')
Out[3]:

If we do not provide a color_index overlaid Path elements will cycle colors just like other elements do unlike Curve a single Path element can contain multiple lines that are disconnected from each other. A Path can therefore often useful to draw arbitrary annotations on top of an existing plot.

A Path Element accepts multiple formats for specifying the paths, the simplest of which is passing a list of Nx2 arrays of the x- and y-coordinates, alternative we can pass lists of coordinates. In this example we will create some coordinates representing rectangles and ellipses annotating an RGB image:

In [4]:
%%opts Path (linewidth=4)
angle = np.linspace(0, 2*np.pi, 100)
baby = list(zip(0.15*np.sin(angle),  0.2*np.cos(angle)-0.2))

adultR = [(0.25, 0.45), (0.35,0.35), (0.25, 0.25), (0.15, 0.35), (0.25, 0.45)]
adultL = [(-0.3, 0.4), (-0.3, 0.3), (-0.2, 0.3), (-0.2, 0.4),(-0.3, 0.4)]
scene = hv.RGB.load_image('../assets/penguins.png')

scene * hv.Path([adultL, adultR, baby]) * hv.Path([baby])
Out[4]:

A Path can also be used as a means to display a number of lines with the same sampling along the x-axis at once. If we initialize the Path with a tuple of x-coordinates and stacked y-coordinates, we can quickly view a number of lines at once. Here we will generate a number of random traces each slightly offset along the y-axis:

In [5]:
%%opts Path [aspect=3 fig_size=300]
N, NLINES = 100, 10
hv.Path((np.arange(N), np.random.rand(N, NLINES) + np.arange(NLINES)[np.newaxis, :])) *\
hv.Path((np.arange(N), np.random.rand(N, NLINES) + np.arange(NLINES)[np.newaxis, :]))
Out[5]:

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


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