Scalebar#

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


Title: Scalebar#

Dependencies: Bokeh

Backends: Bokeh

The scalebar feature overlays a scale bar on the element to help gauge the size of features on a plot. This is particularly useful for maps, images like CT or MRI scans, and other scenarios where traditional axes might be insufficient.

import holoviews as hv
import numpy as np

hv.extension("bokeh")

pollen = hv.RGB.load_image("../assets/pollen.png", bounds=(-10, -5, 10, 15)).opts(scalebar=True)
pollen

Zoom in and out to see the scale bar dynamically adjust.

Custom Units#

By default, the scalebar uses meters. To customize the units, use the scalebar_unit parameter, which accepts a tuple of two strings: the first for the actual measurement and the second for the base unit that remains invariant regardless of scale. In the example below, the y-axis unit is micro-volts (µV), and the base unit is Volts (V).

You can also apply a unit to the y-label independently of the scale bar specification using hv.Dimension.

dim = hv.Dimension('Voltage', unit='µV')
hv.Curve(np.random.rand(1000), ['time'], [dim]).opts(
    width=400,
    scalebar=True,
    scalebar_range='y',
    scalebar_unit=('µV', 'V'),
)

Customization#

In the plot above, you can see that we applied the scalebar to the y-axis by specifying the scalebar_range argument. Below are further customization options for the scalebar:

  • The scalebar_location parameter defines the positioning anchor for the scalebar, with options like “bottom_right”, “top_left”, “center”, etc.

  • The scalebar_label parameter allows customization of the label template, using variables such as @{value} and @{unit}.

  • The scalebar_opts parameter enables specific styling options for the scalebar, as detailed in the Bokeh’s documentation.

All these parameters are only utilized if scalebar is set to True in .opts().

dim = hv.Dimension('Voltage', unit='µV')
hv.Curve(np.random.rand(1000), ['time'], [dim]).opts(
    color='lightgrey',
    width=400,
    scalebar=True,
    scalebar_range='y',
    scalebar_unit=('µV', 'V'),
    scalebar_location = 'top_right',
    scalebar_label = '@{value} [@{unit}]',
    scalebar_opts={
        'background_fill_alpha': 0,
        'border_line_color': None,
        'label_text_font_size': '20px',
        'label_text_color': 'maroon',
        'label_text_alpha': .5,
        'label_location': 'left',
        'length_sizing': 'exact',
        'bar_length': 0.5,
        'bar_line_color': 'maroon',
        'bar_line_alpha': .5,        
        'bar_line_width': 5,
        'margin': 0,
        'padding': 5,
    },
)

Toolbar#

The scalebar tool is added to the toolbar with a measurement ruler icon. Toggling this icon will either hide or show the scalebars. To remove scalebar icon from the toolbar, set scalebar_tool = False.

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).