# Color function doesn't span the whole color range in MatrixPlot

In matrix visualization, for instance,

MatrixPlot[{{1, 2}, {3, 4}}, PlotRangePadding -> 0,
ColorFunction -> "Rainbow"] the color map does not cover the whole color range as shown in the following density plot.

DensityPlot[x, {x, 1, 4}, {y, 0, 0.1}, AspectRatio -> 0.2,
PlotRangePadding -> 0, ColorFunction -> "Rainbow", Frame -> True,
FrameTicks -> None] I need to add the density plot as a color bar, while not using the Legend. So, how to solve this problem?

The cause of your problem is mentioned in the details section of the MatrixPlot doc page:

With the default setting ColorFunctionScaling->True, scaling is done based on a mixture of relative value and ranking for each matrix element. The final scaled value always lies between 0 and 1, with scaled value 0.5 corresponding to matrix element value 0.

So, you don't get a linear scaling based on the matrix elements values. To get that you have to rescale yourself:

MatrixPlot[{{1, 2}, {3, 4}},
ColorFunction -> (ColorData["Rainbow"][Rescale[#, {1, 4}, {0, 1}]] &),
ColorFunctionScaling -> False
] Perhaps this question is more about understanding why it doesn't give the expected output, which Sjoerd answers, but pragmatically why not use ArrayPlot?:

ArrayPlot[
{{1, 2}, {3, 4}},
ColorFunction -> "Rainbow",
FrameTicks -> All
] • Great! I had a related (but more complicated) problem with MatrixPlot and ArrayPlot solved everything at once, without having to do rescaling of the data and the bar legend. Even the bar legend default values are decimals and not fractions as in MatrixPlot. – Santi Aug 2 '14 at 14:33
• @Santi I'm you found this helpful. :-) – Mr.Wizard Aug 2 '14 at 18:41

Would something like this satisfy you?

MatrixPlot[{{1, 2}, {3, 4}},
ColorFunction -> Function[x, ColorData["Rainbow"][(x - 0.3)*1.1]],
PlotRange -> All] 