# dynamic FramelLabel and Legend

How to create a dynamic FramelLabel and dynamic Legend for various parameter y1? ( for the commands Plot, Plot3D or Show )

y1 = 1;
f1[x_, y_] = x^2 y
p1 = Plot[f1[x, y1], {x, 0, 5}, PlotStyle -> {Red, Dashed},
Frame -> True, FrameLabel ->   {{"f1[x,y1]", ""}, {x, ""}}]


For example, when y1 = 1, the outcome for dynamical FrameLabel will be as follows:

when

y1 = 2;

f2[x_, y_] = x^3 y


the outcome for dynamical legends will be as follows:

My Goal: I want to automatize that when I change variable y1, legend and Framelabel in Plots automatically will be changed

• Could you explain a bit about your desired outcome? plotting with multiple y1 or using Manipulate or ... Apr 3 at 10:23
• Thanks for your interest. I edited the post. Apr 3 at 10:36

I think what you're asking is Manipulate:

f1[x_, y_] = x^2 y
Manipulate[
Plot[f1[x, y1], {x, 0, 5}, PlotStyle -> {Red, Dashed}, Frame -> True,
FrameLabel -> {{"f1[x," <> ToString[y1] <> "]", ""}, {"x", ""}},
AxesOrigin -> {0, 0}, PlotLabel -> "y1=" <> ToString[y1]], {y1, 1, 5, 1}]


Result:

Also, you can plot multiple functions at the same time just like your second image, check out Manipulate Documentation.

• Thank you. What is the difference between <> ToString[y1] <> and <> ToString[y1]. I am quite new in Mathematica. Apr 3 at 11:52
• @1_student <> is used for concatenating strings, the main function is StringJoin but <> is easier to use. I used <> ToString[y1] <> to add some string before and after y1 value but with <> ToString[y1] you can't add string after the y1 value. "f1[x," <> ToString[y1] <> "]" is the equivalent to StringJoin["f1[x,",ToString[y1] , "]"]. Apr 3 at 12:33

You may try this:

y1 = 2;
f1[x_, y_] = x^2 y
p1 = Dynamic@
Plot[Evaluate[f1[x, y1]], {x, 0, 5}, PlotStyle -> {Red, Dashed},
Frame -> True, FrameLabel -> {{Dynamic[f[x, y]], ""}, {x, ""}}]


If you now change y1:

y1 = 10;


or if you change the function f1:

y1 = 2;
f1[x_, y_] = x^(1/2) y


y1 = 1;
f1[x_, y_] = x^2 y
p1 = Plot[f1[x, y1], {x, 0, 5}, PlotStyle -> {Red, Dashed},
Frame -> True,
FrameLabel -> {{"f1[x," <> ToString[Evaluate[y1]] <> "]", ""}, {x,
""}}]


• Is Evaluate necessary in the f1[x," <> ToString[Evaluate[y1]] <> "]. I think that f1[x," <> ToString[y1] <> "] runs, too ? Apr 3 at 11:44
• Depends. If like in the question y1 is assigned a value in exactly the presented way. This is Set. In the section Different kind of values there are example to be considered, scoping, if the parameter is in need to be function of some kind. To use Evaluate in this in general is somewhat a safe wrapper but not necessary to get results in all cases. This is a tricky question in Mathematica. Try a fresh variable assign, then reuse. Hold, Unevaluated differences. Apr 3 at 12:00
• Thank you for all. Apr 3 at 12:10
• @1_student Evaluate does nothing here. It would only do something if ToString had a Hold attribute, which of course it doesn't. It only looks at the function that wraps it directly. I honestly can't understand what Steffen is trying to say with that comment, but either he's saying nothing or he's saying something incorrect. Apr 9 at 18:41