Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

matlab like multiline plot in Mathematica

So I have a matrix of values were the rows correspond to sets of data, and I want to plot each of these using ListPlot but I want to have a different x-axis than the index. In matlab I would have:

x = 0:4;

ys = [10 20 40 80 160; 
      20 40 80 160 320; 
      30 60 120 240 480]';

plot(x,ys)

and this would give three lines with x-values 0-4 and the y-values being each column.

The closest I could come up with in Mathematica is

x = Range[0,4];

ys = {{10, 20, 40, 80, 160},
      {20, 40, 80, 160, 320},
      {30, 60, 120, 240, 480}};

ListPlot[Transpose[{x,#}]& /@ ys]

Mathematica graphics

Is this the correct way? It seems a bit cryptic. Hoping there is a function or option I am missing.

like image 790
Gabriel Avatar asked Mar 23 '11 22:03

Gabriel


1 Answers

In your particular case, since the points are equidistant, you can use

ListPlot[ys, DataRange -> x[[{1,-1}]]]

Hope this is less cryptic. You can of course also use the range values directly:

ListPlot[ys, DataRange -> {0, 4}]
like image 138
Leonid Shifrin Avatar answered Sep 21 '22 23:09

Leonid Shifrin