14
$\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$
3
  • 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, 2013 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, 2013 at 7:16
  • $\begingroup$ By “independent variables”, I meant the abscissa. I’m not sure about elsewhere, but in the U.S. that’s usually referred to as the independent variable or even simply $x$. I know I’ve had people from other countries ask me what I mean by $x$-axis when there was no $x$ defined in the question, but in the U.S. the values of the abscissa are usually referred to as the “independent variable” or simply $x$. $\endgroup$
    – Joe
    Dec 7, 2019 at 12:52

4 Answers 4

14
$\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$
3
  • $\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, 2014 at 1:15
  • $\begingroup$ Joe, my pleasure. Thank you for the Accept. $\endgroup$
    – kglr
    Nov 12, 2014 at 1:14
  • $\begingroup$ Thank you for letting me know what those triangle did. I had no idea. $\endgroup$
    – Joe
    Nov 13, 2014 at 12:42
12
$\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$
1
  • $\begingroup$ Does this work if the number of y values per x value are different for different x? If not, can it be generalized? $\endgroup$
    – Kvothe
    Jul 18 at 14:01
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$
4
$\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$
1
  • $\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, 2013 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.