3
$\begingroup$

I want to make a parametric plot like this:

ParametricPlot[{{2 t, -10 t^2}, {t, 2 t}}, {t, 0, 2}]

I have tried the following but it doesn't work:

ParametricPlot[{{2 t, -10 t^2}, {t, 2 t}}, {{t, 0, 2}, {t, 4, 7}}]
$\endgroup$

3 Answers 3

8
$\begingroup$

ConditionalExpression[] works nicely:

ParametricPlot[{ConditionalExpression[{2 t, -10 t^2}, 0 <= t <= 2], 
                ConditionalExpression[{t, 2 t}, 4 <= t <= 7]}, {t, 0, 7}, 
               AspectRatio -> 1/GoldenRatio]

split plots

$\endgroup$
4
  • $\begingroup$ J.M. are you back? :-D $\endgroup$
    – Mr.Wizard
    Apr 3, 2013 at 8:56
  • $\begingroup$ Well, back... ish. $\endgroup$ Apr 3, 2013 at 8:56
  • $\begingroup$ Thank you very much for your answer, appreciate it! $\endgroup$ Apr 3, 2013 at 9:14
  • 4
    $\begingroup$ @user1626227 This is a fine answer, and perhaps the best one you are going to get, but as a rule I recommend that you leave more time before you Accept an answer. I usually wait 24 hours so that people in all time zones have a chance to read the question and respond if they choose, before the question appears concluded. $\endgroup$
    – Mr.Wizard
    Apr 3, 2013 at 9:16
4
$\begingroup$

Without conditional constructs, just building a list of your functions and limits

intervals = {{{2 t, -10  t^2}, {0, 2}}, {{t, 2 t}, {4, 7}}};
Show[ParametricPlot[#[[1]], Evaluate@{t, Sequence @@ #[[2]]}] & /@  intervals,
     PlotRange -> Automatic, AspectRatio -> 1/GoldenRatio]

Mathematica graphics

Edit You may prefer to use \[FormalT] instead of simply t for protection against possible definitions of t elsewhere.

Edit 2

A more featured implementation, taking the color switching from @Mr's answer and forcing it to cycle so allowing to take any number of curves as argument:

f[{var_, l1_}] := Module[{style = ColorData[1] /@ Range@5},
  Show[ParametricPlot[#[[1]], Evaluate@{var, Sequence @@ #[[2]]}] & /@ l1 /.
       x_Line :> {First@(style = RotateRight[style]), x}, 
       PlotRange -> Automatic, AspectRatio -> 1/GoldenRatio]]

Usage:

l = {\[FormalT],Array[{{RandomInteger[10]\[FormalT],RandomInteger[{-10,10}]   
                    \[FormalT]^RandomReal[2]},RandomInteger[10,2]}&,10]};
f[l]

Mathematica graphics

$\endgroup$
6
  • $\begingroup$ If t is defined this fails. I think you should add a Block or at least a warning. (+1 nevertheless) $\endgroup$
    – Mr.Wizard
    Apr 3, 2013 at 9:13
  • $\begingroup$ @Mr.Wizard Usually I use \[FormalT], but I rather prefer to keep the code clean here $\endgroup$ Apr 3, 2013 at 9:18
  • $\begingroup$ @Mr.Wizard Updated with the warning $\endgroup$ Apr 3, 2013 at 9:20
  • $\begingroup$ I would have used MapThread[] instead of Map[] myself... $\endgroup$ Apr 3, 2013 at 9:41
  • $\begingroup$ @J.M. Why? I wanted the function and its associated interval clustered in one list ... $\endgroup$ Apr 3, 2013 at 9:45
3
$\begingroup$

For version 7 users without ConditionalExpression there is Piecewise:

ParametricPlot[
  Piecewise[{{{2 t, -10 t^2}, 0 <= t <= 2}, {{t, 2 t}, 4 <= t <= 7}}], {t, 0, 7},
  AspectRatio -> 1/GoldenRatio
]

Mathematica graphics

To get separate styles requires additional work:

ParametricPlot[
 pwSplit @ Piecewise[{{{2 t, -10 t^2}, 0 <= t <= 2}, {{t, 2 t}, 4 <= t <= 7}}],
 {t, 0, 7},
 AspectRatio -> 1/GoldenRatio,
 PlotStyle -> {Red, Blue},
 Evaluated -> True
]

Mathematica graphics

pwSplit code from here. Or:

Module[{style = {Red, Blue}, i = 1},
 ParametricPlot[
   Piecewise[{{{2 t, -10 t^2}, 0 <= t <= 2}, {{t, 2 t}, 4 <= t <= 7}}], {t, 0, 7},
   AspectRatio -> 1/GoldenRatio
 ] /. x_Line :> {style[[i++]], x}
]

Mathematica graphics

$\endgroup$
2
  • 1
    $\begingroup$ +1 How about something like x_Line :> {First@(style = RotateRight[style]), x} in your last piece of code? $\endgroup$ Apr 3, 2013 at 9:29
  • $\begingroup$ @belisarius that's a good suggestion if one wishes for cyclic behavior. R.M would be pleased. You could also use Mod. $\endgroup$
    – Mr.Wizard
    Apr 3, 2013 at 9:34

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.