Несколько Matrixplots с общей цветовой полосой, но не могут различать значения

Aug 21 2020

Мне нужно создать группу MatrixPlots с общей цветовой полосой. Матрицы имеют значения в разном масштабе. Здесь я написал код, в котором матрицы создаются с помощью RandomRealфункции.

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"]]

Я следил за решением вопроса об общей палитре . Но здесь невозможно использовать общую цветовую шкалу, чтобы узнать о больших или малых значениях из всех различных матричных графиков. Все матричные графики стоят особняком. Я не могу комбинировать матрицы, так как мне нужно упомянуть фреймы для всех графиков. Может ли кто-нибудь помочь решить эту проблему?

Ответы

6 kglr Aug 21 2020 at 07:53
SeedRandom[1]
{mat1, mat2, mat3, mat4} = RandomReal[{-#, #}, {3, 3}] & /@ {100, 50, 5, .1};

minmax = MinMax@{mat1, mat2, mat3, mat4};

1. Используйте ColorFunction -> ColorData[{"Rainbow", minmax}]и добавьте опцию 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. Измените масштаб входных матриц и добавьте параметр 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. Вы также можете использовать матрицы цветов (полученные путем преобразования ColorData['Rainbow"]масштабированных входных матриц) в качестве первого аргумента в 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}]]

3 Natas Aug 21 2020 at 07:42

Проблема в том, что Mathematica автоматически масштабирует все значения матриц, чтобы они лежали в диапазоне, {0, 1}прежде чем передать его ColorFunction.

Решение состоит в том, чтобы определить свой собственный, ColorFunctionкоторый выполняет масштабирование за вас. Это также означает, что вам нужно установить ColorFunctionScaling -> False. Например

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"]]