Group Abstract Group Abstract

Message Boards Message Boards

0
|
5.2K Views
|
6 Replies
|
0 Total Likes
View groups...
Share
Share this post:

2D plot of a cut through a 3D table?

Posted 10 years ago

I'm a complete Mathematica noob (but experienced in many other languages, Java, C++, etc.), picking up someone else's code and extending it. I have a table that looks like this:

{x1, y1, z11}, {x1, y2, z12}...{x1, yn, z1n}

{x2, y1, z21}, {x2, y2, z22}...{x2, yn, z2n}

. . .

I want to make 2D plots of z as a function of y, holding x constant. This has to be simple, but I can't find it in the on-line documentation.

Thank you for your help!

POSTED BY: Bob Loushin
6 Replies
Posted 10 years ago

Thank you! I learned a lot from your answers, and I've got it running now. I like the way Gianluca's answer generalizes to n dimensions.

POSTED BY: Bob Loushin

A different approach, using Cases and replacement rules, let you choose more easily which variable to keep constant:

data = Table[{x, y, x^2 + y^2}, {x, -1, 1, 1/5}, {y, -1, 1, 1/5}];
ListPlot[Cases[data, {4/5, y_, z_} :> {y, z}, All]]
ListPlot[Cases[data, {x_, 2/5, z_} :> {x, z}, All]]

Cases chooses the triples with given x or y value, and the replacement rules turns them into couples.

POSTED BY: Gianluca Gorni
Posted 10 years ago

For your constant y plots look up All in the documentation.

For your [[]] question look up Part in the documentation.

POSTED BY: Bill Simpson
Posted 10 years ago

Thank you! That worked! But also want to plot vs. x, holding y constant. I didn't ask that, because I had expected the answer for y to generalize, but if it does, I don't see how.

BTW, why are there double brackets "[[]]" around the k? I thought functions used single brackets? Knowing this may answer my first question...

POSTED BY: Bob Loushin

You can map the first row this way:

data = Table[{x, y, x^2 + y^2}, {x, -1, 1, 1/5}, {y, -1, 1, 1/5}];
ListPlot[Map[Rest, data[[1]]]]

For the k-th row, just replace data[[1]] with data[[k]].

POSTED BY: Gianluca Gorni
Posted 10 years ago

Here is an example table

In[1]:= data = Table[{x, y, x*y^2}, {x, 1, 3}, {y, 1, 4}]

Out[1]= {{{1, 1, 1}, {1, 2, 4}, {1, 3, 9}, {1, 4, 16}},
         {{2, 1, 2}, {2, 2, 8}, {2, 3, 18}, {2, 4, 32}},
         {{3, 1, 3}, {3, 2, 12}, {3, 3, 27}, {3, 4, 48}}}

Suppose you are interested in the first row. You want to "do the same thing" to each item in that row, get rid of the first number. Map is almost always the answer when you want to "do the same thing" to every item in a list. Rest drops the first item in a list and gives you the rest of the items. So "do" Rest to every list in the row.

In[2]:= Map[Rest, data[[1]]]

Out[2]= {{1, 1}, {2, 4}, {3, 9}, {4, 16}}

And plot that

ListPlot[Map[Rest, data[[1]]]]
POSTED BY: Bill Simpson
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard