I am trying to understand the differences of Overlay and Show to combine two plots. A typical use case is to superpose a ListPlot of some data with a Plot of a curve to fit the data.

The following example comes from the Documentation:

data = {{0, 1}, {1, 0}, {3, 2}, {5, 4}};
line = Fit[data, {1, x}, x];
parabola = Fit[data, {1, x, x^2}, x];

datplot = ListPlot[data, PlotStyle -> Red];
fitplot = Plot[{line, parabola}, {x, 0, 5}];

Then I tried to Show them together or to Overlay them:

{Show[datplot, fitplot], Overlay[{datplot, fitplot}]}

enter image description here

In this case Overlay doesn't work very well. But this is resolved by setting a common PlotRange for both plots.

datplot = ListPlot[data, PlotStyle->Red, PlotRange->{{0,5},{0,5}}];
fitplot = Plot[{line,parabola}, {x,0,5}, PlotRange->{{0,5},{0,5}}];
{Show[datplot,fitplot], Overlay[{datplot,fitplot}]}

enter image description here

And this time we obtain identical results.

So my question is:

What are the differences of Show and Overlay for plots? Can I do something with Show that I cannot do with Overlay? When is one more convenient than the other (again, in the context of plot superposition)?

  • 2
    $\begingroup$ Overlay is not a type of graphics… unlike Show ? $\endgroup$
    – chris
    Commented Dec 30, 2015 at 15:09
  • 4
    $\begingroup$ I'll point out that when you say "we obtain identical results", it seems that that's not quite true. If you look at the last two plots carefully, you can see that the numbers and axes are slightly darker in the second plot than the first. This is because in the first, the two plots get one axis, whereas in the second, you are overlaying one axis on another. $\endgroup$
    – march
    Commented Dec 30, 2015 at 17:45
  • $\begingroup$ @march Good point. $\endgroup$
    – a06e
    Commented Dec 30, 2015 at 21:59
  • 4
    $\begingroup$ Your statement about rasterization is not true. Overlay never rasterizes anything. The point is that Show called on multiple Graphics objects will always return something with head Graphics that has effectively merged the primitive list and options, while Overlay returns a thing with head Overlay that merely shows the items (which need not be graphics) visually stacked. $\endgroup$
    – John Fultz
    Commented Dec 31, 2015 at 2:37

3 Answers 3


One important use of Overlay is when two plots are differently scaled but you want to show them together anyway. Here is one example:

C1 = RGBColor[0.368, 0.507, 0.710];
C2 = RGBColor[0.881, 0.611, 0.142];

p1 =
   {1, 7, 2000, 5, 4, 7, 164},
   ImagePadding -> 45,
   Frame -> {True, True, True, False},
   FrameLabel -> "Blue Series",
   FrameStyle -> {Automatic, C1, Automatic, Automatic},
   FrameTicks -> {All, All, None, None},
   GridLines -> Automatic,
   PlotRange -> {{1, Automatic}, All},
   PlotStyle -> C1];

p2 =
   {1, 4, 3, 5, 2, 6, 8},
   Axes -> False,
   ImagePadding -> 45,
   Frame -> {False, False, False, True},
   FrameLabel -> {None, None, None, "Orange Series"},
   FrameStyle -> {Automatic, Automatic, Automatic, C2},
   FrameTicks -> {None, None, None, All},
   PlotRange -> {{1, Automatic}, All},
   PlotStyle -> C2];

Overlay[{p1, p2}]

enter image description here

Now the same with Show

Show[{p1, p2}]

enter image description here

Show preserves the different scalings whereas Overlay "simply" superimposes one "image" upon the other.

  • 1
    $\begingroup$ +1 Thanks! This is clever use-case where Overlay works but Show doesn't. $\endgroup$
    – a06e
    Commented Dec 30, 2015 at 15:24
  • $\begingroup$ The scale 0 to 8 of the orange series is missing on Windows 10x64, Mathematica 10.3.1 $\endgroup$
    – user36273
    Commented Dec 30, 2015 at 16:19
  • $\begingroup$ Strange, I am on 10.0 $\endgroup$
    – eldo
    Commented Dec 30, 2015 at 16:21

The other answers point out important differences which I won't repeat. But here's another one. Overlay is implemented by drawing everything. It visually stacks all of its contents. Show is implemented by merging the primitive lists and options. This introduces some important differences.

For example, if the options contradict one another in plots, then Show will pick the first fully resolved option. E.g.,

Show[Plot[Sin[x], {x, 0, 2 \[Pi]}, AxesOrigin -> {0, 0}], 
 Plot[Cos[x], {x, 0, 2 \[Pi]}, AxesOrigin -> {\[Pi], 0}]]

enter image description here

Or, you can even add the option to Show to override all local options.

Show[Plot[Sin[x], {x, 0, 2 \[Pi]}, AxesOrigin -> {0, 0}], 
 Plot[Cos[x], {x, 0, 2 \[Pi]}, AxesOrigin -> {\[Pi], 0}], 
 AxesOrigin -> {2 \[Pi], 0}]

enter image description here

Overlay can do nothing more than a visual superposition. Incidentally, since Overlay is doing a visual superposition, that means that it might end up double-drawing things that you don't intend to be double-drawn. For example, imagine you have plots which have GridLines (for simplicity, I'll just use the same plot twice here):

p = Plot[x, {x, 0, 1}, GridLines -> Automatic]

enter image description here

Overlay[{p, p}]

enter image description here

Oops..what happened to those grid lines? They got a lot darker in the Overlay version. They got darker because, by default, GridLines are rendered as partially transparent. This was done so that they look reasonable regardless of the choice of background color you might have chosen for the graphic. But here, what you've done is to overdraw a transparent color on itself, thereby making a darker color. If that description wasn't very clear, then this should help to illustrate what I'm talking about:

Graphics[{GrayLevel[0, 0.3],
  Rectangle[{0, 0}, {1.2, 1.2}], Rectangle[{.8, .8}, {2, 2}]}]

enter image description here

That is exactly what's going on in the Overlay case. And, furthermore, it even has subtle drawing effects on things you might not have noticed at first. For example, antialiasing is implemented as a pixel compositing operation, effectively equivalent to using transparency. Which means that anything in a graphic which is antialiased and multiply drawn is going to look darker around the edges. That's why the tick labels in the Overlay example above look darker. If you look carefully at the screenshot you provide in your question, you'll notice that your tick labels look darker, too.

Okay...so those are some subtle issues with overdrawing. Let's try a non-subtle issue...plots with background colors:

Overlay[{Plot[Sin[x], {x, 0, 2 Pi}, Background -> LightBlue], 
  Plot[Cos[x], {x, 0, 2 Pi}, Background -> LightOrange]}]

enter image description here

Oops...where did the sine wave go? Because the plots are visually stacked, the background color for the second plot completely obscures everything that was drawn as part of the first graphic.


Following your example,

  pp=={Show[datplot,fitplot], Overlay[{datplot,fitplot}]};

I can continue to operate on Show, e.g. as

  pp[[1]] // Show[#, PlotRange -> {0, 1}] &


  pp[[2]] // Show[#, PlotRange -> {0, 1}] &


Overlay is not a type of graphics. >>

  • $\begingroup$ +1 You're right. This is an important difference. Thanks! However, if you export the graphics, Overlay produces unrasterized output (see my edited question). $\endgroup$
    – a06e
    Commented Dec 30, 2015 at 15:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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