7
$\begingroup$

I want to draw a picture with GraphicsRow function in Mathematica. A simple example shows as follows

data1 = RandomInteger[5, {16, 16}];
data2 = RandomInteger[1000000000, {16, 16}];
fig1 = MatrixPlot[data1, PlotLegends -> Placed[Automatic, {0, 0.5}], 
   FrameTicks -> {{Automatic, None}, {Automatic, None}}, 
   ImageSize -> 400];
fig2 = MatrixPlot[data2, PlotLegends -> Placed[Automatic, {1, 0.5}], 
   FrameTicks -> {{None, Automatic}, {Automatic, None}}, 
   ImageSize -> 400];
GraphicsRow[{fig1, fig2}, Spacings -> -2]

The execution result is enter image description here where the two graphs are not of the same size. I then add AspectRatio -> Full action inside MatrixPlot. The result becomes enter image description here But now the two graphs have different widths. I think the size of the two graphs does not agree because their legends are different. Since when I remove the PlotLegend option, the result becomes enter image description here

So how can I let the two pictures agree in size while keeping the legends?

My $Version result is 13.1.0 for Microsoft Windows (64-bit) (June 16, 2022).

$\endgroup$
3
  • 1
    $\begingroup$ These functionalities have been improved on the latest versions of Mathematica. What is your version ? $\endgroup$
    – andre314
    Apr 1 at 8:37
  • $\begingroup$ @andre314 My $Version result is 13.1.0 for Microsoft Windows (64-bit) (June 16, 2022). Do you mean if I install version 13.2 the problem will be fixed? $\endgroup$
    – narip
    Apr 1 at 8:53
  • $\begingroup$ I have tested on Mma 13.2. It turns out that there are no improvements for this specific problem (more precisely PlotMatrix does not have the option "PlotLayout" ) $\endgroup$
    – andre314
    Apr 1 at 11:16

3 Answers 3

7
$\begingroup$

It seems that the following do the job :

(* this is exactly your code : *)
data1 = RandomInteger[5, {16, 16}];
data2 = RandomInteger[1000000000, {16, 16}];
fig1 = MatrixPlot[data1, PlotLegends -> Placed[Automatic, {0, 0.5}], 
   FrameTicks -> {{Automatic, None}, {Automatic, None}}, 
   ImageSize -> 400];
fig2 = MatrixPlot[data2, PlotLegends -> Placed[Automatic, {1, 0.5}], 
   FrameTicks -> {{None, Automatic}, {Automatic, None}}, 
   ImageSize -> 400];   

(* then the new thing : *)

Legended[GraphicsRow[{fig1[[1]], fig2[[1]]}], {Placed[fig1[[2, 1]], 
   Before], Placed[fig2[[2, 1]], After]}]

enter image description here

$\endgroup$
6
$\begingroup$

If you want more control over how exactly your plots are laid out, you can use ResourceFunction["PlotGrid"]:

data1 = RandomInteger[5, {16, 16}];
data2 = RandomInteger[1000000000, {16, 16}];
fig1 = MatrixPlot[data1, PlotLegends -> Placed[Automatic, Left]];
fig2 = MatrixPlot[data2, PlotLegends -> Placed[Automatic, Right]];

ResourceFunction["PlotGrid"][{{fig1, fig2}}]

enter image description here

Note how there is no need to remove the frame/image size manually, and how the default setting for Spacings is effectively gluing the plots together, without any "magic" numbers. Please note that I had to change the legend positioning to Left/Right to achieve the optimal results, the original settings were not behaving as nicely. (settings such as {-0.05, 0.5}/{1.05, 0.5} also work, but you'll have to play around with the numbers to make it look like you want)

$\endgroup$
0
$\begingroup$

The trick seems to be to transfer the size as a variable to the internal code of execution

    data1 = RandomInteger[5, {16, 16}];
   data2 = RandomInteger[1000000000, {16, 16}];

fig1 := MatrixPlot[data1, PlotLegends -> Placed[Automatic, Scaled[{0, 0.5}]], 
   FrameTicks -> {{Automatic, None}, {Automatic, None}}, 
   ImageSize -> Medium ] ;

fig2[s_] := 
  MatrixPlot[data2, PlotLegends -> Placed[Automatic, Scaled[{1, 0.5}]], 
   FrameTicks -> {{None, Automatic}, {Automatic, None}} ,ImageSize -> {s, s}];

 GraphicsRow[{fig1, fig2[420]}] 

enter image description here

$\endgroup$
2
  • 1
    $\begingroup$ Thanks for the answer! But it seems the left graph has lower height than the right one.. $\endgroup$
    – narip
    Apr 1 at 9:12
  • 1
    $\begingroup$ Perhaps fine tunig of the first guess 420-size will help. Of course its possible to define both images as functions of its own sizes and try a Manipulate[ ] in order to exactly find the right parameters. $\endgroup$
    – Roland F
    Apr 1 at 12:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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