3
$\begingroup$

Say I have a function $y = ax^2$. I want a plot of '$y$' vs '$x$'. Here '$a$' is a parameter. I can set different values of '$a$'and get some disjoint set of plots by the following command:

Plot[{1*x^2, 2*x^2, 3*x^2},{x,0,10}]

where, I set $a= 1,2,3$ values. But the plots here are disjoint as $a$ does not take continuous values. What I want is to get a plot of '$y$' vs '$x$' by varying '$a$' continuously between $1$ to $3$. How do I do this?

Note that DensityPlot[a*x^2,{x,0,10},{a,1,3}] gives a plot with '$a$' as y-axis and '$x$' as x-axis. This is not what I want. Also using the command Manipulate[Plot[a*x^2,{x,0,10}],{a,1,3}] we get a moving picture. Again not what I need.

I hope I could make it clear, what is my requirement. I could not find this problem online. I have seen this can be done in some papers. Any ideas?

$\endgroup$
3
  • $\begingroup$ It is not clear what you want. You have 3 variables. I would have proposed Plot3D, but you said you do not want "a" as an axis? $\endgroup$ Oct 9 '20 at 19:34
  • $\begingroup$ As @DanielHuber sugested, isn't this what you want? reference.wolfram.com/language/howto/… $\endgroup$
    – Q.P.
    Oct 9 '20 at 19:35
  • $\begingroup$ No. These are not what I need. I am aware of Plot3D. I need the plot to be in 2D. See the link. Go to page 8. Figure 4 will make things more clear. There $\zeta_Q/\zeta$ has been plotted as a function of $\alpha$ by varying the parameter $z$ from 1 to 10. $\endgroup$ Oct 9 '20 at 19:43
5
$\begingroup$

You can try ParametricPlot:

ParametricPlot[{x, a x^2}, {x, 0, 10}, {a, 1, 3}, AspectRatio -> 1]

enter image description here

You can highlight lines corresponding to specific values of a using the options MeshFunctions and Mesh:

alist = {1, 2, 3};
colors = Opacity[1, #] & /@ {Red, Green, Purple};
mesh = Thread[{alist, Directive[Thick, #] & /@ colors}];

ParametricPlot[{x, a x^2}, {x, 0, 10}, {a, 1, 3}, AspectRatio -> 1, 
 PlotStyle -> LightOrange,
 BoundaryStyle -> None,
 MeshFunctions -> {#4 &},
 Mesh -> {mesh},
 PlotLegends -> 
  LineLegend[colors, alist, LegendLabel -> Style["a = ", 16]]]

enter image description here

$\endgroup$
3
  • $\begingroup$ Okay. This perfect. Thanks alot. $\endgroup$ Oct 9 '20 at 20:19
  • $\begingroup$ @SamapanBhadury, my pleasure. Thank you for the accept. $\endgroup$
    – kglr
    Oct 9 '20 at 20:22
  • $\begingroup$ Thanks. This was a great. $\endgroup$ Oct 9 '20 at 20:31
3
$\begingroup$

Here we give an example which Filling not alway easy handle.So we have to use ParametricPlot or ParametricRegion

f[x_, a_] = Sin[x + a] ((a - 1) (a - 3) + x);
curves = Plot[Table[f[x, a], {a, 1, 3, .1}] // Evaluate, {x, -2, 2}, 
   AspectRatio -> 1];
region = ParametricPlot[{x, f[x, a]}, {a, 1, 3}, {x, -2, 2}, 
   PlotStyle -> Directive[Opacity[0.2], Yellow]];
Show[curves, region]

enter image description here

$\endgroup$
4
  • 1
    $\begingroup$ Why do you say that Filling doesn't work? Plot[Table[f[x, a], {a, 1, 3, .5}] // Evaluate, {x, -2, 2}, AspectRatio -> 1, Filling -> {3 -> {5}}, FillingStyle -> LightYellow] $\endgroup$
    – Bob Hanlon
    Oct 10 '20 at 0:08
  • $\begingroup$ @BobHanlon Thanks your example. You can test my update example. $\endgroup$
    – cvgmt
    Oct 10 '20 at 0:19
  • $\begingroup$ f[x_, a_] = Sin[x + a] ((a - 1) (a - 3) + x); Plot[Table[f[x, a], {a, 1, 3, .5}] // Evaluate, {x, -2, 2}, AspectRatio -> 1, Filling -> {1 -> {5}, 4 -> {5}, 3 -> {4}}, FillingStyle -> LightRed] $\endgroup$
    – Bob Hanlon
    Oct 10 '20 at 0:27
  • $\begingroup$ @BobHanlon a=2.8 ,Plot[f[x, 2.8], {x, -2, 2}, AspectRatio -> 1, PlotStyle -> Black] is not all in the filling region Filling -> {1 -> {5}, 4 -> {5}, 3 -> {4}} $\endgroup$
    – cvgmt
    Oct 10 '20 at 1:00
3
$\begingroup$

Based on @kglr's answer, but using the option ColorFunction in ParametricPlot. Basically the arguments of ColorFunction are the actual Cartesian coordinates (which I have called xx and yy) followed by the parameter variables (in this case x and a):

{xMin, xMax} = {0, 10};
{aMin, aMax} = {1, 3};
ital[str_] := Style[str, Italic];
ParametricPlot[
  {x, a x^2}
  , {x, xMin, xMax}
  , {a, aMin, aMax}
  , AspectRatio -> 1 / GoldenRatio
  , ColorFunction -> Function[{xx, yy, x, a}, Hue[a]]
  , FrameLabel -> ital /@ {"x", "y"}
  , LabelStyle -> Directive[Black, 14]
  , RotateLabel -> False
  , PlotLabel -> ital["y"] == ital["a"] ital["x"]^2
  , PlotLegends -> Placed[
      BarLegend[{Hue, {aMin, aMax}}
        , LegendLabel -> ital["a"]
        , LegendLayout -> "Column"
      ],
      After
    ]
]

ParametricPlot with filling based on the value of a

$\endgroup$
2
$\begingroup$

After the answer by @kglr I found another solution:

Plot[{x^2, 3*x^2}, {x, 0, 10}, Filling -> {1 -> {2}}]

This generates:enter image description here

$\endgroup$
1
  • $\begingroup$ Manipulate[Plot[{3 x^2, a*x^2, x^2}, {x, 0, 10}, PlotStyle -> {Dashed, Automatic, Dashed}, Filling -> {1 -> {3}}, PlotLegends -> Placed[{3 x^2, a*x^2, x^2}, {0.3, 0.6}]], {{a, 1.5}, 1, 3, 0.05, Appearance -> "Labeled"}] $\endgroup$
    – Bob Hanlon
    Oct 9 '20 at 23:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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