3
$\begingroup$

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}}
$\endgroup$

3 Answers 3

4
$\begingroup$

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]

Mathematica graphics

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

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

enter image description here

$\endgroup$
0
$\begingroup$

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

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

enter image description here

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

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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