0
$\begingroup$

I am trying to plot the following graph for different values of t (t=1 to t=10) in one plot.

Plot[Sqrt[a[x/x0[t], t]], {x, 0, x0[t]}, 
 PlotRange -> {{0, 14}, {0, 6}}]

When I use

 Plot[Sqrt[a[x/x0[1], 1]], {x, 0, x0[1]}, 
     PlotRange -> {{0, 14}, {0, 6}}]

I get a correct output. However, when I use this in a for loop (as follows) I get no output.

For[t = 1, t < 10, t++, 
 Plot[Sqrt[a[x/x0[t], t]], {x, 0, x0[t]}, 
  PlotRange -> {{0, 14}, {0, 6}}]]
$\endgroup$
2
  • $\begingroup$ Maybe wrap a Print around your Plot[...] Or try a[u_,v_]:=u^3;x0[t_]:=t^4; Table[ Plot[Sqrt[a[x/x0[t], t]],{x,0,x0[t]},PlotRange->{{0,14},{0,6}}],{t,1,10}] but we are just guessing what your a[u_,v_] and your x0[t] are $\endgroup$
    – Bill
    Commented Jul 10 at 15:47
  • $\begingroup$ Thanks. These both work but show many graphs at output. Is there any way one could show them all in one graph with different colors for different values of t? @Bill $\endgroup$ Commented Jul 10 at 16:05

1 Answer 1

2
$\begingroup$

You can generate the plots using Table and combine them into one plot with Show.

You don't define the functions a and x0 in your question, so I use dummy definitions as an example:

a[y_, t_] := 2 y^2 + 2 t;
x0[t_] := t + 4;


plot = Show[
  Table[
    Plot[Sqrt[a[x/x0[t], t]], {x, 0, x0[t]}, 
       PlotRange -> {{0, 14}, {0, 6}}, PlotStyle -> ColorData[97][t]], 
  {t,10}]
]

This gives

enter image description here

If you want to a legend showing the color of each t value, first create the legend:

legend = LineLegend @@ Transpose[Table[{ColorData[97][t], t}, {t, 10}]]

then combine it with the plot

Legended[plot, legend]
$\endgroup$
4
  • $\begingroup$ Is it possible to show what t value each color is related to? $\endgroup$ Commented Jul 10 at 16:46
  • $\begingroup$ yes, see my edit for how to add a legend $\endgroup$
    – tad
    Commented Jul 10 at 16:53
  • $\begingroup$ would it be possible to add the legend label "t" to the legend too? $\endgroup$ Commented Jul 10 at 17:05
  • $\begingroup$ yes. Check the documentation of LineLegend for ways to format the legend. The option LegendLabel adds a label. $\endgroup$
    – tad
    Commented Jul 10 at 18:07

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.