0
$\begingroup$

This question already has an answer here:

l1={1,2,3,4}
l2={5,6,7,8}

I want to plot the l2 against l1. that is at value 1 on x axis, it should show value 5, 6 at value 2 and so on..how can i do that?

$\endgroup$

marked as duplicate by Kuba, J. M. is away Nov 2 '15 at 8:19

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • 1
    $\begingroup$ ListPlot[Transpose@{l1,l2}] $\endgroup$ – IPoiler Nov 2 '15 at 2:00
  • $\begingroup$ @IPoiler that's a legit answer; you should post it as one. $\endgroup$ – Pillsy Nov 2 '15 at 2:05
  • 1
    $\begingroup$ I'm quite sure this is a dupe. $\endgroup$ – J. M. is away Nov 2 '15 at 2:46
  • $\begingroup$ @J.M. yep, it is, maybe some additional key words are needed $\endgroup$ – Kuba Nov 2 '15 at 8:14
  • $\begingroup$ Now, that didn't show up in my searches. Thanks @Kuba! $\endgroup$ – J. M. is away Nov 2 '15 at 8:16
4
$\begingroup$

ListPlot can be used to plot lists of discrete points. The documentation notes that to specify both abscissa, as well as the ordinate, for your points then the supplied list must be of the form $\{\{x_1,y_1\},\dotsc,\{x_n,y_n\}\}$. So we can

{l1,l2}

to obtain the form $\{\{x_1,\dotsc,x_2\},\{y_1,\dotsc,y_n\}\}$, then transpose it to get the necessary form. In one shot,

l1={1,2,3,4}
l2={5,6,7,8}
ListPlot[Transpose@{l1,l2}]
$\endgroup$
1
$\begingroup$

To see why Transpose dose that job, you can run the following code:

l1 = {1, 2, 3, 4} ;
l2 = {5, 6, 7, 8};
{l1, l2} // MatrixForm
l3 = Transpose[{l1, l2}];
l3 // MatrixForm
ListPlot[l3, 
 Joined -> True](*Set to False if you want the discrete points*)
$\endgroup$
  • 1
    $\begingroup$ Joined->False is the default for ListPlot. It does no harm but is not needed. $\endgroup$ – Jack LaVigne Nov 2 '15 at 3:16
  • $\begingroup$ @JackLaVigne I just want to show OP an option. $\endgroup$ – 喵喵是我的猫猫 Nov 2 '15 at 3:41

Not the answer you're looking for? Browse other questions tagged or ask your own question.