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.

I am trying to show a gradient field behind a plot, and then its density in a separate plot below.

I understand (as in this question) that this must normally be done using the ImagePadding directive. However, that does not quite align the plots in this case:

padding = {{20, 30}, {20, 30}}; 
GraphicsColumn[
  {
   Show[
    DensityPlot[PDF[NormalDistribution[0, 1], Log[x/40]] , 
      {x, 15, 60}, {y, 0, 30}, 
      ColorFunction -> "SunsetColors"], 
    Graphics[{Dashed, Gray, Line[{{40, 0}, {40, 30}}]}], 
    Plot[If[x < 20, 10, If[x < 25, 2 x - 30, 20]], {x, 15, 60}, 
      PlotRange -> {0, 30} , 
      PlotStyle -> {RGBColor[0.5, .7, 0.2], Thickness[0.01]}], 
    ImagePadding -> padding, PlotRangePadding -> None
   ], 
   Plot[PDF[NormalDistribution[0, 1], 5 Log[x/40]] , {x, 15, 60}, 
      PlotStyle -> {RGBColor[1, 0.4, 0], Thickness[0.01]},
      ImagePadding -> padding]
  }
]

Align me

Is there a way to get the abscissa ranges to match?

share|improve this question
1  
You also need to set PlotRange explicitly for both plots –  Verbeia Apr 15 '12 at 21:20
2  
You might also need to explicitly set the AxesOrigin option in the bottom plot. –  Guillochon Apr 15 '12 at 21:24
2  
To those tempted to close: I believe the issue is slightly different from earlier alignment questions. Please don't close. –  Sjoerd C. de Vries Apr 15 '12 at 22:14

2 Answers 2

up vote 13 down vote accepted

AbsoluteOptions shows there to be several differences between the output settings of the first and second plots:

Grid[Prepend[Transpose[
     AbsoluteOptions[#, {PlotRangePadding, AspectRatio, 
           Frame}] & /@ {plot1, plot2}], {"first", "second"}], Frame -> All]

settings

It'll look better if you use the same aspect ratio (I used 1/GoldenRatio) and a frame in each picture. I also set PlotRangePadding-> None in the second case.

plots

share|improve this answer
    
Your method of examining AbsoluteOptions is very generalizable. I think it's great. –  Brian B Apr 16 '12 at 2:05
    
Thanks. It's probably still a good idea to first scan the output of AbsoluteOptions[plot1] and AbsoluteOptions[plot2] to identify the options you want to compare. –  David Carraher Apr 16 '12 at 12:21

Use for example Framed[]:

padding = {{20, 20}, {20, 20}};
GraphicsColumn[{
  Framed[Show[
    DensityPlot[
     PDF[NormalDistribution[0, 1], Log[x/40]], {x, 15, 60}, {y, 0, 
      30}, ColorFunction -> "SunsetColors"],
    Graphics[{Dashed, Gray, Line[{{40, 0}, {40, 30}}]}], 
    Plot[If[x < 20, 10, If[x < 25, 2 x - 30, 20]], {x, 15, 60}, 
     PlotRange -> {0, 30}, 
     PlotStyle -> {RGBColor[0.5, .7, 0.2], Thickness[0.01]}], 
    ImagePadding -> padding, PlotRangePadding -> None], 
   FrameStyle -> Directive[White]],
  Framed[Plot[
    PDF[NormalDistribution[0, 1], 5 Log[x/40]], {x, 15, 60}, 
    PlotStyle -> {RGBColor[1, 0.4, 0], Thickness[0.01]}, 
    ImagePadding -> padding, Frame -> True], 
   FrameStyle -> Directive[White]]}]

enter image description here

share|improve this answer
    
Nice, sneaky trick! –  Brian B Apr 16 '12 at 2:04

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.