Group Abstract Group Abstract

Message Boards Message Boards

0
|
334 Views
|
0 Replies
|
0 Total Likes
View groups...
Share
Share this post:

Uniformize paddings for a list of plots

Posted 3 months ago
(* Define the module to uniformize paddings for a list of plots *)
    uniformizePaddings[plots_List] := Module[
      {paddings, dim, maxPadding, uniformPlots, uniformPlotsReshape
      , imSize=300 (*image size of subfigure*)
      , labels (*subfigure label, by default they're labled in alphabetical order*)
      , labelPos = {0.1, 0.9}(*specify the positions of subfigure label*)},

  (* Find the dimension of plots list *)
  dim = Dimensions[plots];

  (* Create label array*)
  labels = ArrayReshape[Alphabet[](*RomanNumeral[Range[1,100]]*),dim];

  (* Compute paddings for each plot *)
  paddings = BorderDimensions[Image[Show[#, LabelStyle -> White, Background -> White]]] & /@ Flatten[plots, 1];

  (* Find the maximum padding in each dimension *)
  maxPadding = Map[Max, Transpose[paddings]];

  (* Adjust each plot to have uniform padding *)
  uniformPlots = Show[#, ImageSize->imSize,ImagePadding -> maxPadding] & /@ Flatten[plots, 1];

  (* Reshape the uniform plots to match the input dimensions *)
  uniformPlotsReshape = ArrayReshape[uniformPlots, dim];

  (* Display the modified plots *)
  MapIndexed[
    Show[
      #1,

      (*Label each subfigure in the alphabetical order*)
      Epilog -> {Text[Style["(" <> ToString[Extract[labels,#2]] <> ")", 20, Bold, FontFamily -> "Times New Roman"], Scaled[labelPos]]}(*You could comment this line if you don't want this function*)

      ,ImagePadding->{{Automatic, 10}, {Automatic, 10}} 
    ] &,
    uniformPlotsReshape,
    {2}
  ] //GraphicsGrid


];

(* Example usage with a 3x2 array of plots *)
plots = {
  {Plot[Exp[x], {x, -2, 2}, PlotLabel -> "exp(x)", Frame -> True, ImageSize->400,FrameTicks -> Automatic],
   Plot[Sin[x], {x, -2*Pi, 2*Pi}, PlotLabel -> "sin(x)", Frame -> True, ImageSize->200, FrameTicks -> Automatic]},
  {Plot[Cos[x], {x, -2*Pi, 2*Pi}, PlotLabel -> "cos(x)", Frame -> True, ImageSize->200, FrameTicks -> Automatic],
   Plot[Sin[x]/x, {x, -10, 10}, PlotLabel -> "sinc(x)", Frame->True,ImageSize->300, Frame -> True, FrameTicks -> Automatic]},
  {Plot[Log[x], {x, 0.1, 5}, PlotLabel -> "ln(x)", Frame -> True, FrameTicks -> Automatic],
   Plot[1/x, {x, 0.1, 5}, PlotLabel -> "1/x", Frame -> True, FrameTicks -> Automatic]}
};

(* Uniformize paddings in the m x n array of plots *)
uniformPlots = uniformizePaddings[plots];

(* Display the combined plots *)
GraphicsGrid[plots](*before*)
uniformPlots(*after*)
POSTED BY: Wenxuan Xie
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard