2

I would like to align the axes of the three plots: g3, gPosteriorMarginalMu and gPosteriorMarginalSigma:

vData = {100, 150, 50};
pPrior[μ_, σ_] := 1/(σ^2);
pLikelihood[μ_, σ_] := Likelihood[NormalDistribution[μ, σ], vData];
pPosteriorNumerator[μ_, σ_] := pPrior[μ, σ] * pLikelihood[μ, σ];
g3 = ContourPlot[pPosteriorNumerator[μ, σ], {μ, 0, 300}, {σ, 0, 100}, ContourShading -> {White}, ContourStyle -> {Black}, FrameLabel -> {μ, σ^2}, BaseStyle -> {FontSize -> 16}];
pPriorSigma[σ_] := pPrior[10, σ]
pPosteriorMarginalSigmaCorrect[σ_] := PDF[InverseChiSquareDistribution[Length[vData] - 1, Sqrt[Variance[vData]/Length[vData]]], {σ}]
gPosteriorMarginalSigma = Rotate[Plot[pPosteriorMarginalSigmaCorrect[σ], { σ, 0, 100}, FrameLabel -> {σ^2, pdf}, Frame -> {True, True, False, False}, BaseStyle -> {FontSize -> 16}, PlotStyle -> {Thickness[0.01], Gray}], π/2];
pPosteriorMarginalMuCorrect[μ_] := PDF[StudentTDistribution[Mean[vData], Sqrt[Variance[vData]/Length[vData]], Length[vData] - 1], {μ}]
gPosteriorMarginalMu = Plot[pPosteriorMarginalMuCorrect[μ], {μ, 0, 300}, FrameLabel -> {μ, pdf}, Frame -> {True, True, False, False}, BaseStyle -> {FontSize -> 16}, PlotStyle -> {Thickness[0.01], Gray}];
GraphicsGrid[{{g3, gPosteriorMarginalSigma}, {gPosteriorMarginalMu,}}]

At the moment, the plot axes of the top and bottom plots do not align. Similarly, the axes of the rotated axis does not align with the ContoutPlot:

enter image description here

I have played around with ImagePadding, and have used the code from here: Aligning plot axes in a graphics object to try to solve the problem.

Does anyone have a solution to this particular problem, and is there a general method which will always align plot axes?

Best,

Ben

ben18785
  • 3,167
  • 15
  • 28

1 Answers1

5

I would also play around with different values for ImagePadding. Your example is a bit more difficult in that you want to align a rotated plot.

I had success by using Grid (as well as GraphicsGrid, but Grid allows for less whitespace) as suggested by Kuba and playing around with ImagePadding while prescribing a fixed AspectRatio and ImageSize.

ar1 = 1; (*keep this fixed*)
ar2 = 1/2; (*free to choose*)
padding = {{70, 20}, {70, 20}}; (*play with these values*)
paddingRotated = {padding[[2]],{45, 20}};(*play with these values*)
imSize = 300; (*free to choose*)

g3 = ContourPlot[
   pPosteriorNumerator[μ, σ], {μ, 0, 300}, {σ, 
    0, 100}, ContourShading -> {White}, ContourStyle -> {Black}, 
   FrameLabel -> {μ, σ^2}, BaseStyle -> {FontSize -> 16}
   , ImagePadding -> padding, AspectRatio -> ar1, ImageSize -> imSize];

gPosteriorMarginalSigma = 
  Rotate[Plot[
    pPosteriorMarginalSigmaCorrect[σ], {σ, 0, 100}, 
    FrameLabel -> {σ^2, pdf}, 
    Frame -> {True, True, False, False}, 
    BaseStyle -> {FontSize -> 16}, PlotStyle -> {Thickness[0.01], Gray}
    , ImagePadding -> paddingRotated, AspectRatio -> ar2, 
    ImageSize -> imSize],π/2];

gPosteriorMarginalMu = 
  Plot[pPosteriorMarginalMuCorrect[μ], {μ, 0, 300}, 
   FrameLabel -> {μ, pdf}, Frame -> {True, True, False, False}, 
   BaseStyle -> {FontSize -> 16}, 
   PlotStyle -> {Thickness[0.01], Gray}
   , ImagePadding -> padding, AspectRatio -> ar2, ImageSize -> imSize];

Grid[{{g3, gPosteriorMarginalSigma}, {gPosteriorMarginalMu}}
 , Frame -> All, Spacings -> 0]

Output plot of above code

fabern
  • 173
  • 1
  • 6