Labels#
Download this notebook from GitHub (right-click to download).
- Title
- Labels Element
- Dependencies
- Bokeh
- Backends
- Bokeh
- Matplotlib
import numpy as np
import holoviews as hv
from holoviews import opts
hv.extension('bokeh')
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 other 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:
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')
overlay = (points * labels).redim.range(x=(-0.2, 1.2), y=(-.2, 1.2))
overlay.opts(
opts.Labels(text_font_size='10pt', xoffset=0.08),
opts.Points(color='black', size=5))
The same concept also works for other types, e.g. here we will overlay Labels
for every second value on top of a HeatMap
by casting it to a Labels type:
data = [(chr(65+i), chr(97+j), i*j) for i in range(8) for j in range(8) if i!=j]
heatmap = hv.HeatMap(data)
heatmap * hv.Labels(heatmap)
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 text_color
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:
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]
labels = hv.Labels((xs, ys, zs), vdims=value_dimension)
labels.opts(
opts.Labels(bgcolor='black', cmap='magma', text_color='Values', height=400, text_font_size='6pt', width=400))
Download this notebook from GitHub (right-click to download).