I have two lists produce by a script that are most likely identical (but might not be).

I would like to plot them using ListPlot[] in such a way to display only even elements from the first list and odd elements from the second one, essentially interleaving the two lists. Goal is to show that the two are in fact overlapping.

Sample data is just two identical lists such as

a = Table[y,{y,1,20}];
b = Table[y,{y,1,20}];

My lists come without x coordinates but can be added if needed.

  • $\begingroup$ Can you post example data...? $\endgroup$ – user6014 Jul 30 at 20:02
  • 1
    $\begingroup$ ListPlot[{list1[[;; ;; 2]], list2[[2 ;; ;; 2]]}]? $\endgroup$ – kglr Jul 30 at 20:04

Using sample data which actually is identical, of the form {x, y}, you can do this with creative use of Part:

data1 = data2 = Table[{x, Sin[x]}, {x, 0, 2 Pi, .1}];

ListPlot[{data1[[1 ;; ;; 2]], data2[[2 ;; ;; 2]]}, 
 PlotStyle -> {Red, Blue}]

enter image description here

data1[[1 ;; ;; 2]] takes every 2nd element of data1 starting from element 1, data2[[2 ;; ;; 2]] takes every 2nd element of data2 starting from element 2.

Specifying PlotStyle -> {Red, Blue} isn't explicitly necessary as they sets of data will be different styles by default, but you may wish to control how they look.

  • $\begingroup$ Precisely what I was looking for, thanks! $\endgroup$ – Three Diag Jul 30 at 20:16
l = RandomReal[100, 30];
{list1, list2} = Thread[{Range[30], #}] & /@ ({#, # + RandomReal[{-.05, 05}]} & @ l);

ListPlot[{list1[[;; ;; 2]], list2[[2 ;; ;; 2]]}, 
  PlotLegends -> {"list1", "list2"}, BaseStyle -> PointSize[Large]]

enter image description here


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.