3
$\begingroup$

I want to combine a Plot and a ListPlot at their common x-axis. Here's an example of what I've got so far:

fgraph = Plot[1 - x^2
  - 0.5654788166963185 E^(-2.25 (-0.2 + x)^2)
  - 0.5654788166963185 E^(-2.25 (0.2 + x)^2), {x, -1, 1}, 
  AxesOrigin -> {-1, 0}];
ggraph = ListPlot[
  {{{-0.2, 0}, {-0.2, 0.5}}, {{0.2, 0}, {0.2, 0.35}}},
  Joined -> True, PlotRange -> {{-1, 1}, {0, All}},
  AxesOrigin -> {1, 0}];
GraphicsColumn[{ggraph, fgraph}, Automatic, -112]

Mathematica graphics

It looks pretty close to what I want, but there are a few problems:

  1. The x-axes are not on the exact same horizontal scale, so they don't line up perfectly,
  2. The spacing of -112 in GraphicsColumn had to be found manually and it's not quite right either, and
  3. I'd like to be able to adjust the top plot's AspectRatio to make it around half the current height.

The solution needs to be automated since this is only an example.

Note that I need to keep the two different scales for the y-axis, since the y-axes in fgraph and ggraph are measured in different units and may be orders of magnitude different. This problem seems similar to 1 Plot, 2 Scale/Axis but different since I want the same x-axis at y==0.

$\endgroup$
  • $\begingroup$ Use Show Show[ggraph, fgraph, PlotRange -> All, AxesOrigin -> {0, 0}] $\endgroup$ – Bob Hanlon May 20 at 14:07
  • $\begingroup$ @BobHanlon That's easy and looks OK in this example, but I need to keep the two different scales for the y-axis since the y-axes in fgraph and ggraph are measured in different units and may be orders of magnitude different. I'll clarify my question. $\endgroup$ – Chris K May 20 at 14:11
4
$\begingroup$

I'll also use kglr's approach (in his deleted answer) of insetting the list plot into the Plot output. The one tricky part is that the entire list plot, both plot range and image padding, gets inserted. This means that if the object has any image padding, it will be shrunk when it is placed into the Plot output. This is what happened in kglr's deleted answer.

The trick, then, is to eliminate the image padding, and make sure that Plot uses the same plot range, which almost works:

fgraph = Plot[
    1-x^2-0.5654788166963185 E^(-2.25 (-0.2+x)^2)-0.5654788166963185 E^(-2.25 (0.2+x)^2),
    {x,-1,1},
    AxesOrigin->{-1,0},
    PlotRange -> {{-1, 1}, Automatic} (* same plot range *)
];
ggraph=ListPlot[
    {{{-0.2,0},{-0.2,0.5}},{{0.2,0},{0.2,0.35}}},
    Joined->True,
    PlotRange->{{-1,1},{0,All}},
    AxesOrigin->{1,0},
    AspectRatio -> 1/4, (* adjust aspect ratio *)
    ImagePadding->0 (* eliminate padding *)
];

Show[fgraph, Epilog -> Inset[ggraph, {1, 0}, {1, 0}, Scaled[1]]]

enter image description here

Note that the horizontal axis matches perfectly now. The main issue is that setting ImagePadding to 0 caused the list plots vertical axis to disappear. One simple remedy is to increase the plot range to include the vertical axis:

fgraph = Plot[
    1-x^2-0.5654788166963185 E^(-2.25 (-0.2+x)^2)-0.5654788166963185 E^(-2.25 (0.2+x)^2),
    {x,-1,1},
    AxesOrigin->{-1,0},
    PlotRange -> {{-1, 1.05}, Automatic} (* same plot range *)
];
ggraph=ListPlot[
    {{{-0.2,0},{-0.2,0.5}},{{0.2,0},{0.2,0.35}}},
    Joined->True,
    PlotRange->{{-1,1.05},{0,All}},
    AxesOrigin->{1,0},
    AspectRatio -> 1/4, (* adjust aspect ratio *)
    ImagePadding->0 (* eliminate padding *)
];

Show[fgraph, Epilog -> Inset[ggraph, {1, 0}, {1, 0}, Scaled[1]]]

enter image description here

$\endgroup$
  • $\begingroup$ Thanks to you and @kglr, that looks great! $\endgroup$ – Chris K May 20 at 16:04
  • $\begingroup$ Not to gild the lily, but would it be possible to do the same with Frame -> {{False, True}, {False, False}} on ggraph, to put the right side's ticks and labels outside the graph? I haven't been able to get that working. $\endgroup$ – Chris K May 20 at 17:23
  • $\begingroup$ That's harder, because frame ticks always live in ImagePadding. $\endgroup$ – Carl Woll May 20 at 17:41
  • $\begingroup$ This is still awesome! $\endgroup$ – Chris K May 20 at 17:46

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.