0
$\begingroup$

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.

$\endgroup$

closed as off-topic by Jens, bbgodfrey, Bob Hanlon, Karsten 7., Dr. belisarius Mar 17 '15 at 4:35

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "This question cannot be answered without additional information. Questions on problems in code must describe the specific problem and include valid code to reproduce it. Any data used for programming examples should be embedded in the question or code to generate the (fake) data must be included." – Jens, bbgodfrey, Bob Hanlon, Karsten 7., Dr. belisarius
If this question can be reworded to fit the rules in the help center, please edit the question.

2
$\begingroup$

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]

enter image description here

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

$\endgroup$
  • $\begingroup$ Edit and upvote, if you dont mind $\endgroup$ – user9660 Jun 6 '16 at 14:54
1
$\begingroup$

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}}
        ]
    ]
]

enter image description here

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.

$\endgroup$

Not the answer you're looking for? Browse other questions tagged or ask your own question.