1
$\begingroup$

I have the following example:

x = {1, 2, 3, 4}
y = {{1, 2, 3, 4}, {2, 3, 4, 5}, {3, 4, 5, 6}, {4, 5, 6, 7}}

Now I can plot:

ListPlot[{Transpose[{x, y[[1]]}], Transpose[{x, y[[2]]}], 
  Transpose[{x, y[[3]]}], Transpose[{x, y[[4]]}]}]

enter image description here

How can this be simplified?

$\endgroup$
  • 1
    $\begingroup$ Have you tried Map to avoid typing the same thing four times?? $\endgroup$ – Szabolcs May 27 '16 at 13:32
  • $\begingroup$ Map[(Transpose[{x, #}]) &, y] ? $\endgroup$ – mrz May 27 '16 at 13:32
  • 1
    $\begingroup$ Yes, or just Transpose[{x,#}]& /@ y if you find that more readable. $\endgroup$ – Szabolcs May 27 '16 at 13:33
  • 1
    $\begingroup$ With your x use the default: ListPlot[y] $\endgroup$ – Bob Hanlon May 27 '16 at 17:27
  • $\begingroup$ @Bob Hanson: For my analysis in general x is not a list of of consecutive integers. $\endgroup$ – mrz May 28 '16 at 7:23
2
$\begingroup$

If you want to simplify generating the input these are simpler ways:

x = Range[4];
y = Partition[ Range[7], 4, 1];

then you can use Inner:

z = Inner[List, x, y, List];

and finally

ListPlot[z] 

Instead of playing with Partition and Range you can generate the input of ListPlot with Table:

z = Table[{i, i + k - 1}, {k, 4}, {i, 4}];

or even better with Array:

z = Array[{#2, #2 + #1 - 1} &, {4, 4}]

The simplest approach would make use of the Front - end shorthands for Transpose (esc tr esc) (see e.g. Add a vector to a list of vectors) and Map (/@) e.g.

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.