3D Bounding Box Text Labels ValueError:Pos


I am trying to visualize bounding boxes for 3D images over time with a corresponding text label in Napari. The bounding boxes work, however, with the labels I get “ValueError: pos must have 2 or 3 elements”. This occurs in the Vispy text source code and from my interpretation, the text labels require a 3D coordinate in addition to the label? However, I am not sure where to specify this positioning in Napari. I followed the Annotation Tutorial but am not sure how to proceed. Thanks in advance!

I suspect that this is a bug in our text annotation code. We probably need to subset the text coordinates to remove non-displayed dimensions, and we are probably not doing that. Could you provide us with a minimal code snippet that fails so we can investigate?

Thanks for your reply. Yes, certainly, I hope this is what you need. The Viewer opens but the error then gets thrown once you go across the time dimension.

# 3D images for 20 timesteps
test_img_stack = np.zeros((20,25,500,500))

# create corners sort of centered in the middle of image    
corners = np.random.uniform(100, 300, size=(100, 4, 2))

# time component same for all coordinates
t = np.random.randint(0, 20, size=(100, 1, 1))
t = np.repeat(t, repeats=4, axis=1)

# z coordinate same for all corner points
z = np.random.randint(0, 25, size=(100,1,1))
z= np.repeat(z, repeats=4, axis=1)

shapes = np.concatenate((t, z, corners), axis=2)
# one label per box
labels = np.random.randint(0, 20, (100))

# specify properties and text
properties = {
        'label': labels

text_parameters = {
        'text': 'label: {label}',
        'size': 4,
        'color': 'green',
        'anchor': 'center',

with napari.gui_qt():
        viewer = napari.Viewer()
        viewer.add_shapes(shapes, shape_type='polygon', face_color='transparent',
                     edge_color='green',name='Box', blending='additive',
                     properties=properties, text=text_parameters)
        viewer.add_image(test_img_stack, name='Image', colormap='gray', blending='additive')

Thanks @nleh, I can easily reproduce on my machine. I’ve made an issue to track this bug here:

Hopefully it won’t take too long to fix! :crossed_fingers: