9
$\begingroup$

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?

$\endgroup$
6
$\begingroup$

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

$\endgroup$
8
$\begingroup$

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

$\endgroup$
  • $\begingroup$ 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. $\endgroup$ – Santi Aug 2 '14 at 14:33
  • $\begingroup$ @Santi I'm you found this helpful. :-) $\endgroup$ – Mr.Wizard Aug 2 '14 at 18:41
2
$\begingroup$

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

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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