2
$\begingroup$

How can I plot x from 0 to 1, and x^2, from 1 to 2?

Plot[{x, x^2}, {x, 0, 2}]

Thus, in a way, I want to combine the two functions into one plot.

$\endgroup$
3
  • 1
    $\begingroup$ Show[Plot[x^2, {x, 1, 3}], Plot[x, {x, 0, 1}], PlotRange -> All] $\endgroup$
    – Karsten7
    Commented Apr 26, 2015 at 11:19
  • $\begingroup$ Piecewise or similar functions. $\endgroup$
    – Karsten7
    Commented Apr 26, 2015 at 11:21
  • $\begingroup$ @Karsten7. Indeed, it's a bit hard to say if this question is about piecewise plotting, or multiple plots. $\endgroup$
    – kirma
    Commented Apr 26, 2015 at 11:23

1 Answer 1

11
$\begingroup$

Combine Plots with Show, and remember to use PlotRange -> All:

Show[
 Plot[x, {x, 0, 1}, PlotStyle -> Blue],
 Plot[x^2, {x, 1, 2}, PlotStyle -> Red],
 PlotRange -> All]

enter image description here

Another option, as Karsten7. stated, is to use piecewise functions with non-numeric default value outside their domain:

Plot[
 {Piecewise[{{x, x < 1}}, Null], 
  Piecewise[{{x^2, x >= 1}}, Null]},
 {x, 0, 2}]

enter image description here

Then again, if you are really interested of not having separate plots, you probably want a direct Piecewise expression like:

Plot[
 Piecewise[
  {{x, x < 1},
   {x^2, x >= 1}}],
 {x, 0, 2}]

enter image description here

$\endgroup$

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