# How convert list of numbers to list of points on x-axis? [duplicate]

I have a list of real numbers, say:

  vec = RandomInteger[{1, 20}, 6]
(* {2, 4, 7, 10, 13, 6} *)


I want to convert each of these numbers to the corresponding point on the x-axis, that is, to obtain:

  {{2, 0}, {4, 0}, {7, 0}, {10, 0}, {13, 0}, {6, 0}}


The purpose of doing this is to include these points, among other graphics objects, in the plot of a function.

Here are some ways to do it:

  Partition[Riffle[vec, 0], 2]
(Append[#, 0] &) /@ Partition[vec, 1]
PadRight[#, 2] & /@ Partition[vec, 1]
vec /. x_?NumericQ -> {x, 0}


What is an especially simple way to do this that will be readily understandable by somebody relatively new to Mathematica -- especially somebody who may still be uncomfortable with employing pure functions, functional approaches, and pattern-matching?

• Related: (7996), (45980) Jun 21, 2015 at 10:40
• I see two reopen votes on this question yet the Accepted answer shows exactly the same methods as the answers in the original. The question itself is also identical except for an updated emphasis on a "simple way" but since the "simple way" is already shown in the original this does not differentiate it. Jun 21, 2015 at 21:09

vec = {2, 4, 7, 10, 13, 6};

{#, 0} & /@ vec


{{2, 0}, {4, 0}, {7, 0}, {10, 0}, {13, 0}, {6, 0}}

Thread[{vec, 0}]


{{2, 0}, {4, 0}, {7, 0}, {10, 0}, {13, 0}, {6, 0}}

• I especially like Thread[{vec,0}] because it's not only short, but also avoids use of pure functions. Jun 20, 2015 at 15:14
• Very much out-of-the-box approach with Thread +1 Jun 20, 2015 at 16:14
• +1. Thread certainly is a beautiful solution. Nevertheless the performance advantage of a pure function seems hard to beat?
– gwr
Jun 20, 2015 at 17:34
• @gwr I doubt that Thread will be slower. As an in-built function, it should be quite efficient (but you are very welcome to benchmark). Jun 20, 2015 at 18:57
• I did with 1 Mio random numbers in the vector and the pure function was faster by around 25 % though in absolute duration that did maybe not matter that much (around .13 secs for my machine for the Thread-sokution).
– gwr
Jun 20, 2015 at 21:27

There's no need to convert points on the real line to points in the Cartesian plane.

Simply use NumberLinePlot with Spacings-> 0

NumberLinePlot[{2, 4, 7, 10, 13, 6}, Spacings -> 0]


• I'm afraid using NumberLinePlot doesn't solve the problem, as the points on the x-axis I want to form are going to go into the Epilog of a Plot expression. And it's not obvious how to combine a NumberLinePlot with a Plot by means of Show, even though both have head Graphics. Jun 21, 2015 at 15:35
• You may easily combine Plot and NumberLinePlot. For example, Show[{ Plot[2 Sin[x] + x, {x, 0, 15}], NumberLinePlot[{2, 4, 7, 10, 13, 6}, Spacings -> 0]}] Jun 21, 2015 at 15:41
• you're right, I'm wrong. My test with Show had a typo! Jun 21, 2015 at 16:25

 SeedRandom[1]; {#, 0} & /@ RandomInteger[{1, 20}, 6]

{{6, 0}, {1, 0}, {8, 0}, {1, 0}, {3, 0}, {4, 0}}

• Sorry I couldn't accept two answers: your use of {#,0}&/@ came first. But @Bob Hanlon's answer included also the so-nice use of Thread. Jun 20, 2015 at 16:49

If the starting point is vec one approach would be

Transpose[{vec, ConstantArray[0, Length[vec]]}]

• Even though this approach is hardly the tersest, it is very easy to understand, even by a beginner: no pure functions, no use of Map, no other functional construct such as Thread. (Having grown up programming in APL, this way should have occurred to me.) Jun 21, 2015 at 15:38

There are of course more compact solutions but I like the readability of Function avoiding the slot notations:

f = Function[ x, {x, 0}, Listable ];

f @ { 1, 2, 3, 4, 5, 6}


{{1, 0}, {2, 0}, {3, 0}, {4, 0}, {5, 0}, {6, 0}}

A thing to note may be that using g instead of f

g = {#, 0}& /@ #&


eventually is much faster and also beats many other solutions.

• Actually, efficiency was not among my concerns; brevity and, especially, transparency to those relatively new to Mathematica were. (Perhaps I should have said that.) Jun 20, 2015 at 18:44
• That is why I sometimes go for the named function as it is so close to mathematics. While beatifully compact I do find other solutions quite unfortunate with regard to transparency and ease of understanding.
– gwr
Jun 20, 2015 at 21:31