# Issue displaying color legend in other slice views

**URL:** <https://discourse.slicer.org/t/issue-displaying-color-legend-in-other-slice-views/27784>\
**Category:** Support\
**Created:** [February 12, 2023, 11:43pm UTC](https://discourse.slicer.org/t/issue-displaying-color-legend-in-other-slice-views/27784 "2023-02-12T23:43:50Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![jamesobutler](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/jamesobutler/32/7511_2.png) [@jamesobutler](https://discourse.slicer.org/u/jamesobutler)\
**Post date:** [February 12, 2023, 11:43pm UTC](https://discourse.slicer.org/t/issue-displaying-color-legend-in-other-slice-views/27784/1 "2023-02-12T23:43:50Z")

</div>

I was trying to display a color legend in some custom slice views when I noticed that it was not showing as I expected. Is there a certain set of conditions to make sure the color legend shows in the other non-standard slice views.

I start Slicer 5.2.1 (and latest Slicer Preview 5.3.0-2023-02-10), set layout to Four-Up, close Slicer, restart Slicer then run the below snippet

```python
slicer.app.layoutManager().setLayout(slicer.vtkMRMLLayoutNode.SlicerLayoutThreeOverThreeView)

import SampleData
sampleDataLogic = SampleData.SampleDataLogic()
mrHead = sampleDataLogic.downloadMRHead()

color_legend_display_node = slicer.modules.colors.logic().AddDefaultColorLegendDisplayNode(mrHead)
color_legend_display_node.SetVisibility(True)

```

I observe the following where the color legend is showing in the Red/Yellow/Green slice views, but not in the Red+/Yellow+/Green+ slice views.

 ![image](https://us1.discourse-cdn.com/flex002/uploads/slicer/original/3X/e/e/ee8a147b6bdb4a521e2627a7fde71e4d657d0426.jpeg)

Then if I close Slicer so that it remembers the “Three over three” layout, relaunch Slicer and run the same script I observe the following where the color legend is showing in all the slice views as I would expect:

 ![image](https://us1.discourse-cdn.com/flex002/uploads/slicer/original/3X/e/b/eb0ece809066c4123b7f924b6b747b8c1cd019a3.jpeg)

---

<div class="post-metadata">

**Author:** ![jamesobutler](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/jamesobutler/32/7511_2.png) [@jamesobutler](https://discourse.slicer.org/u/jamesobutler)\
**Post date:** [February 12, 2023, 11:45pm UTC](https://discourse.slicer.org/t/issue-displaying-color-legend-in-other-slice-views/27784/2 "2023-02-12T23:45:14Z")

</div>

@Mik, since you did some of the recent color legend rework, do you know what is going on here?

---

<div class="post-metadata">

**Author:** ![Mik](https://avatars.discourse-cdn.com/v4/letter/m/958977/32.png) [@Mik](https://discourse.slicer.org/u/Mik)\
**Post date:** [February 13, 2023, 12:25pm UTC](https://discourse.slicer.org/t/issue-displaying-color-legend-in-other-slice-views/27784/3 "2023-02-13T12:25:10Z")

</div>

Definitely a bug. SliceCompositeNode is invalid for a new slice views in color legend displayable manager.

For a fast fix:

1. Enable `Toggle slice visibility in 3D view` in slice controller widget  
or
2. Python script below

```python
layoutManager = slicer.app.layoutManager()
layoutManager.setLayout(slicer.vtkMRMLLayoutNode.SlicerLayoutThreeOverThreeView)
import SampleData
sampleDataLogic = SampleData.SampleDataLogic()
mrHead = sampleDataLogic.downloadMRHead()

for sliceViewName in layoutManager.sliceViewNames():
  view = layoutManager.sliceWidget(sliceViewName).sliceView()
  sliceNode = view.mrmlSliceNode()
  sliceLogic = slicer.app.applicationLogic().GetSliceLogic(sliceNode)
  sliceLogic.StartSliceNodeInteraction(slicer.vtkMRMLSliceNode.SliceVisibleFlag)
  sliceNode.SetSliceVisible(True)
  sliceLogic.EndSliceNodeInteraction()

color_legend_display_node = slicer.modules.colors.logic().AddDefaultColorLegendDisplayNode(mrHead)
color_legend_display_node.SetVisibility(True)

```

After that the color legends are visible and available for editing.  
I hope it helps.

---

<div class="post-metadata">

**Author:** ![jamesobutler](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/jamesobutler/32/7511_2.png) [@jamesobutler](https://discourse.slicer.org/u/jamesobutler)\
**Post date:** [February 13, 2023, 1:55pm UTC](https://discourse.slicer.org/t/issue-displaying-color-legend-in-other-slice-views/27784/4 "2023-02-13T13:55:09Z")

</div>

I assume then that this commit is likely the culprit for requiring slice visibility to be on?

> <https://github.com/Slicer/Slicer/pull/6550/commits/c0343f171968766c0bbfec0fe854d8efcb866709>
>
> Color legend is shown if color legend display is enabled for the volume, the vol…ume is shown in a slice view, and that slice view is visible in the 3D view.
> 
> Co-authored-by: Andras Lasso \<lasso@queensu.ca\>

---

<div class="post-metadata">

**Author:** ![Mik](https://avatars.discourse-cdn.com/v4/letter/m/958977/32.png) [@Mik](https://discourse.slicer.org/u/Mik)\
**Post date:** [February 13, 2023, 2:21pm UTC](https://discourse.slicer.org/t/issue-displaying-color-legend-in-other-slice-views/27784/5 "2023-02-13T14:21:11Z")

</div>

Quite possible. I haven’t tested color legends with nonstandard slice views.

---

<div class="post-metadata">

**Author:** ![jamesobutler](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/jamesobutler/32/7511_2.png) [@jamesobutler](https://discourse.slicer.org/u/jamesobutler)\
**Post date:** [February 13, 2023, 5:11pm UTC](https://discourse.slicer.org/t/issue-displaying-color-legend-in-other-slice-views/27784/6 "2023-02-13T17:11:32Z")

</div>

Issue now being tracked at:

> <https://github.com/Slicer/Slicer/issues/6827>
>
> \## Summary
> 
> As originally mentioned at https://discourse.slicer.org/t/issue-di…splaying-color-legend-in-other-slice-views/27784, the color legend display is not working as expected in some of the other slice views other than Red/Yellow/Green. 
> 
> \## Steps to reproduce
> 
> \`\`\`python
> \# Start Slicer so that on startup it is the Four-Up Layout
> slicer.app.layoutManager().setLayout(slicer.vtkMRMLLayoutNode.SlicerLayoutThreeOverThreeView)
> 
> import SampleData
> sampleDataLogic = SampleData.SampleDataLogic()
> mrHead = sampleDataLogic.downloadMRHead()
> 
> color\_legend\_display\_node = slicer.modules.colors.logic().AddDefaultColorLegendDisplayNode(mrHead)
> color\_legend\_display\_node.SetVisibility(True)
> \`\`\`
> If you start Slicer where it begins in the the Three-over-three layout, then the color legend display shows as expected. Something about the views on startup has some impact here.
> 
> \## Expected behavior
> 
> The color legend display should turn on/off for the specified volume in all slice views that the volume is displayed in. It should not be tied to Slice visibility in 3D either.
> 
> \## Current workaround
> \`\`\`python
> slicer.app.layoutManager().setLayout(slicer.vtkMRMLLayoutNode.SlicerLayoutThreeOverThreeView)
> 
> import SampleData
> sampleDataLogic = SampleData.SampleDataLogic()
> mrHead = sampleDataLogic.downloadMRHead()
> 
> \# Workaround - set slice view visibles before turning on color legend display
> layoutManager = slicer.app.layoutManager()
> for sliceViewName in layoutManager.sliceViewNames():
> view = layoutManager.sliceWidget(sliceViewName).sliceView()
> sliceNode = view.mrmlSliceNode()
> sliceNode.SetSliceVisible(True)
> 
> color\_legend\_display\_node = slicer.modules.colors.logic().AddDefaultColorLegendDisplayNode(mrHead)
> color\_legend\_display\_node.SetVisibility(True)
> \`\`\`
> 
> \## Environment
> \- Slicer version: Slicer-5.3.0-2022-02-10 and Slicer 5.2.1.
> \- Operating system: Windows

---

<div class="post-metadata">

**Author:** ![lassoan](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/lassoan/32/13_2.png) [@lassoan](https://discourse.slicer.org/u/lassoan)\
**Post date:** [February 13, 2023, 5:49pm UTC](https://discourse.slicer.org/t/issue-displaying-color-legend-in-other-slice-views/27784/7 "2023-02-13T17:49:22Z")

</div>

@Mik It would be great if you could try to fix this. I’ve added a comment to the issue about potential approaches.

---

<div class="post-metadata">

**Author:** ![Mik](https://avatars.discourse-cdn.com/v4/letter/m/958977/32.png) [@Mik](https://discourse.slicer.org/u/Mik)\
**Post date:** [February 24, 2023, 3:46pm UTC](https://discourse.slicer.org/t/issue-displaying-color-legend-in-other-slice-views/27784/8 "2023-02-24T15:46:48Z")

</div>

Fix is ready to review and tests.

---

<div class="post-metadata">

**Author:** ![Mik](https://avatars.discourse-cdn.com/v4/letter/m/958977/32.png) [@Mik](https://discourse.slicer.org/u/Mik)\
**Post date:** [April 11, 2023, 4:22pm UTC](https://discourse.slicer.org/t/issue-displaying-color-legend-in-other-slice-views/27784/9 "2023-04-11T16:22:06Z")

</div>

Is it possible to update displayable manager data using data from other displayable manager? When we create a non standard slice view and displayable manager for that view we must somehow to get color legend actors from already displayed views (for example from `Red` slice). Since a newly created slice views don’t have such data.

---

<div class="post-metadata">

**Author:** ![lassoan](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/lassoan/32/13_2.png) [@lassoan](https://discourse.slicer.org/u/lassoan)\
**Post date:** [April 11, 2023, 7:01pm UTC](https://discourse.slicer.org/t/issue-displaying-color-legend-in-other-slice-views/27784/10 "2023-04-11T19:01:29Z")

</div>

> [@Mik](#):
>
> Is it possible to update displayable manager data using data from other displayable manager?

In general, no. Displayable managers should be kept independent to keep the overall complexity of the application manageable.

> [@Mik](#):
>
> When we create a non standard slice view

What do you mean by “non-standard”?

> [@Mik](#):
>
> displayable manager for that view we must somehow to get color legend actors from already displayed views (for example from `Red` slice)

Why would you need to get the color legend actors?  
And why would you need to get any information from other displayable managers instead of getting information directly from the MRML scene content? If there is some logic that you don’t want to repeat in multiple displayable managers then that logic can be moved to somewhere the displayable managers can all access it (in MRML display node, MRML logic classes, etc.).

---

<div class="post-metadata">

**Author:** ![Mik](https://avatars.discourse-cdn.com/v4/letter/m/958977/32.png) [@Mik](https://discourse.slicer.org/u/Mik)\
**Post date:** [April 12, 2023, 2:34pm UTC](https://discourse.slicer.org/t/issue-displaying-color-legend-in-other-slice-views/27784/11 "2023-04-12T14:34:40Z")

</div>

> [@lassoan](#):
>
> What do you mean by “non-standard”?

Non-standard (initial) slice views are any slice views created after already displayed color legend.

For example in script

```python
slicer.app.layoutManager().setLayout(slicer.vtkMRMLLayoutNode.SlicerLayoutFourUpView)

import SampleData
sampleDataLogic = SampleData.SampleDataLogic()
mrHead = sampleDataLogic.downloadMRHead()

clDisplayNode = slicer.modules.colors.logic().AddDefaultColorLegendDisplayNode(mrHead)
slicer.app.layoutManager().setLayout(slicer.vtkMRMLLayoutNode.SlicerLayoutThreeOverThreeView)

```

Standard slice views are: `Red`, `Green`, `Yellow`  
Non standard slice views are: `Red+`, `Green+`, `Yellow+`,

When we try to display a volume for a non standard slice views the color legend is invisible, because color legend display node and actors were created before these non standard slice views.

```python
for color in ["Red+", "Yellow+"]:
    slicer.app.layoutManager().sliceWidget(color).sliceLogic().GetSliceCompositeNode().SetForegroundVolumeID(mrHead.GetID())

```

The size of the ColorLegendActorsMap is zero for ColorLegendDisplayableManagers in these newly created slice views.

In order to display color legend we must iether recreate ColorLegendDisplayNode or process ColorLegendDisplayNodes from the scene in some logic or in the displayable manager itself.
