# How to plot a two-variable function in 2D?

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?

• 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? Commented Oct 9, 2020 at 19:34
• As @DanielHuber sugested, isn't this what you want? reference.wolfram.com/language/howto/… Commented Oct 9, 2020 at 19:35
• 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. Commented Oct 9, 2020 at 19:43

You can try ParametricPlot:

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


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


• Okay. This perfect. Thanks alot. Commented Oct 9, 2020 at 20:19
• @SamapanBhadury, my pleasure. Thank you for the accept.
– kglr
Commented Oct 9, 2020 at 20:22
• Thanks. This was a great. Commented Oct 9, 2020 at 20:31

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]


• 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] Commented Oct 10, 2020 at 0:08
• @BobHanlon Thanks your example. You can test my update example. Commented Oct 10, 2020 at 0:19
• 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] Commented Oct 10, 2020 at 0:27
• @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}} Commented Oct 10, 2020 at 1:00

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


After the answer by @kglr I found another solution:

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

This generates:

• 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"}] Commented Oct 9, 2020 at 23:29