9
$\begingroup$
Plot[2x, {x,0,4}];
Plot[x^2, {x,4,8}];

How do I merge these two graphs into one?

$\endgroup$

closed as off-topic by Karsten 7., Öskå, RunnyKine, ybeltukov, rhermans Oct 26 '14 at 20:25

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "This question arises due to a simple mistake such as a trivial syntax error, incorrect capitalization, spelling mistake, or other typographical error and is unlikely to help any future visitors, or else it is easily found in the documentation." – Karsten 7., Öskå, RunnyKine, ybeltukov, rhermans
If this question can be reworded to fit the rules in the help center, please edit the question.

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

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}]

Mathematica graphics

Another example:

tab = Table[x^(1/n) p[x, n, n + 1], {n, 1, 10}]; 
Plot[tab, {x, 0, 8}, PlotStyle -> Thick]

Mathematica graphics

$\endgroup$
4
$\begingroup$

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]]

enter image description here

Plot[{Piecewise[{{2 x, 0<=x<4}}, Indeterminate], 
      Piecewise[{{x^2, 4<x<= 8}}, Indeterminate]}, {x, 0, 8}, BaseStyle -> Thickness[.02]]

enter image description here

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

enter image description here

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

enter image description here

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]]

enter image description here

tab = Table[x^(1/n) p[x, n, n + 1], {n, 1, 10}];
Plot[tab, {x, 0, 8}, PlotStyle -> Thickness[.02]]

enter image description here

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]]

enter image description here

$\endgroup$
  • $\begingroup$ 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}] $\endgroup$ – Bob Hanlon Oct 26 '14 at 18:12
  • $\begingroup$ @Bob, multiple colors is in fact the reason for the clunkier multiple Piecewises. $\endgroup$ – kglr Oct 26 '14 at 18:41

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