# Creating lines in graphics

It is a question very easy, but I am confused how to use the Table or Riffle or Partition to achieve this result.

data = {{0, 0}, {228.9, 0.06}, {313.7, 0.10}, {340.6, 0.14}, {355.1,
0.18}, {368.2, 0.22}};
lPlot = ListPlot[data, Joined -> True];
pt = Graphics[{Red, PointSize[0.02],
Point[Table[data[[#]] &[Range[Length[data]]], 1]]}];
lines = Graphics[{Dashed, Line[{{0, 0}, {0, 0}}],
Line[{{228.9, 0}, {228.9, 0.06}}],
Line[{{313.7, 0}, {313.7, 0.1}}],
Line[{{340.6, 0}, {340.6, 0.14}}],
Line[{{355.1, 0}, {355.1, 0.18}}],
Line[{{368.2, 0}, {368.2, 0.22}}]}];
Show[lPlot, pt, lines]


I got two tables, but I found it difficult to use them

p1Line = Partition[
Riffle[Flatten@Table[data[[#, 1]] &[Range[Length[data]]], 1],
Table[0, Length[data]]], 2]
p2Line = Partition[
Flatten[Table[data[[#]] &[Range[Length[data]]], 1]], 2]


• What is your question? Commented Sep 26, 2016 at 14:15
• @JPeter You can make your plot using: ListPlot[ data , Joined -> True , PlotMarkers -> {Graphics[{Red, Point[{0, 0}]}]} , Epilog -> {Dashed, Line[{{#1, 0}, {#1, #2}}] & @@@ data} ] Commented Sep 26, 2016 at 14:21
• If you insist on the same form for p1Line and p2Line, p1Line={#1,0}&@@@data, and p2Line=data. Commented Sep 26, 2016 at 14:25
• @N.J.Evans Sorry to steal your answer. What I did is valid or not in the community? Commented Sep 26, 2016 at 14:33
• @LMC I'm not sure about everyone else, but it doesn't bother me at all. I didn't have the time to write up a full answer and wasn't quite sure what OP was looking for. You might add some explanation to the answer though. Commented Sep 26, 2016 at 15:09

Based on the comments of N.J.Evans

data = {{0, 0}, {228.9, 0.06}, {313.7, 0.10}, {340.6, 0.14}, {355.1,
0.18}, {368.2, 0.22}};

lPlot = ListPlot[data, Joined -> True];

pt = Graphics[{Red, PointSize[0.02],
Point[Table[data[[#]] &[Range[Length[data]]], 1]]}];

lines = Graphics[{
Dashed, Line[{{#1, 0}, {#1, #2}}] & @@@ data}];

Show[lPlot, pt, lines]


• I will try this Commented Sep 26, 2016 at 14:29
• This is the answer that most closely approximates what I need, but it is not the best explained. Commented Sep 26, 2016 at 14:49

You may do it as follows:

data = {{0, 0}, {228.9, 0.06}, {313.7, 0.10}, {340.6, 0.14}, {355.1,
0.18}, {368.2, 0.22}};
lPlot = ListPlot[data, Joined -> True];
pt = Graphics[{Red, PointSize[0.02],
Point[Table[data[[#]] &[Range[Length[data]]], 1]]}];
lines = Graphics[{Dashed, Line /@ Transpose[{p1Line, p2Line}]}];
Show[lPlot, pt, lines]


Here Transpose[{p1Line, p2Line}] makes the job.

But you may do it in a shorter way:

    Show[{
ListPlot[data, PlotStyle -> Red, Filling -> Bottom,
FillingStyle -> Directive[Black, Dashed]],
ListPlot[data, PlotStyle -> Blue, Joined -> True]
}]


yielding this:

Have fun!

• @AlexiBoulbitch +1 Great use of Filling! Commented Sep 27, 2016 at 12:01

So at first I really suggest you read how to use Table. Play with it, look how the parameters work together.

If you want to use table (and see how it works) you can look at this code:

lines=Graphics[Join[{Dashed},Table[Line[{data[[i]],{data[[i,1]],0}}],{i,1,Length[data]}]]];
points=Graphics[Join[{Red,PointSize[0.02]},Table[Point[data[[i]]],{i,1,Length[data]}]]];
lPlot=ListPlot[data,Joined->True];
Show[lPlot,lines,points]


But i would use Operators to map directly to your data See Map, Apply which gives us:

lPlot=ListPlot[data,Joined->True];
Show[lPlot,Graphics[{Red,PointSize[0.02],Point/@data,Dashed,Line[{{#1,#2},{#1,0}}]&@@@data}]]


In the end you can put everything in ListPlot with the Epilog-Option instead of using Show with Graphics:

lPlot=ListPlot[data,Joined->True,Epilog->{Red,PointSize[0.02],Point/@data,Dashed,Line[{{#1,#2},{#1,0}}]&@@@data}]

• Map and Apply are exactly the commands that don't work as well. Commented Sep 26, 2016 at 14:45
• I used them in my solutions in the operatorform. /@ and @@@ Commented Sep 26, 2016 at 15:27

A couple of points.

The use of Table in the following construction from your question

pt = Graphics[{Red, PointSize[0.02],
Point[Table[data[[#]] &[Range[Length[data]]], 1]]}];


is not needed. You can just use data.

pt = Graphics[
{
Red,
PointSize[0.02],
Point[data]
}
]


To make up the lines

{
Line[{{0,     0}, {0,        0}}],
Line[{{228.9, 0}, {228.9, 0.06}}],
Line[{{313.7, 0}, {313.7,  0.1}}],
Line[{{340.6, 0}, {340.6, 0.14}}],
Line[{{355.1, 0}, {355.1, 0.18}}],
Line[{{368.2, 0}, {368.2, 0.22}}]
}


you can use

Map[Line[{{#[[1]], 0}, #}] &, data]


or if you prefer Table

Table[
Line[{{dataPt[[1]], 0}, dataPt}],
{dataPt, data}
]


It is a matter of choice whether you want to use a symbol name for these constructions and then use the symbol in Graphics or use the constructions directly. Here I will use it directly.

Show[
ListPlot[data, Joined -> True],
Graphics[
{
Dashed,
Map[Line[{{#[[1]], 0}, #}] &, data[[2 ;; -1]]],
Red,
PointSize[0.02],
Point[data]
}
]
]


Using Mesh and Projection:

data = {{0, 0}, {228.9, 0.06}, {313.7, 0.10}, {340.6, 0.14}, {355.1,
0.18}, {368.2, 0.22}};

ListLinePlot[data
, Mesh -> All
, MeshStyle -> Red
, Epilog -> {Dashed
, Line[{#, Projection[#, {1, 0}]}] & /@ data
}
]


Or the Graphics approach only.

Graphics[{
ColorData[97][1], Line@data
, Black, Dashed
, Line[{#, Projection[#, {1, 0}]}] & /@ data
, Red, AbsolutePointSize[6], Point@data
}
, AspectRatio -> 1/2 (* Can you plot without it? *)
, Axes -> True
]


Result: