# Display Colour Legend for Surface

**URL:** <https://discourse.slicer.org/t/display-colour-legend-for-surface/22520>\
**Category:** Development\
**Tags:** color\
**Created:** [March 15, 2022, 2:08pm UTC](https://discourse.slicer.org/t/display-colour-legend-for-surface/22520 "2022-03-15T14:08:16Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![connorh](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/connorh/32/79802_2.png) [@connorh](https://discourse.slicer.org/u/connorh)\
**Post date:** [March 15, 2022, 2:08pm UTC](https://discourse.slicer.org/t/display-colour-legend-for-surface/22520/1 "2022-03-15T14:08:16Z")

</div>

Hello,

I’m working on an extension (in python) where I have a surface mesh with attributes that I’m colouring the mesh based on attribute value. I would like to add a colour bar/legend programmatically with the values corresponding to my colours.  
Something similar to this: [Change Color map and add color scale](https://discourse.slicer.org/t/change-color-map-and-add-color-scale/10953)  
Except through the colours module I can only seem to create a colour map for my foreground/background images and not for my vtkMRMLModelNode object.

First time posting, so I hope I’m providing sufficient detail - thanks in advance for any support!  
Connor

---

<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:** [March 15, 2022, 7:22pm UTC](https://discourse.slicer.org/t/display-colour-legend-for-surface/22520/2 "2022-03-15T19:22:30Z")

</div>

For displayed model node you can use “Models” module with “Scalars” and “Color Legend” sub-sections.

 ![image](https://us1.discourse-cdn.com/flex002/uploads/slicer/original/3X/6/2/62106d20878fe3410ff53f560013b2eee07cc865.png)

Major features of “Color Legend” is described here:

> [@"Color Scalar Bar" reworked and upgraded, now it’s called "Color Legend"](https://discourse.slicer.org/t/color-scalar-bar-reworked-and-upgraded-now-it-s-called-color-legend/21567):
>
> Starting from version [30530 build 2022-01-15](https://github.com/Slicer/Slicer/pull/5828/commits/170a0e0edd448fd2c5d8e47ac6de591293fc8354) “Color Scalar Bar” section from “Colors” module was replaced with “Color Legend” section. The “Color Legend” section has been also added to the “Volume”, “Model” and “Markups” modules to control visibility and behavior of color legend. New major features: More unified API. Each Volume, Model, Markups or any other displayable node now can have an individual color legend by means of [vtkMRMLColorLegendDisplayNode](https://apidocs.slicer.org/master/classvtkMRMLColorLegendDisplayNode.html). Displayed LUT is in sy…

---

<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:** [March 16, 2022, 9:12pm UTC](https://discourse.slicer.org/t/display-colour-legend-for-surface/22520/3 "2022-03-16T21:12:18Z")

</div>

If you want to display color legend to a model node then this should be sufficient:

```python
slicer.modules.colors.logic().AddDefaultColorLegendDisplayNode(modelNode)

```

You can find a complete example of displaying a model node with a custom colormap and a color legend in the [script repository](https://slicer.readthedocs.io/en/latest/developer_guide/script_repository.html#create-custom-color-map-and-display-color-legend).

---

<div class="post-metadata">

**Author:** ![connorh](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/connorh/32/79802_2.png) [@connorh](https://discourse.slicer.org/u/connorh)\
**Post date:** [March 31, 2022, 12:59pm UTC](https://discourse.slicer.org/t/display-colour-legend-for-surface/22520/4 "2022-03-31T12:59:35Z")

</div>

Thank you for the help. Unfortunately I keep getting the error, even when following the full example you posted:

> Traceback (most recent call last):  
> File “”, line 1, in   
> AttributeError: ‘vtkSlicerColorsModuleLogicPython.vtkSlicerColorLog’ object has no attribute ‘AddDefaultColorLegendDisplayNode’

I am running the last stable release of Slicer (4.11), do I need to be running 4.13? To make sure it wasn’t just my modelNode doing something weird, I tried [this](https://slicer.readthedocs.io/en/latest/developer_guide/script_repository.html#show-color-legend-for-a-volume-node) with a simple PET dataset and got the same error.

Any suggestions?

---

<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:** [March 31, 2022, 1:00pm UTC](https://discourse.slicer.org/t/display-colour-legend-for-surface/22520/5 "2022-03-31T13:00:38Z")

</div>

Yes, you need to use the latest Slicer Preview Release. This feature has been added recently.
