11
$\begingroup$

Is there a better way to enter my data into a list plot?

ListPLot[{{{1.1, 2.2}, {2.3, 4.3}, {2.9, 5.5}}, 
         {{1.1, 2.1}, {2.3, 4.6}, {2.9, 5.9}}, 
         {{1.1, 2}, {2.3, 4.8}, {2.9, 6.2}}}]

I think there should be a more efficient way, but cannot find it. I've read the documentation on ListPlot and DataRange.

$\endgroup$
  • 1
    $\begingroup$ By more efficient, I mean without typing the values of the independent variables more than once. For large data sets, this would be a waste of time. $\endgroup$ – Joe Nov 16 '13 at 2:50
  • 1
    $\begingroup$ Please clarify what you mean by "independent" variable. What does you original data look like? $\endgroup$ – Yves Klett Nov 16 '13 at 7:16
11
$\begingroup$
times = {1.1, 2.3, 2.9}; (* list of x values *)
states = {{2.2, 4.3, 5.5}, {2.1, 4.6, 5.9}, {2, 4.8, 6.2}}; (* lists of y values *)

td = TemporalData[states, {times}];
ListPlot[td, BaseStyle -> PointSize[.02], PlotRange -> {0, 7}]

enter image description here

$\endgroup$
  • $\begingroup$ That's terrific! Thank you for introducing me to TemporalData. That's exactly the type of functionality that I was hoping existed in Mathematica. $\endgroup$ – Joe Nov 10 '14 at 1:15
  • $\begingroup$ Joe, my pleasure. Thank you for the Accept. $\endgroup$ – kglr Nov 12 '14 at 1:14
  • $\begingroup$ Joe, please read the FAQs. Also when you see good questions and answers you can vote them up by clicking the gray triangles. Btw, it is expected that you would upvote an answer that you find worthy of Accept ;) $\endgroup$ – kglr Nov 12 '14 at 1:19
  • $\begingroup$ Thank you for letting me know what those triangle did. I had no idea. $\endgroup$ – Joe Nov 13 '14 at 12:42
11
$\begingroup$

A different approach, following J.W. Perry's structure,

ivar = {1.1, 2.3, 2.9};
dvars = {{2.2, 4.3, 5.5}, {2.1, 4.6, 5.9}, {2, 4.8, 6.2}}

data = Inner[List, ivar, #, List] & /@ dvars

Or, generalized (any number of dimensions),

data = MapThread[Flatten @* List, {ivar, #}] & /@ dvars

It might seem convoluted at first, but once you get acquainted with high level operators (Inner, Outer, MapThread...), "For" loops will look too verbose.

$\endgroup$
5
$\begingroup$

If all what you want is to plot the data, then simply leave it as is, and use Show to combine the plots.

ivar = {1.1, 2.3, 2.9};
dvars = {{2.2, 4.3, 5.5}, {2.1, 4.6, 5.9}, {2, 4.8, 6.2}};
Show[ ListPlot[{ivar, #}] & /@ dvars ]

or to get more control of final layout, add more options to Show

Show[ListPlot[{ivar, #}] & /@ dvars, PlotRange -> All, AxesOrigin -> {0, 0}]

Mathematica graphics

$\endgroup$
3
$\begingroup$

Here I have assigned ivar to be your independent variables. You can add as many triplets as you want to dvars in this code and build the set data:

ivar = {1.1, 2.3, 2.9}; (*your independent variable values*)
dvars = {{2.2, 4.3, 5.5}, {2.1, 4.6, 5.9}, {2, 4.8, 6.2}}; (*dependent variables*)
data = {}; (*your data set*)
For[i = 1, i <= Length[dvars], i++,
 AppendTo[data, 
  Table[{ivar[[j]], dvars[[i]][[j]]}, {j, 1, Length[ivar]}]
  ]
 ]

You can view data of course, and you can produce your output with

ListPlot[data]

This will alleviate the repeated typing of ivars for your larger data set. Judicious modification of the Length[ ] function should make this code scalable for different tuple sets.

$\endgroup$
  • $\begingroup$ Thank you! Your programming skills far exceed mine. I was hoping mathematica had a built-in easy way to do it. $\endgroup$ – Joe Nov 16 '13 at 17:47

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.