Message Boards Message Boards

Plot a simple x vs. y plot from a table

I have a table that I imported from a csv file with 15 columns of hippocampal subregion volumes and some memory tests. For some reason, mathematica does not make it easy to make a simple x vs. y plot. Any Ideas how I could simply refer to two colums that I want plotted against each other?

Extra:
I would really like to use headers in the csv, but I had to take them out because mathematica cannot handle them without what seems like a lot of extra work. Any advice?

I should probably just go back to r....

Thank you all in advance for any help!
POSTED BY: trodriguez2011
Here is a simple example. Given table of 3 rows and 4 columns, suppose you want to plot the second column against the 4th column.

Method 1

tbl = { {1, 2, 3, 4},
        {2, 5, 7, 8},
        {9, 10, 11, 12}};
x = tbl[[All, 2]];
y = tbl[[All, 4]];
data = Transpose[{x, y}]
Now your data is
{{2, 4},
{5, 8},
{10, 12}}
Now you can plot it as
ListLinePlot[data, Mesh -> All, AxesOrigin -> {0, 0}]

Method 2

You can select the columns and plot directly
tbl = {{1, 2, 3, 4}, {2, 5, 7, 8}, {9, 10, 11, 12}};
data = tbl[[ All, {2, 4} ]]
ListLinePlot[data, Mesh -> All, AxesOrigin -> {0, 0}]

bypassing headers

Now if your table had header in it, you do not need to remove these. Simply start from the second row, instead of the first row, like this:
tbl = {{"h1", "h2", "h3", "h4"}, {1, 2, 3, 4},{2, 5, 7, 8},{9, 10, 11, 12}};
data = tbl[[ 2 ;;, {2, 4} ]]
ListLinePlot[data, Mesh -> All, AxesOrigin -> {0, 0}]
POSTED BY: Nasser M. Abbasi
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract