2
$\begingroup$

I used the following code to generate a plot for a function with four different parameters.

Plot[
 Evaluate@Table[(100*(1 - (x - 1)^(-1/r))/((x - 1)^(1 - 1/r) + 1)),
   {r, 0.2, 0.8, 0.2}],
 {x, 2, 6},
 AxesLabel -> {"X", "y (Ooptimal choice)"}, PlotTheme -> "Detailed"]

The results are as follows:

enter image description here

I want PlotLegends to show parameters; i.e., "r = 0.2", "r = 0.4" etc. rather than the expressions. How can I do that?

$\endgroup$

2 Answers 2

6
$\begingroup$
Plot[Evaluate@Table[(100*(1 - (x - 1)^(-1/r))/((x - 1)^(1 - 1/r) + 1))
   , {r, 0.2, 0.8, 0.2}]
 , {x, 2, 6}
 , AxesLabel -> {"X", "y (Ooptimal choice)"}
 , PlotLegends -> Placed[LineLegend[Automatic
    , StringForm["r=``", #] & /@ Range[0.2, 0.8, 0.2]]
   , After
   ]
 ]

Another variation:

Clear["Global`*"]
params = Range[0.2, 0.8, 0.2]
exprs = Evaluate@
  Table[(100*(1 - (x - 1)^(-1/r))/((x - 1)^(1 - 1/r) + 1))
   , {r, params}]
labels = StringForm["r = ``", NumberForm[#, 2]] & /@ params
Plot[Evaluate@MapThread[
   Callout[Tooltip@#1, #2] &, {exprs, labels}]
 , {x, 2, 6}
 , AxesLabel -> {"X", "y (Optimal choice)"}
 (*,PlotLegends\[Rule]Placed[LineLegend[Automatic,labels]
 ,After]*)
 ]

enter image description here

$\endgroup$
5
$\begingroup$
Module[
    {
        params =Table[r,  {r, 0.2, 0.8, 0.2}]
    },
    Plot[
        Evaluate[
            ReplaceAll[
                (100*(1 - (x - 1)^(-1/r))/((x - 1)^(1 - 1/r) + 1))
                , r -> params
            ]
        ]
        , {x, 2, 6}
        , FrameLabel   -> {"X", "y (Ooptimal choice)"}
        , PlotTheme   -> "Detailed"
        , PlotLegends -> StringTemplate["r = ``"] /@ params
    ]
]

enter image description here

or

Module[
    {
        params = Table[r,  {r, 0.2, 0.8, 0.2}],
        expre  = (100*(1 - (x - 1)^(-1/r))/((x - 1)^(1 - 1/r) + 1))
    },
    Plot[
        Evaluate[
            ReplaceAll[
                expre
                , r -> params
            ]
        ]
        , {x, 2, 6}
        , FrameLabel   -> {"X", "Y (Optimal choice)"}
        , PlotTheme   -> "Scientific"
        , PlotLegends -> StringTemplate["r = ``"] /@ params
        , Epilog-> Inset[Framed[TraditionalForm[Factor@expre]], {5,20}]
    ]
]

enter image description here

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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