Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

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.

share|improve this question
1  
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. –  Joe Nov 16 '13 at 2:50
1  
Please clarify what you mean by "independent" variable. What does you original data look like? –  Yves Klett Nov 16 '13 at 7:16

3 Answers 3

up vote 2 down vote accepted

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.

share|improve this answer
    
Thank you! Your programming skills far exceed mine. I was hoping mathematica had a built-in easy way to do it. –  Joe Nov 16 '13 at 17:47

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[Composition[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.

share|improve this answer

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

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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