0
$\begingroup$

I want to plot 4 separate lines on my graph but for some reason Mathematica is linking all my points into a single lines.

My data:

v = {3.26797, 4.07436, 5.12821, 5.42005};
m = {0.004, 0.00592, 0.00836, 0.01060};

I want a straight line from the origin {0,0} to the point {0.004,3.26797}, {0,0} to the point {0.00592,4.07436}, ..., {0,0} to the point {m[[4]], v[[4]]}.

I tried the code:

ListLinePlot[{{0.004, 10.6797}, {0.00592, 16.6004}, {0.00836, 26.2985},
  {0.01060, 29.3769}}]

but all I am getting is a single line.

$\endgroup$
2
$\begingroup$
v = {3.26797, 4.07436, 5.12821, 5.42005}; 
m = {0.004, 0.00592,0.00836, 0.01060};

 ListLinePlot[Table[{{0, 0}, {m[[i]], v[[i]]}}, {i, 1, 4}],Frame -> True]

enter image description here

$\endgroup$
  • $\begingroup$ Hi Hubble, how would I label each lines? You don't necessarily have to give me the code (although you could and I would study them). Pointing me to a right direction is good (: $\endgroup$ – boyinneed Mar 18 '15 at 12:50
  • $\begingroup$ The easiest way is to use PlotLegend. reference.wolfram.com/language/PlotLegends/ref/PlotLegend.html $\endgroup$ – Hubble07 Mar 18 '15 at 12:55
  • $\begingroup$ I did it =p Thank you! And if I would like to label both axes, it's with the function AxesLabel? $\endgroup$ – boyinneed Mar 18 '15 at 13:01
  • $\begingroup$ I made an edit. $\endgroup$ – boyinneed Mar 18 '15 at 13:09
2
$\begingroup$
v = {3.26797, 4.07436, 5.12821, 5.42005};
m = {0.004, 0.00592, 0.00836, 0.01060};

ListLinePlot[{{0, 0}, #} & /@ Transpose[{m, v}]]

enter image description here

$\endgroup$
2
$\begingroup$

I think I'd use MapThread here, instead of either Table or Map plus Transpose. Table has two disadvantages: each element must be accessed by index and the length of the data must be determined. Map, on the other hand, cannot do the job by itself and requires Transpose or Thread. None are great tasks, but represent extra work. MapThread is conceptually simpler:

MapThread[{{0, 0}, {##}}&, {m, v}]
(*
{{{0, 0}, {0.004, 3.26797}}, {{0, 0}, {0.00592, 4.07436}}, 
 {{0, 0}, {0.00836, 5.12821}}, {{0, 0}, {0.0106, 5.42005}}}
*)

That said, Table is the simplest if the data sets are of unequal length:

Table[{{0,0}, {m[[i]], v[[i]]}}, {i, Min[Length /@ {m,v}]}]
$\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.