# Continous color table for heatmap

**URL:** <https://discourse.slicer.org/t/continous-color-table-for-heatmap/33464>\
**Category:** Support\
**Created:** [December 20, 2023, 5:52am UTC](https://discourse.slicer.org/t/continous-color-table-for-heatmap/33464 "2023-12-20T05:52:39Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![muratmaga](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/muratmaga/32/3622_2.png) [@muratmaga](https://discourse.slicer.org/u/muratmaga)\
**Post date:** [December 20, 2023, 5:52am UTC](https://discourse.slicer.org/t/continous-color-table-for-heatmap/33464/1 "2023-12-20T05:52:39Z")

</div>

We have an image in which the voxel values are the statistical effect sizes ranging from about -0.3 to 0.9. Most of the image is 0s (voxels that didn’t reach significance). I would like to render this image in a way that negative values are a blue to green ramp (-1, 0), positive values green to red (0, 1), and values of 0 are not shown at all.

I am trying creating color tables but have no luck. I appreciate if someone can guide me.

attached is a sample nRRD file: [Box](https://app.box.com/s/bfr72p0ms3nus79sk4uq0k1hc9u18nvg)

---

<div class="post-metadata">

**Author:** ![muratmaga](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/muratmaga/32/3622_2.png) [@muratmaga](https://discourse.slicer.org/u/muratmaga)\
**Post date:** [December 20, 2023, 6:13am UTC](https://discourse.slicer.org/t/continous-color-table-for-heatmap/33464/2 "2023-12-20T06:13:33Z")

</div>

To give a visual example, this is with my modified color table which supposedly have symmetric values from -2 to 2 with 256 colors.

For whatever reason, the scale bar is displaying values only from -0.1 to 0.3 even though the max and min values for this volume is clearly reported to be -0.3 to 0.9 range

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

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

---

<div class="post-metadata">

**Author:** ![muratmaga](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/muratmaga/32/3622_2.png) [@muratmaga](https://discourse.slicer.org/u/muratmaga)\
**Post date:** [December 20, 2023, 6:45am UTC](https://discourse.slicer.org/t/continous-color-table-for-heatmap/33464/3 "2023-12-20T06:45:46Z")

</div>

I made progress up to a point. Min/Max window level adjustment is really not intuitiuve to work with. So the remaining issue, how can I supress 0 values to be shown in slide views. Since I cannot enter a split range into the threshold value, threshold setting is not very useful.

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

---

<div class="post-metadata">

**Author:** ![muratmaga](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/muratmaga/32/3622_2.png) [@muratmaga](https://discourse.slicer.org/u/muratmaga)\
**Post date:** [December 20, 2023, 6:59am UTC](https://discourse.slicer.org/t/continous-color-table-for-heatmap/33464/4 "2023-12-20T06:59:07Z")

</div>

In volume rendering I can hack something with manually adjusting things. But I need to be able to do this more consistently with more control over colors, and particularly in 2D as well.  
Please provide pointers/suggestions.

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

---

<div class="post-metadata">

**Author:** ![muratmaga](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/muratmaga/32/3622_2.png) [@muratmaga](https://discourse.slicer.org/u/muratmaga)\
**Post date:** [December 21, 2023, 12:05am UTC](https://discourse.slicer.org/t/continous-color-table-for-heatmap/33464/5 "2023-12-21T00:05:21Z")

</div>

Any suggestions to hide 0 values (green background)? @pieper

---

<div class="post-metadata">

**Author:** ![muratmaga](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/muratmaga/32/3622_2.png) [@muratmaga](https://discourse.slicer.org/u/muratmaga)\
**Post date:** [December 21, 2023, 12:44am UTC](https://discourse.slicer.org/t/continous-color-table-for-heatmap/33464/6 "2023-12-21T00:44:05Z")

</div>

I tried setting 0 to NaN, but then in slice views it switch to color of minimum value in the dataset (even though the data probe shows it as NaN)

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

---

<div class="post-metadata">

**Author:** ![muratmaga](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/muratmaga/32/3622_2.png) [@muratmaga](https://discourse.slicer.org/u/muratmaga)\
**Post date:** [December 21, 2023, 9:25pm UTC](https://discourse.slicer.org/t/continous-color-table-for-heatmap/33464/7 "2023-12-21T21:25:25Z")

</div>

We are still looking for a solution. @jcfr @lassoan

---

<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:** [December 22, 2023, 4:05pm UTC](https://discourse.slicer.org/t/continous-color-table-for-heatmap/33464/8 "2023-12-22T16:05:30Z")

</div>

To specify a color transfer function I would recommend using a procedural color node (where you can define any color for any absolute value) instead of a color table (where you specify color values at regular intervals that you later map to some intensity range). See full example in the [script repository](https://slicer.readthedocs.io/en/latest/developer_guide/script_repository.html#create-custom-color-map-and-display-color-legend).

> Any suggestions to hide 0 values (green background)?

Display threshold can be used for hiding background.

---

<div class="post-metadata">

**Author:** ![muratmaga](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/muratmaga/32/3622_2.png) [@muratmaga](https://discourse.slicer.org/u/muratmaga)\
**Post date:** [December 22, 2023, 4:43pm UTC](https://discourse.slicer.org/t/continous-color-table-for-heatmap/33464/9 "2023-12-22T16:43:48Z")

</div>

my values range from negative to positive, zero being in the middle not at the lowest value. How can I just threshold 0, or a range like (-0.0001, 0.00001) ?  
There are values close 0 on either side, and they are not discernible if 0 is rendered in the overlay  
For example this is the full range

 ![image](https://us1.discourse-cdn.com/flex002/uploads/slicer/original/3X/5/4/5413705f867041b9cc1064b8140d5efa18557941.jpeg)

negative only (-0.3 to -0.01)

 ![image](https://us1.discourse-cdn.com/flex002/uploads/slicer/original/3X/6/2/6241ed2a565d3137f8aab9341f2c069d47495da9.jpeg)

positively only (0.01 to 0.9)

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

I want the last two together in the same image.

---

<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:** [December 24, 2023, 2:00pm UTC](https://discourse.slicer.org/t/continous-color-table-for-heatmap/33464/10 "2023-12-24T14:00:03Z")

</div>

Here are two solutions, by modifying an existing discrete color table to be a transparent in the center or a continous mapping function to be black in the center.

## Option A: Discrete color table

- Create a discrete color map that has **transparent** color in the middle and a different color on each side
  - Go to Colors module
  - Select `Colors` → `DivergingBlueRed` (a color function that that uses just 3 colors: blue = low  
values, white = center, red = high values)
  - Click `Copy` button (to create a modifiable copy), accept the proposed default name `DivergingBlueRedCopy`
  - In the color table, for color 127, set `opacity` → `0` (to make the center of the color map transparent, to avoid adding a white haze for values around 0 in the overlay)

- Select this color map for the overlay volume
  - Go to Volumes module
  - Select `Active volume` → `Exp-02...`
  - Select `Lookup Table` → `DivergingBlueRedCopy`
  - Set Window/Level `L` → `0` (to make the center at 0)

- Set this colored image as overlay in the slice view controller
  - Select `Exp-02...` volume as foreground, set its opacity to around 0.5
  - Select `final` volume as background

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

If the abrupt change from transparent to opaque is not desirable then set more colors around the center semitransparent by decreasing their opacity values. It may be easier to do this by editing the color table in a text editor or by Python scripting.

## Option B: Continuous color mapping

Opacity cannot be specified for continuous color mapping, so we will use black as center color and use additive image blending.

- Create a continous color map that has **black** in the middle and a different color on each side
  - Go to Colors module
  - Select `Colors` → `RedGreenBlue` (a color function that that uses just 3 colors: red = low  
values, green = center, blue = high values)
  - Click `Copy` button (to create a modifiable copy), accept the proposed default name `RedGreenBlueCopy`
  - Select the middle point (by entering `Point` → `1` above the color function editor)
  - Set center color to black (click on the colored square next to it, set the value to black using the brightness control bar on the right side, and click OK)

- Select this color map for the overlay volume
  - Go to Volumes module
  - Select `Active volume` → `Exp-02...`
  - Select `Lookup Table` → `RedGreenBlueCopy`
  - Set Window/Level `L` → `0` (to make the center at 0)

- Select additive image overlay display in the slice view controller
  - Select `Exp-02...` volume as foreground, set its opacity to 1
  - Select `final` volume as background
  - Click `Blend mode` icon and choose `Add`

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

* * *

Creating the color table is somewhat tedious, so it could make sense to add a transparent/black centered color table to the presets.
