Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

How do I vertically align plots so that the two plots are displayed in a way that lines up with respect to the axes?

The code I have so far is:

GraphicsColumn[
 {
  Plot[0.125 x^2 + 0.05 x^3 + 0.005 x^4, {x, -8.0, 3.0}, 
    AspectRatio -> .4, PlotRange -> {-.19, .7}, Frame -> True,
    FrameStyle -> Thickness[.003],
    FrameLabel -> {Null, "V(x)", "m=1.0,   w=0.5,   y=2.0,   g=1.0"}, 
    FrameTicks -> {{Automatic, Automatic}, {None, All}}], 
  Plot[0.5 + 0.2 x, {x, -8.0, 3.0}, AspectRatio -> .4, 
    PlotRange -> {-1, .9}, Frame -> True,  FrameStyle -> Thickness[.003], 
    FrameLabel -> {"x", "B(x)", Null}, 
    FrameTicks -> {{Automatic, Automatic}, {Automatic, Automatic}}]
 }, 
 ImageSize -> 500, Frame -> None, Spacings -> -20]

My horrid attempt to align plots

I'm looking at this right now, and realized how ugly this is... like why is the lower frame thicker than the upper one???

share|improve this question
    
Use AbsoluteThickness instead of just Thickness –  rm -rf Oct 20 '12 at 23:21
    
@rm-rf oh! thanks! –  QuantumDot Oct 20 '12 at 23:22
1  
IMHO, this is one of the few things that is easier to do in MATLAB (using the subplot function) than in Mathematica. But make no mistake, I prefer Mathematica 1000 times over MATLAB. –  Gustavo Delfino Dec 13 '12 at 14:49

1 Answer 1

up vote 14 down vote accepted

Firstly, you need to use AbsoluteThickness instead of Thickness, as the latter is relative and is a fraction of the horizontal plot range. Secondly, for precise alignment, you'll have to give both figures the exact same ImagePadding (just like you did with other options), as otherwise, each plot chooses its own padding depending on the extents of the ticks, labels, etc.

Here's how it will look if you fix these (working code given at the end):

A natural question that arises, is "How do you choose the right ImagePadding?" Surely, you can't be fiddling with the values all day... The solution is to use this idea to create a function that gets the max padding for both plots, like:

getMaxPadding[p_List] := Map[Max, (BorderDimensions@
    Image[Show[#, LabelStyle -> White, Background -> White]] & /@ p) ~Flatten~ {{3}, {2}}, {2}] + 1

I used this function in this answer to align two plots vertically. I'll leave incorporating this into your code as an exercise (not very hard, since my answer shows you how to use it).

The code for the figure above is:

GraphicsColumn[{
  Plot[0.125 x^2 + 0.05 x^3 + 0.005 x^4, {x, -8.0, 3.0},
   AspectRatio -> .4,
   PlotRange -> {{-8, 3}, {-.19, .7}},
   Frame -> True,
   FrameStyle -> AbsoluteThickness[.003], 
   FrameLabel -> {Null, "V(x)", "m=1.0,   w=0.5,   y=2.0,   g=1.0"}, 
   FrameTicks -> {{Automatic, Automatic}, {None, All}},
   PlotRangePadding -> None,
   ImagePadding -> {{40, 1}, {20, 30}}],

  Plot[0.5 + 0.2 x, {x, -8.0, 3.0},
   AspectRatio -> .4,
   PlotRange -> {{-8, 3}, {-1, .9}},
   Frame -> True, FrameStyle -> AbsoluteThickness[.003],
   FrameLabel -> {"x", "B(x)", Null}, 
   FrameTicks -> {{Automatic, Automatic}, {Automatic, Automatic}},
   PlotRangePadding -> None,
   ImagePadding -> {{40, 1}, {20, 30}}]
  }, ImageSize -> 500, Spacings -> -30]
share|improve this answer
    
Even with the update the x-axis label on the bottom image is missing. I have gotten better looking results with your code by setting ImagePadding -> {{40, 1}, {30, 30}} for plots and Spacings -> -40 for the graphics column. Also, isn't FrameTicks -> {{Automatic, Automatic},{Automatic, Automatic}} really just the same as FrameTicks -> Automatic. Actually, I'd use FrameTicks -> {{Automatic, Automatic}, {All, None}} for the lower plot. –  m_goldberg Oct 22 '12 at 7:54
1  
@m_goldberg Yes, I was sloppy in setting the image padding, but as I indicated earlier, it was just an example and the OP has to tweak it to get the right values for his needs. I also gave hints (see getMaxPadding in the middle) on how to automatically get the maximum padding of both plots and apply it to the figure. Lastly, yes, that is the same as FrameTicks -> Automatic, but this isn't my code... this is the OP's code, with minor modifications to solve the issue at hand. I didn't bother looking at other stuff. –  rm -rf Oct 22 '12 at 13:00

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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