2
$\begingroup$

I want to generate a list as below for the legend of my plot,

{D=0, D=0.5, D=1, ...}

using the code

Table["D="  defocus, {defocus, Range[0, 3, 0.5]}]

However, it returns me the following,

{0., 0.5 "D=", 1. "D=", 1.5 "D=", 2. "D=", 2.5 "D=", 3. "D="}

How could I fix this problem? Thank you very much in advance.

$\endgroup$
  • 4
    $\begingroup$ Table["D=" <> ToString[defocus], {defocus, Range[0, 3, 0.5]}] $\endgroup$ – Sjoerd C. de Vries Feb 14 '16 at 22:06
  • 1
    $\begingroup$ BTW you can leave away the Range function and just use the numbers. $\endgroup$ – Sjoerd C. de Vries Feb 14 '16 at 22:16
  • $\begingroup$ Thank you. Now I see why my code doesn't work. $\endgroup$ – Bemtevi77 Feb 14 '16 at 22:18
  • $\begingroup$ Related: (20412) $\endgroup$ – Mr.Wizard Feb 16 '16 at 17:17
  • $\begingroup$ Thank you so much for showing me this thread $\endgroup$ – Bemtevi77 Feb 17 '16 at 1:52
5
$\begingroup$

Use Row to build the legend text.

funcs = Table[With[{k = k}, k # &], {k, 0, 3, 1/2}];
legends = Table[Row[{"D = ", N @ k}], {k, 0, 3, 1/2}];
Plot[Evaluate @ Through[funcs[x]], {x, 0, 5}, PlotLegends -> legends]

plot

| improve this answer | |
$\endgroup$

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