공통 컬러 바가있는 다중 행렬도, 값을 구분할 수 없음

Aug 21 2020

공통 색상 막대를 사용하여 MatrixPlot 그룹을 만들어야합니다. 행렬은 다른 스케일의 값을 갖습니다. 여기에 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"]]

나는 일반적인 colorbar 에 대한 질문의 해결책을 따랐다 . 그러나 여기서는 공통 컬러 바를 사용하여 모든 다른 행렬도에서 크거나 작은 값에 대해 알 수 없습니다. 모든 행렬 플롯은 그 자체로 서 있습니다. 모든 플롯에 프레임 틱을 언급해야하므로 행렬을 결합 할 수 없습니다. 누군가이 문제를 해결하는 데 도움을 줄 수 있습니까?

답변

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