2
$\begingroup$

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?

$\endgroup$

2 Answers 2

4
$\begingroup$

Or use the 3D version.

Clear[c];
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

$\endgroup$
2
$\begingroup$

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

I think this does it

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

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.

$\endgroup$
0

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.