Mathematica Asked on April 15, 2021
I need to create a group of MatrixPlots with a common color bar. The matrices have the value at different scale. Here I have written the code where matrices are created with RandomReal
function.
mat1=RandomReal[{-100,100}, {3,3}]; mat2=RandomReal[{-50,50}, {3,3}]; mat3=RandomReal[{-5,5}, {3,3}]; mat4=RandomReal[{-0.1, 0.1}, {3, 3}];
plot1 = MatrixPlot[mat1, ColorFunction -> "Rainbow", Frame -> True,FrameTicks -> {{{1, "n=-2"}, {2,"n=-1"}, {3, "n=0"}}, {{1,"m=-2"}, {2, "-1"}, {3, "0"}}},FrameTicksStyle -> Directive[Bold, 20]];
plot2 = MatrixPlot[mat2, ColorFunction -> "Rainbow", Frame -> True,FrameTicks -> {{{1, "n=-2"}, {2,"n=-1"}, {3, "n=0"}}, {{1,"m=-2"}, {2, "-1"}, {3, "0"}}},FrameTicksStyle -> Directive[Bold, 20]];
plot3 = MatrixPlot[mat3, ColorFunction -> "Rainbow", Frame -> True,FrameTicks -> {{{1, "n=-2"}, {2,"n=-1"}, {3, "n=0"}}, {{1,"m=-2"}, {2, "-1"}, {3, "0"}}},FrameTicksStyle -> Directive[Bold, 20]];
plot4 = MatrixPlot[mat4, ColorFunction -> "Rainbow", Frame -> True,FrameTicks -> {{{1, "n=-2"}, {2,"n=-1"}, {3, "n=0"}}, {{1,"m=-2"}, {2, "-1"}, {3, "0"}}},FrameTicksStyle -> Directive[Bold, 20]];
minmax = MinMax@Flatten[{mat1, mat2, mat3, mat4}, 2];
Legended[GraphicsGrid[Partition[{plot1, plot2, plot3, plot4}, 2]], BarLegend[{"Rainbow", minmax}, LegendLayout -> "Column"]]
I have followed solution of the question about common colorbar. But here it’s not possible to use the common colorbar to know about large or small values from t all the different matrixplots. All the matrix plots stand on its own. I can’t combine the matrices as, I need to mention frameticks to all the plots. Can someone please help to tackle this problem?
SeedRandom[1]
{mat1, mat2, mat3, mat4} = RandomReal[{-#, #}, {3, 3}] & /@ {100, 50, 5, .1};
minmax = MinMax@{mat1, mat2, mat3, mat4};
1. Use ColorFunction -> ColorData[{"Rainbow", minmax}]
and add the option ColorFunctionScaling -> False
:
plotsa = MatrixPlot[#,
ColorFunction -> ColorData[{"Rainbow", minmax}],
ColorFunctionScaling -> False,
Frame -> True,
FrameTicks -> {{{1, "n=-2"}, {2, "n=-1"}, {3, "n=0"}},
{{1, "m=-2"}, {2, "-1"}, {3, "0"}}},
FrameTicksStyle -> Directive[Bold, 20]] & /@
{mat1, mat2, mat3, mat4};
Legended[GraphicsGrid[Partition[plotsa, 2]], BarLegend[{"Rainbow", minmax}]]
2. Rescale the input matrices and add the option ColorFunctionScaling -> False
:
plotsb = MatrixPlot[#,
ColorFunction -> "Rainbow",
ColorFunctionScaling -> False,
Frame -> True,
FrameTicks -> {{{1, "n=-2"}, {2, "n=-1"}, {3, "n=0"}},
{{1,"m=-2"}, {2, "-1"}, {3, "0"}}},
FrameTicksStyle -> Directive[Bold, 20]] & /@
Rescale[{mat1, mat2, mat3, mat4}];
Legended[GraphicsGrid[Partition[plotsb, 2]], BarLegend[{"Rainbow", minmax}]]
3. You can also use matrices of colors (obtained by by mapping ColorData['Rainbow"]
on re-scaled input matrices) as the first argument in MatrixPlot
:
plotsc = MatrixPlot[#, Frame -> True,
FrameTicks -> {{{1, "n=-2"}, {2, "n=-1"}, {3, "n=0"}},
{{1, "m=-2"}, {2, "-1"}, {3, "0"}}},
FrameTicksStyle -> Directive[Bold, 20]] & /@
Map[ColorData["Rainbow"], Rescale[{mat1, mat2, mat3, mat4}], {-1}];
Legended[GraphicsGrid[Partition[plotsc, 2]], BarLegend[{"Rainbow", minmax}]]
Correct answer by kglr on April 15, 2021
The problem is that Mathematica automatically scales all the values of the matrices to lie in the range {0, 1}
before passing it to ColorFunction
.
The solution is to define your own ColorFunction
that does the scaling for you. This also means that you need to set ColorFunctionScaling -> False
. For instance
mat1 = RandomReal[{-100, 100}, {3, 3}]; mat2 =
RandomReal[{-50, 50}, {3, 3}]; mat3 =
RandomReal[{-5, 5}, {3, 3}]; mat4 = RandomReal[{-0.1, 0.1}, {3, 3}];
minmax = MinMax@Flatten[{mat1, mat2, mat3, mat4}, 2];
cf = ColorData["Rainbow"]@Rescale[#, minmax, {0, 1}] &;
plot1 = MatrixPlot[mat1, ColorFunction -> cf, Frame -> True,
FrameTicks -> {{{1, "n=-2"}, {2, "n=-1"}, {3, "n=0"}}, {{1,
"m=-2"}, {2, "-1"}, {3, "0"}}},
FrameTicksStyle -> Directive[Bold, 20],
ColorFunctionScaling -> False];
plot2 = MatrixPlot[mat2, ColorFunction -> cf, Frame -> True,
FrameTicks -> {{{1, "n=-2"}, {2, "n=-1"}, {3, "n=0"}}, {{1,
"m=-2"}, {2, "-1"}, {3, "0"}}},
FrameTicksStyle -> Directive[Bold, 20],
ColorFunctionScaling -> False];
plot3 = MatrixPlot[mat3, ColorFunction -> cf, Frame -> True,
FrameTicks -> {{{1, "n=-2"}, {2, "n=-1"}, {3, "n=0"}}, {{1,
"m=-2"}, {2, "-1"}, {3, "0"}}},
FrameTicksStyle -> Directive[Bold, 20],
ColorFunctionScaling -> False];
plot4 = MatrixPlot[mat4, ColorFunction -> cf, Frame -> True,
FrameTicks -> {{{1, "n=-2"}, {2, "n=-1"}, {3, "n=0"}}, {{1,
"m=-2"}, {2, "-1"}, {3, "0"}}},
FrameTicksStyle -> Directive[Bold, 20],
ColorFunctionScaling -> False];
Legended[GraphicsGrid[Partition[{plot1, plot2, plot3, plot4}, 2]],
BarLegend[{"Rainbow", minmax}, LegendLayout -> "Column"]]
Answered by Natas on April 15, 2021
Get help from others!
Recent Questions
Recent Answers
© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP