2
$\begingroup$

I have a list of objects, which I can plot manually:

one   = MapThread[{#, 1 #}&, {Range[0, 5]}];
two   = MapThread[{#, 2 #}&, {Range[0, 5]}];
three = MapThread[{#, 3 #}&, {Range[0, 5]}];

ListLinePlot[
  {
    Legended[ Symbol["one"],   "one"   ],
    Legended[ Symbol["two"],   "two"   ],
    Legended[ Symbol["three"], "three" ]
  }
]

I'm looking for a way to plot the same in a loop:

ListLinePlot[
  MapThread[Legended[ Symbol[#], # ]&, {"one", "two", "three"}]
]

However, this does not work.

$\endgroup$
  • 1
    $\begingroup$ Just use Map instead of MapThread.: ListLinePlot[Map[Legended[Symbol[#], #] &, {"one", "two", "three"}]]. $\endgroup$ – Henrik Schumacher Oct 25 '18 at 6:25
  • $\begingroup$ Btw., Map also suffices for constructing the data: one = Map[{#, 1 #} &, Range[0, 5]]; two = Map[{#, 2 #} &, Range[0, 5]]; three = Map[{#, 3 #} &, Range[0, 5]];. $\endgroup$ – Henrik Schumacher Oct 25 '18 at 6:26
  • $\begingroup$ @HenrikSchumacher: Indeed! $\endgroup$ – Adobe Oct 25 '18 at 6:30
  • $\begingroup$ @HenrikSchumacher, can you make it an answer so I can accept it? $\endgroup$ – Adobe Oct 25 '18 at 7:43
4
$\begingroup$

Turning a comment into an answer.

Just use Map instead of MapThread:

ListLinePlot[Map[Legended[Symbol[#], #] &, {"one", "two", "three"}]]

Indeed, Map also suffices for constructing the data:

one = Map[{#, 1 #} &, Range[0, 5]]; 
two = Map[{#, 2 #} &, Range[0, 5]]; 
three = Map[{#, 3 #} &, Range[0, 5]];

MapThread is meant only for functions in several variables that are supposed to be fed from arguments stored in several lists. Of course, you can also use MapThread, but you would have to add another pair of braces in order to match MapThread's syntax:

ListLinePlot[MapThread[Legended[Symbol[#], #] &, {{"one", "two", "three"}}]]
$\endgroup$
0
$\begingroup$

How about this:

data = Table[{#, i #} & /@ Range[0, 5], {i, 3}];

ListLinePlot[data, PlotLegends -> {"one", "two", "three"}]

figure plotted

$\endgroup$
  • $\begingroup$ The data points in the question are not real. The real ones are much more complex. The crucial thing remains the same: to generate Legended invocations. PlotLegends are much more complex as well (involving string interpolation). $\endgroup$ – Adobe Oct 25 '18 at 6:17
  • $\begingroup$ @Adobe So you need to provide a small part of the real data, yet keeping the full data structure. $\endgroup$ – Αλέξανδρος Ζεγγ Oct 25 '18 at 6:59

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.