5
$\begingroup$

I need to make several plots displaying a ListPlot overlaid with a Histogram and close to it (ideally even sharing X axis) a Plot. After reading answers to similar questions, I've got his far:

randomData2 = Table[{RandomVariate[dist], RandomReal[]}, {i, 1, 3000}];

opts = {AspectRatio -> 1, PlotRangePadding -> 0, ImagePadding -> 50}; 
plot1 = ListPlot[ randomData2,
            PlotMarkers -> {".", 10}, Frame -> {True,True,True, True},
            FrameTicks -> {{None, All}, {True, None}}, opts ];
plot2 = Histogram[ randomData2[[All, 1]], 50, "ProbabilityDensity",
            ChartStyle -> Red, ChartBaseStyle -> Directive[Opacity[0.5]],
            FrameStyle -> {Automatic, Automatic, Automatic, Red}, opts];
plot3 = Plot[PDF[dist, x], {x, 0, 5},
            PlotStyle -> PointSize[Medium], AspectRatio -> 1,
            PlotRangePadding -> 0, ImagePadding -> 50, Frame -> True];
plot4 = Overlay[{plot1, plot2}, ImageSize -> Full]

GraphicsGrid[{{plot3}, {plot4}}] 

My questions:

  1. The Overlay seems to be a little "moved". Numbers and ticks in the X Axis are not exactly on top of the previous. I could hide one, but I guess that the plots are not exactly aligned. How to align them exactly and automatically?
  2. How to make the plots in the GraphicsGrid of the same size, scale and aligned so that one can easily compare the results in the upper and lower graphic?

I cannot manually align them with padding as I have to use the same structure with many different graphics of different sizes and ranges.

$\endgroup$
5
$\begingroup$

Overlay is not so useful for precisely aligning graphics objects with respect to the coordinate system of the Graphics itself. For that, you should use Inset, or directly apply Show.

A quick way to do this is by using the function plotGrid from my answer here, which is built on Inset. To make it work, we have to adjust the PlotRange for each of the plots so that they can be stuck together seamlessly.

I combine plot1 and plot2 using Show, and then put the result in the form of a List that mirrors the shape of the graphics grid I want to produce. This is then fed into plotGrid:

dist = NormalDistribution[]; randomData2 = 
 Table[{RandomVariate[dist], RandomReal[]}, {i, 1, 3000}];
opts = {AspectRatio -> 1, PlotRangePadding -> 0, ImagePadding -> 50};

plot1 = ListPlot[randomData2, PlotMarkers -> {".", 10}, 
   PlotRange -> {{-3, 5}, {0, 1.04}}, Frame -> True, 
   FrameTicks -> {{None, All}, {True, None}}, opts];
plot2 = Histogram[randomData2[[All, 1]], 50, "ProbabilityDensity", 
   ChartStyle -> Red, ChartBaseStyle -> Directive[Opacity[0.5]], 
   FrameStyle -> {Automatic, Automatic, Automatic, Red}, opts];


plot3 = Plot[PDF[dist, x], {x, -3, 5}, PlotStyle -> PointSize[Medium],
    Frame -> True, PlotRange -> {{-3, 5}, {-.04, .4}}];

plotGrid[{{plot3}, {Show[plot1, plot2]}}, 500, 300]

plotgrid

$\endgroup$
  • $\begingroup$ Thanks a lot! Great function and great answer! $\endgroup$ – user1084363 Mar 18 '15 at 22:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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