How do I color an Image generated from data such that one range of values is one color gradient and the others another color gradient [closed]

I can generate a Gray scale Image from some data that I collect. However I am trying to figure out how to generate the image with two color gradients or color scales based on the value. For example negative values are Red gradient and positive values are a blue gradient. Another example since gray scale rescales from 0 to 1 have 0 to .5 be a red gradient and .5 to 1 be a blue gradient.

If its possible creating a scale bar or a bar legend would be nice.

• – kglr Mar 16 '15 at 20:56

Alternately, if you're just planning on using the barebones function Image, you could do something like this:

col = Compile[{{z, _Real}}, {Sign[z]/4 + 1, Exp[1 - Max[Abs[z], 1]],
Min[Abs[z], 1]}, RuntimeAttributes -> {Listable}]


which produces a basic red-blue color scheme in Hue colorspace.

Example usage:

Image[col@Table[Sin[x] Sin[y], {x, 0, 10, 0.01}, {y, 0, 10, 0.01}],
ColorSpace -> Hue]


Note that this has no labeling, legending, or other options, so this is only useful if performance is a concern (like on large datasets).

• Edit and upvote, if you dont mind – user9660 Jun 6 '16 at 14:54

Use ColorFunction.

Plot[Sin[500 x], {x, 0, 1}, ColorFunction ->
Function[{x, y},
Piecewise[
{{RGBColor[0, 0, 1 - 2 y], 0 <= y < .5},
{RGBColor[2 (y - .5), 0, 0], .5 <= y <= 1}}
]
]
]


A bit ugly, but you get the idea. Here the line of the plot is colored based on the y coordinate. The documentation for ColorFunction is quite good and has a lot of examples.