I have a table of dimensions {10,3} say. Each column is the value of some function f(x,a) wrt to x and the parameter $a~ ( =1,2,3$ say) refers to the columns. So for example if we let

$f(x,a) = a \sin(x)$ then the first column of my matrix contains values $$1\cdot \sin(x), (\text{where }x=1,...10)$$ second column $$2\cdot \sin(x)$$

and so on. Obviously one can plot column 1 vs x and so on for the other columns. But is there a simple way to plot each column vs x in one and the same graph?


2 Answers 2

data = Table[a Sin[x], {x, 1, 10}, {a, 1, 3}]

ListPlot[Transpose@data] is what you need:

Show[Plot[Table[a Sin[x], {a, 1, 3}], {x, 1, 10}], 
 ListPlot[Transpose@data, PlotStyle -> PointSize[Large]], 
 Frame -> True, Axes -> {True, False}]

enter image description here

  • $\begingroup$ Awesome, very simple and clean! $\endgroup$ Commented Oct 20, 2016 at 14:23
f[x_, a_] = a Sin[x];
avals = {1, 2, 3};
xvals = N@Subdivide[0, \[Pi], 9];
mat = Outer[N@*f, xvals, avals];
(*returns {10,3}*)

as exampledata (please provide that next time in your question). You can "pair" each of your columns with the corresponding xvalues by "threading" the column and the xvalues with the function Thread.

Thread[{xvals, mat[[All,1]]}]
(*returns {{0, 0.}, {\[Pi]/10, 0.309017},...}*)

Do this for each column separately (here we transpose the table first so our columns become rows).

Thread[{xvals, #}] & /@ Transpose[mat]

and finally, wrap a ListLinePlot or ListPlot around it

ListLinePlot[Thread[{xvals, #}] & /@ Transpose[mat]]

enter image description here
I hope that's what you wanted. If not, let me know.



Your Answer

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

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