2
$\begingroup$

f is a function a single variable (x) and it depends on a parameter (b). It was plotted as shown below. I am looking for a modality to change the labels. Currently, the plots are labeled as "1." "0.75" "0.5" "0.25". I would like to include the symbol for the parameter in the label and to write each associated numerical value using two decimals, i.e., "b=1.00" "b=0.75" "b=0.50" "b=0.25".

f[x_] := ((1 + x^2) b)/2

Plot[Evaluate[Table[Labeled[f[x], b], {b, {0.25, 0.50, 0.75, 1.00}}]], {x, 0, 1}]
$\endgroup$
3
  • $\begingroup$ Thanks, kglr! Good to know about PromptForm! I experimented wit PlotLabels prior to asking the question. My solution to getting to a similar (but less elegant) result was f[x_, b_] := ((1 + x^2) b)/2 Plot[{f[x, 0.25], f[x, 0.5], f[x, 0.75], f[x, 1]}, {x, 0, 1}, PlotLabels -> {"b=0.25", "b=0.50", "b=0.75", "b=1.00"}]. Your solution is superior. I will vote for Syed's solution. The grey handles are often helpful, but in this graph's case, they seem unnecessary, as the labels themselves fit nicely without the need for handles. $\endgroup$ Aug 29 '21 at 13:33
  • $\begingroup$ Also, kglr: if I execute the code you shown below, I get an error: "Table::iterb: Iterator {b,blist} does not have appropriate bounds." $\endgroup$ Aug 29 '21 at 14:14
  • $\begingroup$ Add blist = {0.25, 0.50, 0.75, 1.00}. Either comment under kglr's answer or use @kglr so that the user is notified. $\endgroup$
    – Syed
    Aug 29 '21 at 15:17
2
$\begingroup$
f[x_] := ((1 + x^2) b)/2

Plot[Evaluate[
  Table[Labeled[f[x], 
    "b=" <> ToString@NumberForm[b, {6, 2}]], {b, {0.25, 0.50, 0.75, 
     1.00}}]], {x, 0, 1}]

enter image description here

I would also encourage you to take a look at Callout.

$\endgroup$
2
  • $\begingroup$ Thanks Syed! This is exactly what I was looking for. I explained above why. I don't think this graph needs handles, just the values of the parameter next to the plot. Mathematica: so many features... so little time (to learn of all of them) :--) $\endgroup$ Aug 29 '21 at 13:36
  • $\begingroup$ @RainforestFrog I am happy that I was able to help you. Best of luck. $\endgroup$
    – Syed
    Aug 29 '21 at 16:43
5
$\begingroup$

You can also use PromptForm + the optionPlotLabels as follows:

f[x_] := ((1 + x^2) b)/2

Plot[Evaluate[Table[f[x], {b, blist}]], {x, 0, 1}, 
 PlotLabels -> (PromptForm["b", NumberForm[#, {5,2}]] & /@ blist)]

enter image description here

Note: Unfortunately, PromptForm is not documented.

Information @ PromptForm

enter image description here

Some examples:

Column[PromptForm[lefthandside, #] & /@ {Pi, 10^3, foo, blah}]

enter image description here

$\endgroup$

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.