I have 2D data $f(x,y)$ and would like to get a plot along either of the axes, i.e. $f(x_0,y)$ or $f(x,y_0)$.

twoDdata = Table[{x, y, Cos[y ] Sin[x]}, {x, 0, 2 \[Pi], \[Pi]/20}, {y, 0, 
2 \[Pi], \[Pi]/20}];
ListDensityPlot[Flatten[twoDdata, 1], ColorFunction -> "Rainbow", PlotLegends -> Automatic]

enter image description here

what is the best way to get a line plot along the black and brown lines in the Fig above?

here is my idea for the brown one

ListLinePlot[twoDdata[[30]][[All, {2, 3}]]]    

enter image description here

How can we do it similarly but along the black line? Are there more elegant ways to do that?


2 Answers 2


Or use the 3D version.

twoDdata = 
  Table[{x, y, Cos[y] Sin[x]}, {x, 0, 2 π, π/20}, {y, 0, 
    2 π, π/20}];
c = 3.5; 
ListPlot3D[Flatten[twoDdata, 1], ColorFunction -> "Rainbow", 
  PlotLegends -> Automatic, MeshFunctions -> {#2 &}, Mesh -> {{c}}, 
  MeshStyle -> Thick, ViewPoint -> Front, PlotStyle -> None, 
  BoundaryStyle -> None, ViewProjection -> "Orthographic", 
  AxesEdge -> {Automatic, None, Automatic}, 
  PerformanceGoal -> "Quality"] /. {(VertexColors -> None) -> 
   VertexColors -> Automatic}

enter image description here


How can we do it similarly but along the black line?

I think this does it

twoDdata[[All, 20]][[All, {1, 3}]];

Mathematica graphics

I picked the 20'th column by inspection as there are 41 columns and this is about half way. If you have specific value for the y coordinates this can be improved.

This is how rowDdata is structured: First row contains all entries related for first $x$ value. Second row contains all entries related to second $x$ value, and so on.

Each row of rowDdata in turn is matrix whose each row is for the corresponding $x,y,f(x,y)$ value for that fixed $x$ and changing $y$.

So to pick the samples for the 20's y-value, that will be twoDdata[[All, 20]]. This gives a matrix with 3 entries in each row and has as many entries are there are $x$ values. Then twoDdata[[All, 20]][[All, {1, 3}]] picks the $x$ and $f(x,y)$ values from that matrix.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.