Layer units impact rendering#

This example demonstrates how units can impact rendering in napari. Two layers are added with different scales, however by specifying the units, napari is able to render them in the same physical space using the unit-aware library Pint [1]. The first layer is in nanometers and the second layer is in micrometers, but they are rendered in the same physical space because 1 μm = 1000 nm. Units also affect the scale bar, which will display the correct physical unit automatically.

Tags: visualization-advanced, layers

units impact rendering
/home/runner/work/docs/docs/.venv/lib/python3.12/site-packages/napari/_qt/qt_event_loop.py:50: UserWarning: System theme detection requires a Qt6 backend. Please switch to PyQt6 or PySide6 to use it.
  theme_type=get_system_theme(),
/home/runner/work/docs/docs/.venv/lib/python3.12/site-packages/napari/_qt/qt_event_loop.py:50: UserWarning: System theme detection requires a Qt6 backend. Please switch to PyQt6 or PySide6 to use it.
  theme_type=get_system_theme(),

import skimage

import napari

data = skimage.data.cells3d()

membrane = data[:, 0]
nuclei = data[:, 1]

viewer = napari.Viewer()
viewer.scale_bar.visible = True
viewer.add_image(
    membrane,
    name='membrane-nm',
    units=('nm', 'nm', 'nm'),
    scale=(210, 70, 70),
    colormap='magenta'
)
viewer.add_image(
    nuclei,
    name='nuclei-μm',
    units=('μm', 'μm', 'μm'),
    scale=(0.210, 0.07, 0.07),
    colormap='green',
    blending='additive'
)

viewer.dims.ndisplay = 3
viewer.camera.angles = (-20, 20, -20)
viewer.fit_to_view()

if __name__ == '__main__':
    napari.run()

Gallery generated by Sphinx-Gallery