Labels

Title : Labels Element

Dependencies Matplotlib

Backends Matplotlib , Bokeh

In [1]:
import numpy as np
import holoviews as hv
hv.extension('matplotlib')

The Labels element may be used to annotate a plot with a number of labels. Unlike the Text element, Labels is vectorized and allows plotting many labels at once. It also supports any tabular or gridded data format. This also means that most other elements may be cast to a Labels element to annotate or label the values.

Labels also support various options that make it convenient to use as an annotation, e.g. xoffset and yoffset options allow adjusting the position of the labels relative to an existing data point and the color_index option allows us to colormap the data by a certain dimension.

In [2]:
np.random.seed(9)
data = np.random.rand(10, 2)
points = hv.Points(data)
labels = hv.Labels({('x', 'y'): data, 'text': [chr(65+i) for i in range(10)]}, ['x', 'y'], 'text').options(
    color_index='text', cmap='Category20', xoffset=0.05, yoffset=0.05, size=14
)

(points.options(color='black', s=25) * labels).redim.range(x=(-0.2, 1.2), y=(-.2, 1.2))
Out[2]:

If the value dimension of the data is not already of string type it will be formatted using the applicable entry in Dimension.type_formatters or an explicit value_format defined on the Dimension. Additionally the color_index option allows us to colormap the text by a dimension.

Here we will create a 2D array of values, define a Dimension with a formatter and then colormap the text:

In [3]:
value_dimension = hv.Dimension('Values', value_format=lambda x: '%.1f' % x)
xs = ys = np.linspace(-2.5, 2.5, 25)
zs = np.sin(xs**2)*np.sin(ys**2)[:, np.newaxis]

hv.Labels((xs, ys, zs), vdims=value_dimension).options(
    bgcolor='black', cmap='viridis', color_index='Values', fig_size=200, size=8
).redim.range(x=(-2.6, 2.6), y=(-2.6, 2.6))
Out[3]:

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