# How can I create a legend for a Heat Map when I don't want to show the full range of colors?

I produce a heat map with legend with the code shown below

Needs["PlotLegends"]

mydata = Table[2 + Sin[x y/5.], {x, -10, 10}, {y, -10, 10}];

ShowLegend[
MatrixPlot[mydata,
Mesh -> True, ColorFunction -> "TemperatureMap",
FrameTicks -> None, MeshStyle -> Thin,
FrameStyle -> AbsoluteThickness[0],
],
{ColorData["TemperatureMap"][1 - #1] &, 4,
ToString[NumberForm[Max[mydata], 4]], "",
LegendPosition -> {1.1, -0.8}, LegendSize -> {.25, 0.5},
}
]


The data used to generate the meat map does not contain negative values, so the plotted heat map only shows a color range from white to red. But when I plot the legend, the color range shown is from blue to red. How can I adjust the legend's color range to the data. Alternatively, how can I manually adjust the labelling in the legend so as to label the red area with Max[mydata], add something like "<0" to the blue area and also attach a label to other colors? Many thanks for anyone's help!

-
hello @pjk and welcome to Mathematica.Stackexchange. Could you please complete your post with the definition of xtix ytix? – chris Oct 23 '12 at 7:26
hi @chris, sorry, I wanted to delete that, it refers to the data, one can set it FrameTicks->None – pjk Oct 23 '12 at 7:36
Try ColorData["TemperatureMap"][.5 + (1 - #1)/2] in place of ColorData["TemperatureMap1 - #1]. – kglr Oct 23 '12 at 8:18
@kguler: Many thanks that works. Can you also tell me what I would have to change if I want everything from white to blue (i.e. into the negatives)? Many thanks pjk – pjk Oct 23 '12 at 8:25
@kguler It is a short answer, but still an answer. I suggest you use this as an official answer. I hate questions with zero answers that actually are solved in comments. Bad for our score. – Sjoerd C. de Vries Oct 24 '12 at 8:04

An alternative to the solution suggested by kguler in comments would be to stretch the color range used in the plot by circumventing MatrixPlot's own ColorFunctionScaling:

ShowLegend[
MatrixPlot[mydata,
Mesh -> True,
ColorFunctionScaling -> False,
ColorFunction -> (ColorData["TemperatureMap"][Rescale[#, {Min[mydata], Max[mydata]}]] &),
FrameTicks -> None, MeshStyle -> Thin,
FrameStyle -> AbsoluteThickness[0],
],
{ColorData["TemperatureMap"][1 - #1] &, 4,
ToString[NumberForm[Max[mydata], 4]], ToString[NumberForm[Min[mydata], 4]],
LegendPosition -> {1.1, -0.8}, LegendSize -> {.25, 0.5},
}
]


-

To get the color range in the legend to match that in the plot, replace ColorData["TemperatureMap"][1 - #1]& in the original code with

ColorData["TemperatureMap"][.5 + (1 - #1)/2]&


To add additional labels to the legend can prove painful if you need more than the two extreme labels.

Using

Needs["PlotLegends"]

mydata = Table[2 + Sin[x y/5.], {x, -10, 10}, {y, -10, 10}];
mplt = MatrixPlot[mydata, Mesh -> True,
ColorFunction -> "TemperatureMap", FrameTicks -> None,
MeshStyle -> Thin,  FrameStyle -> AbsoluteThickness[0], PlotRangePadding -> 0.0,
ImageSize -> 400];


The settings needed to add a second label at the bottom is straightforward:

legendsettings1 = {ColorData["TemperatureMap"][.5 + (1 - #1)/2] &, 4,
" " <> ToString@NumberForm[Max[Flatten@mydata], {3, 2}],
" " <> ToString@NumberForm[Min[Flatten@mydata], {3, 2}],
LegendPosition -> {1.1, -0.8}, LegendSize -> {.25, 0.5},
LegendBorderSpace -> .2, LegendShadow -> None};


The above syntax does not allow more than two labels. To label all colors, after a long trial-and-error series, I could not get anything simpler than the following:

 legendsettings2 = {Graphics[{ColorData["TemperatureMap"][.5 + (1 - #1)/2],
EdgeForm[{Thickness[.2], ColorData["TemperatureMap"][.5 + (1 - #1)/2]}],
Rectangle[{0.2, -.2}, {.8, 1.}],
Black, Text[NumberForm[Quantile[Flatten@mydata, 1 - #1], {3, 2}], {2.2, 1/2}]},
PlotRange -> {{0, 3}, {-.2, 1.1}}] &, 4,
LegendPosition -> {1.1, -0.8}, LegendSize -> {.3, .5},
LegendBorderSpace -> .10, LegendShadow -> None};


With these two settings,

 Row[{ShowLegend[mplt, legendsettings1], ShowLegend[mplt, legendsettings2]}, Spacer[5]]


gives

-
Could perhaps use FindDivisions – Sjoerd C. de Vries Oct 24 '12 at 11:13