7
$\begingroup$

I did a MatrixPlot of a set of values that range between 0 and 1. I got the following first plot with no problems:

enter image description here

As we can see from the legend, the values range between 0 and 1. Now, I am doing another MatrixPlot with a set of smaller values that range between 0.01 and 0.22. I got this plot:

enter image description here

I want the second plot to use the same scale used by the first plot (between 0 and 1) and hence the same color scale so we can compare the two plots with each other. How can I tell Mathematica to use the same scale of the first plot for second plot?

$\endgroup$
  • $\begingroup$ Related: 104989. $\endgroup$ – Edmund Jul 3 at 13:34
5
$\begingroup$

For this, we need the default color function used by MatrixPlot, which we can get here:

cf = Blend[{{0., RGBColor[0.260487, 0.356, 0.891569]}, {0.166667, 
      RGBColor[0.230198, 0.499962, 0.848188]}, {0.333333, 
      RGBColor[0.392401, 0.658762, 0.797589]}, {0.499999, 
      RGBColor[0.964837, 0.982332, 0.98988]}, {0.5, 
      RGBColor[1, 1, 1]}, {0.500001, 
      RGBColor[0.95735, 0.957281, 0.896269]}, {0.666667, 
      RGBColor[0.913252, 0.790646, 0.462837]}, {0.833333, 
      RGBColor[0.860243, 0.558831, 0.00695811]}, {1., 
      RGBColor[1., 0.42, 0.]}}, #1] &;

Next, we need to rescale it in the same way that MatrixPlot rescales it. Namely, so that 0.5 is at 0.

cfScaled = cf@Rescale[#, {0, 1}, {0.5, 1}] &;

Now we can get the plot like this:

MatrixPlot[
 RandomReal[0.2, {10, 10}],
 PlotLegends -> BarLegend[{Automatic, {0, 1}}],
 ColorFunction -> cfScaled,
 ColorFunctionScaling -> False
 ]

Output

Comments:

  • ColorFunctionScaling is turned off because otherwise your values, which are approximately between 0 and 0.2, would be rescaled to lie between 0 and 1 before being passed to the color function.
  • cfScaled has to be an anonymous function, otherwise it won't work because BarLegend does not work with named functions. Optionally, one can also use a color scheme from ColorData such as ColorFunction -> ColorData["AvocadoColors"].
| improve this answer | |
$\endgroup$
6
$\begingroup$

Note: as others have mentioned here already, the option I show here is the simplest correction that works only when your values lie in the {0,1} interval.

When plotting a MatrixPlot, you can set the option ColorFunctionScaling to False, because by default the colors will be rescaled between 0 and 1.

Suppose you have two matrices, one with values ranging in the {0,1} interval, and the other in the {0,0.22} interval. Choosing some particular color scheme, in this example the "TemperatureMap", you can plot both of these matrices without rescaling like this:

nPts = 100;
myMat1 = RandomReal[{0, 1}, {nPts, nPts}];
myMat2 = RandomReal[{0, 0.22}, {nPts, nPts}];

{MatrixPlot[myMat1, ColorFunction -> ColorData["TemperatureMap"], 
  PlotLegends -> Automatic, ColorFunctionScaling -> False],
 MatrixPlot[myMat2, ColorFunction -> ColorData["TemperatureMap"], 
  PlotLegends -> Automatic, ColorFunctionScaling -> False]}

enter image description here

If this option is not set, the colors will be rescaled:

{MatrixPlot[myMat1, ColorFunction -> ColorData["TemperatureMap"], 
  PlotLegends -> Automatic], 
 MatrixPlot[myMat2, ColorFunction -> ColorData["TemperatureMap"], 
  PlotLegends -> Automatic]}

enter image description here

| improve this answer | |
$\endgroup$
  • $\begingroup$ But how can I use the same colors of the 'First Plot' instead of TemperatureMap colors? $\endgroup$ – Naps Jul 3 at 11:52
  • $\begingroup$ You can either replace "TemperatureMap" for whatever color scheme you like (that is similar to the 'FirstPlot'), or if you want the exact same color then you need to follow the steps given by @C.E. below, because you'd have to find the particular RGB values that are used in the default MatrixPlot function. $\endgroup$ – TumbiSapichu Jul 3 at 12:07
6
$\begingroup$

You may use the ColorFunction, ColorFunctionScaling, and PlotLegends options of MatrixPlot.

With

SeedRandom[123]
dat1 = RandomReal[{0, 1}, {10, 10}];
dat2 = RandomReal[{0.01, 0.22}, {10, 10}];

Then

MatrixPlot[#,
    ColorFunction -> ColorData[{"BrownCyanTones", {0, 1}}],
    ColorFunctionScaling -> False,
    PlotLegends -> BarLegend[{Automatic, {0, 1}}]
    ] & /@ {dat1, dat2} // GraphicsRow

Mathematica graphics

  • The ColourFunction specification forces the color gradient to span the provided range.
  • The ColorFunctionScaling specification prevents the scaling of the values to run between 0 and 1 for the color function. This is needed for when your data does not span 0 to 1; see additional example below.
  • The PlotLedgends specification forces the legend to span the specified range; else it will only span the range of the data but will have the correct colours; see additional example below.

So lets take a range of 50 to 100 and not scale the range of the legend do demonstrate the points made above

With

SeedRandom[456]
dat3 = RandomReal[{50, 100}, {10, 10}];
dat4 = RandomReal[{55, 65}, {10, 10}];

Then

MatrixPlot[#,
    ColorFunction -> ColorData[{"BrownCyanTones", {50, 100}}],
    ColorFunctionScaling -> False,
    PlotLegends -> Automatic
    ] & /@ {dat3, dat4} // GraphicsRow

Mathematica graphics

Notice in the plot of dat4 that the correct colours are used but the legend does not span 50 to 100.

Hope this helps.

| improve this answer | |
$\endgroup$
3
$\begingroup$

This is unfortunately a rather hard problem. For values that lie between 0 and 1 the following works:

fixedRangeMatrixPlot[data_] := With[{cf = "TemperatureMap", ticks = 8},
  Legended[
   MatrixPlot[data, ColorFunction -> (ColorData[cf][# + 1/2] &), 
    ColorFunctionScaling -> False], Placed[
    BarLegend[
     {Function[Blend[cf, #]], {1/2, 1}},
     Rule[Ticks, 
      Table[{.5 (1 + i/(ticks - 1)), N[#, 1] &@(i/(ticks - 1))}, {i, 
        0, ticks - 1}]]
     ], After, Identity]]
  ]

Then you can try

fixedRangeMatrixPlot@RandomReal[{0, 1}, {10, 10}]

MatrixPlot with "full" range

fixedRangeMatrixPlot@RandomReal[{0, .2}, {10, 10}]

MatrixPlot with "partial" range

I found this "solution" by looking at the output of FullForm.

| improve this answer | |
$\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.