# Select color range for heatmap

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:

``````using DelimitedFiles, Plots, LinearAlgebra, Pyplot
writedlm( "MyFile.txt",  values, ' ')
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!

One simple way:

``````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))
``````

2 Likes

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!

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

``````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))
``````

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)`

1 Like

Perfect, thank you so much for your help!

1 Like