Skip to content

Add color bar overlay - #7832

Merged
brisvag merged 167 commits into
napari:mainfrom
brisvag:feature/colormap-overlay
Sep 26, 2025
Merged

brisvag merged 167 commits into
napari:mainfrom
brisvag:feature/colormap-overlay

Conversation

@brisvag

@brisvag brisvag commented Apr 16, 2025

Copy link
Copy Markdown
Contributor

References and relevant issues

Description

Added a colormap/colorbar overlay.

Try with:

import napari

viewer = napari.Viewer()

# enable grid with stride 2 to get layers split two-by-two
viewer.grid.enabled = True
viewer.grid.stride = 2

# set the scale bar to gridded mode so it appears in each grid box
viewer.scale_bar.visible = True
viewer.scale_bar.gridded = True

layers = viewer.open_sample('napari', 'lily')

# enable color bars
for layer in layers:
    layer.color_bar.visible = True

@brisvag
brisvag requested review from a team and melonora as code owners April 16, 2025 09:33
@psobolewskiPhD

Copy link
Copy Markdown
Member

Looking pretty spicy!
One thing I immediately ran into is that it puts both colorbars on top of each other, so you only see the top most.
Well that and the lack of numbers, but you already noted that limitation in the OP.

@brisvag

brisvag commented Apr 16, 2025

Copy link
Copy Markdown
Contributor Author

One thing I immediately ran into is that it puts both colorbars on top of each other, so you only see the top most.

Yeah, this is a pretty big limitation currently with canvas overlays. I've been thinking abotu this since I worked on the unified system using Position.TOP_LEFT etc, and I would like to try to have them "tile". Basically, if you have 3 top_left overlays, they could be put side by side, since we know their dimensions. Might be the right occasion to try and implement this :P

@psobolewskiPhD

Copy link
Copy Markdown
Member

Even having a warning like:
"Overlay foo is already at bottom right, using top right."
(and the behavior) wouldn't be absolutely horrible.
Would need to check scale bar too.

@TimMonko

Copy link
Copy Markdown
Member

Great start! Love this so so so much! I keep seeing this feature request pop up in various places.

  1. I think it's worth trying to sort out a tiling of the colormaps. Logic for moving colormaps should help when trying to implement something like Use Overlays to be able to provide layer names on the canvas (including grid mode) #7321, too!
  2. Could we have some kind of 'box'-like implementation as with the scalebar. For example, it'd be nice to have a white box (or border) because the low values dissapear into the background and you lose the size of the scale.
  3. Can we make the size settable, as with scalebar. Would be nice. E.g., looks a bit small on a large canvas image

Anyways, this is super awesome!

@brisvag

brisvag commented Apr 16, 2025

Copy link
Copy Markdown
Contributor Author

Yes to everything!

@brisvag

brisvag commented Apr 17, 2025

Copy link
Copy Markdown
Contributor Author

I managed to get the tiling to work!

image

import napari
v = napari.Viewer(ndisplay=3)
v.scale_bar.visible = True
ll = v.open_sample('napari', 'cells3d')
for l in ll:
    cm = l._overlays['colormap']
    cm.visible = True
    cm.position = 'top_right'

Cool things that now work as expected:

  • changing order of layers
  • making layers invisible
  • changing position of any canvas overlay (try v.scale_bar.position = 'top_right')

A whole lot of refactoring went into this, so the diff is huge now; I will split out into smaller PRs if we like this approach :)

@brisvag brisvag mentioned this pull request Apr 17, 2025
@brisvag

brisvag commented Apr 17, 2025

Copy link
Copy Markdown
Contributor Author

Ok, I create #7835 and #7836 which respectively refactor the overlay code, and introduce tiling. The diffs are:

main -> refactor: main...brisvag:napari:feature/tiling-overlays2
refactor -> tiling: brisvag/napari@refactor/overlays...brisvag:napari:feature/tiling-overlays2
tiling -> this PR: brisvag/napari@brisvag:napari:feature/tiling-overlays2...feature/colormap-overlay

@github-actions github-actions Bot added the tests Something related to our tests label Apr 17, 2025
@brisvag

brisvag commented Apr 17, 2025

Copy link
Copy Markdown
Contributor Author

And added all sorts of extra details to the colormap!

All these are now settable:

    size: tuple[float, float] = 50, 250
    ticks: bool = True
    n_ticks: int = 4
    tick_length: float = 5
    font_size: float = 7
    color: ColorValue = Field(default_factory=lambda: ColorValue('white'))

image

@brisvag

brisvag commented Sep 25, 2025

Copy link
Copy Markdown
Contributor Author

No, this is wrong. The padding is corrrelty only added to the left because that's just the spacing between the tick and the text, not a "text margin". The reason why it looks bad is again your scaling issues. Looks fine here reverting your change:

image

And if I keep it, I get insane spaces. We need to find a solution that accounts for that weird inconsistent scaling for text :/

@TimMonko

TimMonko commented Sep 25, 2025

Copy link
Copy Markdown
Member

Yay colorbars are fixed!
(I'll post in tiling about the gridded issues))

image
import napari

v = napari.Viewer()

v.scale_bar.visible = True
v.scale_bar.box = True
v.scale_bar.gridded = True

v.text_overlay.visible = True
v.text_overlay.text = 'Points'
v.text_overlay.font_size = 20
v.text_overlay.position = 'bottom_right'

v.grid.enabled = True
v.grid.stride = 2

ll = v.open_sample('napari', 'lily')
for l in ll:
    cm = l._overlays['colormap']
    cm.visible = True
    cm.position = 'top_right'

napari.run()

@brisvag brisvag added the ready to merge Last chance for comments! Will be merged in ~24h label Sep 26, 2025
@brisvag

brisvag commented Sep 26, 2025

Copy link
Copy Markdown
Contributor Author
image

Image for release notes.

@brisvag brisvag changed the title Add colormap overlay Add color bar overlay Sep 26, 2025
@brisvag

brisvag commented Sep 26, 2025

Copy link
Copy Markdown
Contributor Author

Last minute question: layer.color_bar or layer.colorbar? Currently it's color_bar.

@TimMonko

TimMonko commented Sep 26, 2025

Copy link
Copy Markdown
Member

Last minute question: layer.color_bar or layer.colorbar? Currently it's color_bar.

I prefer colorbar, personally. Also, its called colorbar in matplotlib and ggplot2 (R).

@TimMonko TimMonko added the feature New feature or request label Sep 26, 2025
brisvag added a commit that referenced this pull request Sep 26, 2025
# References and relevant issues
Part of the work in #7832.
Depends on #7835.
Diff relative to #7835:
brisvag/napari@refactor/overlays...brisvag:napari:feature/tiling-overlays2

# Description

Canvas overlays can be positioned on the canvas using a limited set of
positions (`top_left`, `bottom_right` and so on).
Currently, if two overlays end up in the same place, they simply
overlap.

This PR introduces tiling for canvas overlays by looping through all
canvas overlays and assigning an (horizontal) offset equal to the space
taken up by all previous overlays.

```py
import napari
v = napari.Viewer()
v.add_points()

v.scale_bar.visible = True

v.text_overlay.visible = True
v.text_overlay.text = 'Some interesting text'
v.text_overlay.position = 'bottom_right'

napari.run()
```

On main:

![image](https://github.com/user-attachments/assets/3920716d-ea88-4555-a661-17f01668c9e2)

With this PR:
<img width="283" height="104" alt="image"
src="https://github.com/user-attachments/assets/6d295077-dc1e-4146-8770-b697532a2501"
/>

---------

Co-authored-by: Grzegorz Bokota <bokota+github@gmail.com>
Co-authored-by: Tim Monko <timmonko@gmail.com>
Co-authored-by: Carol Willing <carolcode@willingconsulting.com>
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
@brisvag
brisvag merged commit e951c0b into napari:main Sep 26, 2025
32 checks passed
@github-actions github-actions Bot removed the ready to merge Last chance for comments! Will be merged in ~24h label Sep 26, 2025
@imagesc-bot

Copy link
Copy Markdown

This pull request has been mentioned on Image.sc Forum. There might be relevant details there:

https://forum.image.sc/t/napari-v0-6-5-is-out/116715/1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

feature New feature or request highlight PR that should be mentioned in next release notes tests Something related to our tests UI/UX

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants