2
$\begingroup$

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: enter image description here

when

y1 = 2;

f2[x_, y_] = x^3 y

the outcome for dynamical legends will be as follows: enter image description here

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

$\endgroup$
2
  • $\begingroup$ Could you explain a bit about your desired outcome? plotting with multiple y1 or using Manipulate or ... $\endgroup$
    – Ben Izd
    Apr 3 at 10:23
  • $\begingroup$ Thanks for your interest. I edited the post. $\endgroup$
    – 1_student
    Apr 3 at 10:36
3
$\begingroup$

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:

enter image description here

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

$\endgroup$
2
  • $\begingroup$ Thank you. What is the difference between <> ToString[y1] <> and <> ToString[y1]. I am quite new in Mathematica. $\endgroup$
    – 1_student
    Apr 3 at 11:52
  • 1
    $\begingroup$ @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] , "]"]. $\endgroup$
    – Ben Izd
    Apr 3 at 12:33
4
$\begingroup$

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, ""}}]

enter image description here

If you now change y1:

y1 = 10;

or if you change the function f1:

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

enter image description here

$\endgroup$
3
$\begingroup$
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, 
     ""}}]

output of the evaluated ToString converted constant

$\endgroup$
4
  • $\begingroup$ Is Evaluate necessary in the f1[x," <> ToString[Evaluate[y1]] <> "]. I think that f1[x," <> ToString[y1] <> "] runs, too ? $\endgroup$
    – 1_student
    Apr 3 at 11:44
  • 1
    $\begingroup$ 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. $\endgroup$ Apr 3 at 12:00
  • $\begingroup$ Thank you for all. $\endgroup$
    – 1_student
    Apr 3 at 12:10
  • $\begingroup$ @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. $\endgroup$
    – b3m2a1
    Apr 9 at 18:41

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.