# Center Diverging color map on a matrix plot

I want to use the color function "RedBlueTones" and I want to center the map at a number. Then I want to blend in the red direction of I am greater than that number and blend in the blue direction if I am less than that number.

Here is an example. How do I center this at 8?

MatrixPlot[stackTest, ColorFunction -> "RedBlueTones"]
stackTest = {{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}}

-

This is the verbose way. Feel free to make a function of it:

stackTest = {{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}}
MatrixPlot[stackTest,
ColorFunction -> (ColorData["RedBlueTones"][
1 - If[# < 8, Rescale[#, {1, 8}, {0, 1/2}], Rescale[#, {8, 10}, {1/2, 1}]]]&),
ColorFunctionScaling -> False]


-
Can this be done for an arbitrary list? Where we can specify a target (the center of the color map) and apply it? – olliepower Apr 28 '14 at 16:45
Instead of ColorData["RedBlueTones"][1 - If[.... you can use ColorData[{"RedBlueTones", "Reversed"}][If[... – Aisamu Apr 28 '14 at 18:03
@olliepower just read the 8s in harilutan's ColorFunction as "center" and, in the ranges {1,8}, {8,10}, the 1 as the data minimum and 10 as maximum – Aisamu Apr 28 '14 at 18:06

Using @halirutan's piecewise re-scaling directly on the input matrix:

ClearAll[rsF];

dt = RandomInteger[10, {8, 8}];
rsF[lst_, n_] := With[{min = Min @@ lst, max = Max @@ lst},
Map[If[# <= n, Rescale[#, {min, n}, {0, 1/2}],Rescale[#, {n, max}, {1/2, 1}]] &,
lst, {-1}]]
mp1 = MatrixPlot[rsF[dt, 8], ColorFunctionScaling -> False,
ColorFunction -> (ColorData[{"RedBlueTones", "Reversed"}][#] &),
Epilog -> {Red, MapIndexed[Text[Style[#1, Bold, 20], #2 - {.5, .5}] &,
Transpose@Reverse[dt], {2}]}, ImageSize -> 300];
mp2 = MatrixPlot[rsF[dt, 3], ColorFunctionScaling -> False,
ColorFunction -> (ColorData[{"RedBlueTones", "Reversed"}][#] &),
Epilog -> {Red, MapIndexed[Text[Style[#1, Bold, 20], #2 - {.5, .5}] &,
Transpose@Reverse[dt], {2}]}, ImageSize -> 300];
mp3 = MatrixPlot[List /@ Range[-1, 1, .2],
Frame -> False, FrameTicks -> None, ImageSize -> {50, 280},
ColorFunctionScaling -> True, ColorFunction -> (ColorData["RedBlueTones"][#] &),
Epilog -> {Red, MapIndexed[ Text[Style[#1, Bold, 20], #2 - {.5, .5}] &,
{Range[0, 10]}, {2}]}];
Row[{Labeled[mp1, "n=8", Top], Labeled[mp2, "n=3", Top], mp3}, Spacer[5]] // Panel


-

You did not specify exactly how you want to blend which permits an extremely simple interpretation of the question:

MatrixPlot[8 - stackTest, ColorFunction -> "RedBlueTones"]
`

Perhaps you could include an example in your question if you are aiming for a specific scaling.

-