n-dimensional array viewer in Python with fast, interactive multi-dimensional image visualization
—
Napari layers are the fundamental data visualization objects that represent different types of scientific data. Each layer type is optimized for specific data structures and visualization needs, from n-dimensional images to geometric annotations and measurements.
class Layer:
"""Base class for all napari layers."""
@property
def name(self) -> str:
"""Layer name displayed in the layer list."""
@property
def visible(self) -> bool:
"""Whether the layer is currently visible."""
@property
def opacity(self) -> float:
"""Layer opacity (0-1)."""
@property
def blending(self) -> str:
"""Blending mode for layer compositing."""
@property
def data(self):
"""The layer's data array or structure."""Displays n-dimensional image data with support for multi-channel, time-series, and volumetric datasets. Optimized for scientific imaging with customizable colormaps and contrast settings.
class Image(Layer):
"""Layer for displaying n-dimensional image data."""
def __init__(
self,
data,
*,
rgb: bool = None,
colormap: str = 'gray',
contrast_limits: tuple = None,
gamma: float = 1.0,
name: str = None,
**kwargs
):
"""
Create an image layer.
Parameters:
- data: array-like, image data (2D, 3D, 4D, etc.)
- rgb: Whether to interpret last dimension as RGB channels
- colormap: Colormap name or Colormap object
- contrast_limits: (min, max) contrast limits
- gamma: Gamma correction factor
- name: Layer name
"""
@property
def colormap(self):
"""Current colormap for the image."""
@property
def contrast_limits(self) -> tuple:
"""Current contrast limits (min, max)."""
@property
def gamma(self) -> float:
"""Current gamma correction value."""Displays integer-valued label data for segmentation, annotation, and region identification. Supports editing, painting, and region-based analysis.
class Labels(Layer):
"""Layer for displaying integer label data."""
def __init__(
self,
data,
*,
num_colors: int = 50,
seed: float = 0.5,
name: str = None,
**kwargs
):
"""
Create a labels layer.
Parameters:
- data: array-like, integer label data
- num_colors: Number of colors in the colormap
- seed: Random seed for color generation
- name: Layer name
"""
@property
def selected_label(self) -> int:
"""Currently selected label value for painting."""
def paint(self, coord, new_label):
"""Paint a region with a new label value."""
def fill(self, coord, new_label):
"""Fill connected region with new label value."""Displays and manages collections of points in n-dimensional space. Supports interactive editing, sizing, coloring, and symbol customization.
class Points(Layer):
"""Layer for displaying point data."""
def __init__(
self,
data,
*,
properties: dict = None,
size: float = 10,
edge_width: float = 1,
face_color: str = 'white',
edge_color: str = 'black',
symbol: str = 'o',
name: str = None,
**kwargs
):
"""
Create a points layer.
Parameters:
- data: array-like, point coordinates (N x D)
- properties: Dictionary of point properties
- size: Point size(s)
- edge_width: Point border width
- face_color: Point fill color(s)
- edge_color: Point border color(s)
- symbol: Point symbol ('o', 's', 't', etc.)
- name: Layer name
"""
def add(self, coord):
"""Add a new point at the given coordinate."""
def remove_selected(self):
"""Remove currently selected points."""
@property
def selected_data(self) -> set:
"""Indices of currently selected points."""Displays and manages geometric shapes including rectangles, ellipses, polygons, lines, and paths. Supports interactive creation, editing, and styling.
class Shapes(Layer):
"""Layer for displaying geometric shapes."""
def __init__(
self,
data=None,
*,
shape_type: str = 'rectangle',
edge_width: float = 1,
edge_color: str = 'black',
face_color: str = 'white',
opacity: float = 0.7,
name: str = None,
**kwargs
):
"""
Create a shapes layer.
Parameters:
- data: List of shape data arrays
- shape_type: Default shape type for new shapes
- edge_width: Shape border width
- edge_color: Shape border color(s)
- face_color: Shape fill color(s)
- opacity: Shape opacity
- name: Layer name
"""
def add_rectangles(self, data):
"""Add rectangle shapes from coordinate data."""
def add_ellipses(self, data):
"""Add ellipse shapes from coordinate data."""
def add_polygons(self, data):
"""Add polygon shapes from coordinate data."""
def add_lines(self, data):
"""Add line shapes from coordinate data."""
def remove_selected(self):
"""Remove currently selected shapes."""Displays 3D mesh surfaces defined by vertices and faces. Supports texture mapping, lighting, and scientific visualization of 3D structures.
class Surface(Layer):
"""Layer for displaying 3D surface meshes."""
def __init__(
self,
data,
*,
colormap: str = 'gray',
contrast_limits: tuple = None,
gamma: float = 1.0,
name: str = None,
**kwargs
):
"""
Create a surface layer.
Parameters:
- data: Tuple of (vertices, faces, values) or (vertices, faces)
- colormap: Colormap for surface values
- contrast_limits: (min, max) contrast limits for values
- gamma: Gamma correction factor
- name: Layer name
"""
@property
def vertices(self):
"""Surface vertex coordinates."""
@property
def faces(self):
"""Surface face connectivity."""
@property
def values(self):
"""Per-vertex values for coloring."""Displays time-series point data with connections showing movement trajectories over time. Optimized for particle tracking and motion analysis.
class Tracks(Layer):
"""Layer for displaying track data."""
def __init__(
self,
data,
*,
properties: dict = None,
graph: dict = None,
tail_width: float = 2,
tail_length: int = 30,
head_length: float = 0,
name: str = None,
**kwargs
):
"""
Create a tracks layer.
Parameters:
- data: Track data with time, track_id, and coordinates
- properties: Dictionary of track properties
- graph: Graph representation of track connections
- tail_width: Width of track tails
- tail_length: Length of visible track tails
- head_length: Length of track heads (arrows)
- name: Layer name
"""
@property
def graph(self) -> dict:
"""Graph representing track connectivity."""
@property
def tail_width(self) -> float:
"""Width of track tail lines."""
@property
def tail_length(self) -> int:
"""Number of time points to show in tails."""Displays vector data showing direction and magnitude at specific positions. Used for flow fields, gradients, and directional measurements.
class Vectors(Layer):
"""Layer for displaying vector data."""
def __init__(
self,
data,
*,
edge_width: float = 1,
edge_color: str = 'red',
length: float = 1,
name: str = None,
**kwargs
):
"""
Create a vectors layer.
Parameters:
- data: Vector data (positions and directions)
- edge_width: Width of vector arrows
- edge_color: Color of vector arrows
- length: Scaling factor for vector lengths
- name: Layer name
"""
@property
def edge_width(self) -> float:
"""Width of vector lines."""
@property
def length(self) -> float:
"""Vector length scaling factor."""
@property
def edge_color(self):
"""Color of vector arrows."""NAMES: set[str]
"""
Set containing lowercase names of all available layer types.
Includes: 'image', 'labels', 'points', 'shapes', 'surface', 'tracks', 'vectors'
"""import napari
import numpy as np
# 2D grayscale image
image_2d = np.random.random((100, 100))
viewer = napari.Viewer()
viewer.add_image(image_2d, colormap='viridis', name='2D Image')
# 3D RGB image
image_rgb = np.random.random((100, 100, 3))
viewer.add_image(image_rgb, rgb=True, name='RGB Image')
# 4D time-series
image_4d = np.random.random((10, 20, 100, 100))
viewer.add_image(image_4d, name='Time Series')import napari
import numpy as np
# Create label data
labels = np.zeros((100, 100), dtype=int)
labels[25:75, 25:75] = 1
labels[10:40, 10:40] = 2
viewer = napari.Viewer()
viewer.add_labels(labels, name='Segmentation')import napari
import numpy as np
# Random points
points_data = np.random.random((100, 2)) * 100
viewer = napari.Viewer()
viewer.add_points(
points_data,
size=10,
face_color='red',
edge_color='white',
name='Random Points'
)import napari
import numpy as np
# Create different data types
image = np.random.random((100, 100))
labels = np.zeros((100, 100), dtype=int)
labels[25:75, 25:75] = 1
points = np.array([[25, 25], [75, 75], [25, 75], [75, 25]])
# Add all layers to viewer
viewer = napari.Viewer()
viewer.add_image(image, colormap='gray', name='Background')
viewer.add_labels(labels, name='Regions')
viewer.add_points(points, size=15, face_color='cyan', name='Landmarks')
napari.run()Install with Tessl CLI
npx tessl i tessl/pypi-napari