2
$\begingroup$

This question already has an answer here:

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$

marked as duplicate by MarcoB, user9660, Yves Klett, rhermans, Mr.Wizard plotting Feb 16 '16 at 17:19

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • 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$ – Yaofeng 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$ – Yaofeng 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

$\endgroup$

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