Combining two or more plots

Mathematica lets you store plots in variables so that you can combine several individual plots in a composite figure. Enter

   splot = Plot[ Sin[x], {x, 0, 2 Pi} ]
   cplot = Plot[ Cos[x], {x, 0, 2 Pi} ]
and you will get two individual plots of the sine and cosine function. To plot both functions on the same set of axes, enter
   Show[splot, cplot]
You can combine different types of plots in this fashion. For example, you might want to combine a plot of experimental data points with a plot of a curve that fits through these points. The data points could be plotted with a ListPlot command, and the curve with a Plot command. Then the Show command would combine these two plots.

Sometimes you will generate a plot, and after looking at it, decide that you want to save it in a variable so that you can combine it with another plot. To do this, you can use the % shortcut we already mentioned. For example,

   Plot[ Sin[x], {x, 0, 2 Pi} ]
   splot = %
will store the plotted sine curve in the variable splot.

The Show command will combine several plots at once:

   splot = Plot[ Sin[x], {x, 0, 2 Pi} ]
   cplot = Plot[ Cos[x], {x, 0, 2 Pi} ]
   tplot = Plot[ Tan[x], {x, 0, 2 Pi} ]
   Show[splot, cplot, tplot]
Or you can combine the plots in stages:
   splot = Plot[ Sin[x], {x, 0, 2 Pi} ]
   cplot = Plot[ Cos[x], {x, 0, 2 Pi} ]
   sandc = Show[splot, cplot]
   tplot = Plot[ Tan[x], {x, 0, 2 Pi} ]
   Show[sandc, tplot]

When you combine two or more plots, you may want to adjust the limits of the x and y axes to focus attention on a particular region of the plot. The PlotRange modifier to the Show command lets you do this. Enter

   Show[splot, cplot, tplot,
        PlotRange -> { {0, 10}, {-10, 10} }]
This tells Mathematica to extend the horizontal axis so that it includes the range 0 < x < 10 and the vertical axis so that it includes the range -10 < y < 10. The general format for the PlotRange modifier is
   PlotRange -> { { <Xmin>, <Xmax> }, { <Ymin>, <Ymax> } }
You can see that the range is specified as a nested list, but not as a list of two (x, y) points.

Note that when we changed the axis limits, Mathematica did not extend the plots across the entire x axis. If you want the curves to extend all the way to x = 10, you need to specify this in the original Plot commands:

   splot = Plot[ Sin[x], {x, 0, 10} ]
   cplot = Plot[ Cos[x], {x, 0, 10} ]
   tplot = Plot[ Tan[x], {x, 0, 10} ]
   Show[splot, cplot, tplot,
        PlotRange -> { {0, 10}, {-10, 10} }]


Other parts of the Mathematica tutorial: