4

I have a table of data with multiple rows and columns, and I have to make a versus data table using two of these columns. Essentially, I have to set one column of data (column 3) as the y-axis and another column of data (column 5) as the x-axis. How do I accomplish this?

dearN
  • 5,341
  • 3
  • 35
  • 74
Taylor
  • 51
  • 1
  • 1
  • 3

4 Answers4

14

Assuming the name of the table is data

data =
  {
   {1, 2, 3, 4, 5, 6},
   {2, 3, 4, 5, 6, 7},
   {3, 4, 5, 6, 7, 8},
   {4, 5, 6, 7, 8, 9}
   };

The 3rd column would be data[[All,3]] and the 5th data[[All,3]]. All means: take all elements of the given index. Since we need to plot x against y we need a submatrix containing the 3rd and 5th column. Data[[All,{5,3}]] does that.

data[[All, {5, 3}]]

(*
==> {{5, 3}, {6, 4}, {7, 5}, {8, 6}}
*)

There are many plot functions in Mathematica. In this case, ListPlot or ListLinePlot are appropriate.

ListPlot[data[[All,{5,3}]]]

Mathematica graphics

ListLinePlot[data[[All,{5,3}]]]

Mathematica graphics

There are many, many options to tune those plots. I suggest looking up those in the extensive electronic documentation in the Help menu or by selecting a command and pressing F1 (Windows)

Things to look up:

Sjoerd C. de Vries
  • 65,815
  • 14
  • 188
  • 323
11

You could do something like

ListLinePlot[data[[All, {5, 3}]]]
Heike
  • 35,858
  • 3
  • 108
  • 157
11

Short version

Use Part.

Longer version

Let's create a dataset consisting of $x$, $x^2$, $\sin(x)$ and $\exp(x)$.

data = Table[{x, x^2, Sin[x], Exp[x]}, {x, 0, 10, .1}]
{{0., 0., 0., 1.}, <<99>>, {10., 100., -0.544021, 22026.5}}

Now let's plot $\exp(x)$ over $x^2$.

The intuitive way of doing this is extracting two separate lists from the dataset, namely the second and fourth columns, by using data[[All, 2]] (or 4, respectively), combining them into a new list, which then consists of {{x^2 data}, {exp data}}. This has then to be transposed to yield a list of pairs of numbers, which can then be plotted:

ListPlot[{data[[All, 2]], data[[All, 4]]} // Transpose]

A simpler solution however uses the rich syntax of the Part function. You usually don't see Part written explicitly, as it is abbreviated using [[ foo ]]. Here's how this version works:

ListPlot[data[[All, {2, 4}]]]

The output of both of these is exactly the same:

enter image description here

A value table for this can be displayed in a similar way, using Grid:

data = Table[{x, x^2, Sin[x], Exp[x]}, {x, 0., 10}];
Grid[data[[All, {2, 4}]], Dividers->{All, None}]

enter image description here

David
  • 14,911
  • 6
  • 51
  • 81
9

It hasn't been mentioned yet, so: the Statistical Plots package features the function PairwiseScatterPlot[], which allows you to compare your data by columns all at once. (If you only need to do two, the other solutions have already given the recipe.)

Here's a sample:

<< StatisticalPlots`

data = Table[{x, x^2, ArcTan[x], Exp[x]} + RandomReal[]/100, {x, 0, 10}];

PairwiseScatterPlot[data, DataLabels ->
                    Map[TraditionalForm, {x, x^2, ArcTan[x], Exp[x]}]]

pairwise scatter plot example

J. M.'s missing motivation
  • 124,525
  • 11
  • 401
  • 574