# ListPlot multiple datasets over same x-values

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.

• 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
Commented Nov 16, 2013 at 2:50
• Please clarify what you mean by "independent" variable. What does you original data look like? Commented Nov 16, 2013 at 7:16
• 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$.
– Joe
Commented Dec 7, 2019 at 12:52

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}]


• That's terrific! Thank you for introducing me to TemporalData. That's exactly the type of functionality that I was hoping existed in Mathematica.
– Joe
Commented Nov 10, 2014 at 1:15
• Joe, my pleasure. Thank you for the Accept.
– kglr
Commented Nov 12, 2014 at 1:14
• Thank you for letting me know what those triangle did. I had no idea.
– Joe
Commented Nov 13, 2014 at 12:42

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.

• Does this work if the number of y values per x value are different for different x? If not, can it be generalized? Commented Jul 18, 2022 at 14:01

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}]


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.

• Thank you! Your programming skills far exceed mine. I was hoping mathematica had a built-in easy way to do it.
– Joe
Commented Nov 16, 2013 at 17:47