# Plotting two functions in one graph, with different value ranges [closed]

Plot[2x, {x,0,4}];
Plot[x^2, {x,4,8}];


How do I merge these two graphs into one?

• Show[ Plot[2 x, {x, 0, 4}], Plot[x^2, {x, 4, 8}], PlotRange -> All]? – Kuba Oct 26 '14 at 16:01
• Related topics: (128), (627), (1128), (8199), (77397) – Mr.Wizard Feb 9 '16 at 9:38

Here's a way that may serve you also for other purposes:

p[x_, left_, right_] := HeavisideTheta[x - left] HeavisideTheta[right - x]
Plot[{2 x p[x, 0, 4], x^2 p[x, 4, 8]}, {x, 0, 8}] Another example:

tab = Table[x^(1/n) p[x, n, n + 1], {n, 1, 10}];
Plot[tab, {x, 0, 8}, PlotStyle -> Thick] Update: Using thicker lines to make the difference between various methods visible:

Plot[{ConditionalExpression[2 x, 0 <= x < 4], ConditionalExpression[x^2, 4 < x <= 8]}, {x, 0, 8},
BaseStyle -> Thickness[.02]] Plot[{Piecewise[{{2 x, 0<=x<4}}, Indeterminate],
Piecewise[{{x^2, 4<x<= 8}}, Indeterminate]}, {x, 0, 8}, BaseStyle -> Thickness[.02]] ct = ConditionalExpression[#, #2] & @@@ Table[{x^(1/n), n < x <= n + 1}, {n, 10}];
Plot[ct, {x, 0, 8}, PlotStyle -> Thickness[.02]] pw = Piecewise[{{#, #2}}, Indeterminate] & @@@ Table[{x^(1/n), n < x <= n + 1}, {n, 10}];
Plot[pw, {x, 0, 8}, PlotStyle -> Thickness[.02]] whereas,

p[x_, left_, right_] := HeavisideTheta[x - left] HeavisideTheta[right - x]
Plot[{2 x p[x, 0, 4], x^2 p[x, 4, 8]}, {x, 0, 8}, BaseStyle -> Thickness[.02]] tab = Table[x^(1/n) p[x, n, n + 1], {n, 1, 10}];
Plot[tab, {x, 0, 8}, PlotStyle -> Thickness[.02]] Similarly, using Boole in place of HeavisideTheta:

Plot[{2 x Boole[0 <= x <= 4], x^2 Boole[4 < x <= 8]}, {x, 0, 8}, BaseStyle -> Thickness[.02]] • If single-color plot is acceptable, Piecewise approach can be simplified to Plot[Piecewise[{{2 x, 0 <= x < 4}, {x^2, 4 < x <= 8}}, Indeterminate], {x, 0, 8}] – Bob Hanlon Oct 26 '14 at 18:12
• @Bob, multiple colors is in fact the reason for the clunkier multiple Piecewises. – kglr Oct 26 '14 at 18:41