# Select color range for heatmap

**URL:** <https://discourse.julialang.org/t/select-color-range-for-heatmap/81589>\
**Category:** New to Julia\
**Tags:** plotting, plots, colorbar\
**Created:** [May 24, 2022, 2:00pm UTC](https://discourse.julialang.org/t/select-color-range-for-heatmap/81589 "2022-05-24T14:00:55Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![vamp](https://avatars.discourse-cdn.com/v4/letter/v/f4b2a3/32.png) [@vamp](https://discourse.julialang.org/u/vamp)\
**Post date:** [May 24, 2022, 2:00pm UTC](https://discourse.julialang.org/t/select-color-range-for-heatmap/81589/1 "2022-05-24T14:00:55Z")

</div>

Hello,

I want to plot the values of a “big” matrix using plots. The matrix is on a .txt file and I do the following:

```julia
using DelimitedFiles, Plots, LinearAlgebra, Pyplot
writedlm( "MyFile.txt", values, ' ')
t1 = readdlm("MyFile.txt")
t2 = heatmap(t1 , xlabel="X", ylabel="Y",title = "Title", yflip=true, axis=false, c =:ocean)
savefig(t2,"MyFigure.svg")

```

When I use `c =:ocean` it selects the color using linear interpolation. How can I define for example:

- If value is between 0 and 10 red.
- If value is between 10 and 100 blue.
- If value is between 100 and 1000 purple.

Thanks!

---

<div class="post-metadata">

**Author:** ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)\
**Post date:** [May 24, 2022, 4:13pm UTC](https://discourse.julialang.org/t/select-color-range-for-heatmap/81589/2 "2022-05-24T16:13:08Z")

</div>

One simple way:

```julia
using Plots; gr()
m = 1000*rand(200,200)
c = cgrad([:red,:blue,:purple], [0.010, 0.100], categorical = true)
heatmap(m, color=c, clim=(0,1000))

```

 ![Plots_heatmap_colorbar_cgrad_scale_function](https://global.discourse-cdn.com/julialang/original/3X/1/c/1cf94d0b35bfc3ad5481d3600d99bd8414e9d42a.png)

---

<div class="post-metadata">

**Author:** ![vamp](https://avatars.discourse-cdn.com/v4/letter/v/f4b2a3/32.png) [@vamp](https://discourse.julialang.org/u/vamp)\
**Post date:** [May 26, 2022, 2:34pm UTC](https://discourse.julialang.org/t/select-color-range-for-heatmap/81589/3 "2022-05-26T14:34:26Z")

</div>

Hello,

What if values are negative?

For example:

- If smaller than -1000 red.

- If bigger than -1000 but smaller than 0 yellow.

- If 0 green.

Thanks!

---

<div class="post-metadata">

**Author:** ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)\
**Post date:** [May 26, 2022, 2:43pm UTC](https://discourse.julialang.org/t/select-color-range-for-heatmap/81589/4 "2022-05-26T14:43:01Z")

</div>

Not mapping exactly `0` to green but all floats between -1 and 0 as per your wishes:

```julia
using Plots; gr()
m = -2000*(rand(200,200))
c = cgrad([:red,:yellow,:green], [0.50, 0.9995], categorical = true)
heatmap(m, color=c, clim=(-2000,0))

```

 ![Plots_heatmap_colorbar_categorical](https://global.discourse-cdn.com/julialang/original/3X/c/1/c1e0ba64b13dc171e32d4a1b58266fc9f459d79c.png)

---

<div class="post-metadata">

**Author:** ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)\
**Post date:** [May 26, 2022, 2:50pm UTC](https://discourse.julialang.org/t/select-color-range-for-heatmap/81589/5 "2022-05-26T14:50:24Z")

</div>

Explanation:  
Given the spread of 2000 units over the color bar limits `(-2000,0)`, the `cgrad()` input vector `[0.50, 0.9995]` that lives in the space [0…1], puts an anchor point at `-1000 (= -2000 + 0.5*2000)`, and another at `-1 (= -2000 + 0.9995*2000)`

---

<div class="post-metadata">

**Author:** ![vamp](https://avatars.discourse-cdn.com/v4/letter/v/f4b2a3/32.png) [@vamp](https://discourse.julialang.org/u/vamp)\
**Post date:** [May 26, 2022, 2:55pm UTC](https://discourse.julialang.org/t/select-color-range-for-heatmap/81589/6 "2022-05-26T14:55:14Z")

</div>

Perfect, thank you so much for your help!
