Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

In matrix visualization, for instance,

MatrixPlot[{{1, 2}, {3, 4}}, PlotRangePadding -> 0, 
 ColorFunction -> "Rainbow"]

matrix

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]

color bar

I need to add the density plot as a color bar, while not using the Legend. So, how to solve this problem?

share|improve this question

3 Answers 3

up vote 3 down vote accepted

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}}, 
  PlotRangePadding -> 0, 
  ColorFunction -> (ColorData["Rainbow"][Rescale[#, {1, 4}, {0, 1}]] &), 
  ColorFunctionScaling -> False
]

Mathematica graphics

share|improve this answer

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}},
 PlotRangePadding -> 0,
 ColorFunction -> "Rainbow", 
 FrameTicks -> All
]

Mathematica graphics

share|improve this answer
    
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 at 14:33
    
@Santi I'm you found this helpful. :-) –  Mr.Wizard Aug 2 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]

Mathematica graphics

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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