2
$\begingroup$

Suppose that I have a following data:

tab = {{1,2,-3,5}, {5,-2,3,1}, {2,0,4,5}};

I want to make a ListPlot based on tab as follows:

  1. Focusing on the first and the second column, plot (1,2), (5,-2), (2,0).
  2. Focusing on the first and the third column, plot (1,-3), (5,3), (2,4).
  3. Focusing on the first and the last column, plot (1,5), (5,1), (2,5).

I want to make the colors in three groups 1., 2., and 3. different. How can I achieve this? (Of course, this is a simple example and my real data has a large number of points, but the number of column can be handled by hand.)

$\endgroup$
5
  • 3
    $\begingroup$ You can access data by 1. tab[[All , {1, 2}]] , 2. tab[[All , {1, 3}]] 3. tab[[All , {1, -1}]]. If you plot them all together Mathematica will assign different colors, if not use PlotStyle. $\endgroup$
    – Ben Izd
    May 30 at 7:53
  • $\begingroup$ @BenIzd This perfectly answers my question. Thank you so much! $\endgroup$
    – eigenvalue
    May 30 at 7:58
  • $\begingroup$ @Nasser The questions is already answered, but my intention was that if there are $n$ columns, then the first and the $i$-th columns for each $2\leq i \leq n$ are used. In my application, $n=6$. $\endgroup$
    – eigenvalue
    May 30 at 7:59
  • 1
    $\begingroup$ A related question. $\endgroup$ May 30 at 8:01
  • 1
    $\begingroup$ @eigenvalue Your welcome. Also, remember for the last point, I use -1, which depends on the number of columns. $\endgroup$
    – Ben Izd
    May 30 at 8:02

1 Answer 1

3
$\begingroup$

Let's say your data looks like this

data = Transpose@Join[{0.1*Range[10]},Sort/@RandomReal[1,{10,10}]];
TableForm@data

enter image description here

You can ListPlot all columns except the first against the first column, without specifying the number of columns, like this

With[
    {n=Length[First@data]},
    ListLinePlot[
        Table[
            data[[All,{1,j}]]
            ,{j,2,n}
        ]
        , PlotStyle  -> Array[Hue,n,{0,0.8}]
        , PlotLegends-> Table[StringTemplate["`` vs ``"][j,1],{j,2,n}]
    ]
]

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.