4
$\begingroup$

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]

enter image description here

$\endgroup$
  • $\begingroup$ What is your question? $\endgroup$ – Szabolcs Sep 26 '16 at 14:15
  • 3
    $\begingroup$ @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} ] $\endgroup$ – N.J.Evans Sep 26 '16 at 14:21
  • 1
    $\begingroup$ If you insist on the same form for p1Line and p2Line, p1Line={#1,0}&@@@data, and p2Line=data. $\endgroup$ – N.J.Evans Sep 26 '16 at 14:25
  • 1
    $\begingroup$ @N.J.Evans Sorry to steal your answer. What I did is valid or not in the community? $\endgroup$ – LCarvalho Sep 26 '16 at 14:33
  • 1
    $\begingroup$ @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. $\endgroup$ – N.J.Evans Sep 26 '16 at 15:09
6
$\begingroup$

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]

enter image description here

$\endgroup$
  • $\begingroup$ I will try this $\endgroup$ – JPeter Sep 26 '16 at 14:29
  • $\begingroup$ This is the answer that most closely approximates what I need, but it is not the best explained. $\endgroup$ – JPeter Sep 26 '16 at 14:49
5
$\begingroup$

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}]
$\endgroup$
  • $\begingroup$ Map and Apply are exactly the commands that don't work as well. $\endgroup$ – JPeter Sep 26 '16 at 14:45
  • $\begingroup$ I used them in my solutions in the operatorform. /@ and @@@ $\endgroup$ – Julien Kluge Sep 26 '16 at 15:27
5
$\begingroup$

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:

enter image description here

Have fun!

$\endgroup$
  • $\begingroup$ @AlexiBoulbitch +1 Great use of Filling! $\endgroup$ – Jack LaVigne Sep 27 '16 at 12:01
5
$\begingroup$

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

Mathematica graphics

$\endgroup$

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.