2
$\begingroup$

this is a simple question, and excuse me if it's already been answered; I searched around and couldn't find anything.

I have two listplots, both along the same number of x data points, but with different y values. I want to find the difference between the two y values, while keeping the x values the same. I tried just subtracting the two, but that leaves all the x values as equal to 0, which is undesirable, of course.

$\endgroup$
  • 4
    $\begingroup$ Take a look at Transpose, Part etc. $\endgroup$ – Kuba Feb 3 '16 at 17:39
  • 4
    $\begingroup$ This snippet is one way to get what you want: Transpose[{list1[[All, 1]], (list1 - list2)[[All, 2]]}]. Take @Kuba's advice though, and look these operations up in the docs. Also interesting: Elegant way to handle columns operations; Elegant operations on matrix rows and columns. $\endgroup$ – MarcoB Feb 3 '16 at 18:06
  • $\begingroup$ You can do MapThread[#1-{0,Last@#2}&,{list1,list2}] if you want something short. Or list1 - ({0, 1}*# & /@ list2). $\endgroup$ – N.J.Evans Feb 3 '16 at 18:32
  • 1
    $\begingroup$ Ugh, I wish there was a better way to do this. I am often trying to do something like this, and end up using the Transpose listed by MarcoB, but every time I write it it just feels clunky to me. $\endgroup$ – Jason B. Feb 4 '16 at 7:50
2
$\begingroup$

ok, you have two lists,

list1 = {{1, 2}, {2, 3}, {3, 5}, {4, 7}, {5, 11}, {6, 13}, {7, 17}}
list2 = {{1, 3.87}, {2, 3.53}, {3, 3.40}, {4, 3.33}, {5, 3.25}, {6, 4.25}, {7, 5.24}}

and you know how to plot them,

ListPlot[{list1, list2}]

enter image description here

List Manipulation giving massive and solid knowledge about List

list1[[All, 2]] - list2[[All, 2]]

{-1.87, -0.53, 1.6, 3.67, 7.75, 8.75, 11.76}

ListPlot[list1[[All, 2]] - list2[[All, 2]]]

enter image description here

or, as proposed by @MarcoB

Transpose[{list1[[All, 1]], (list1 - list2)[[All, 2]]}]

{{1, -1.87}, {2, -0.53}, {3, 1.6}, {4, 3.67}, {5, 7.75}, {6, 8.75}, {7, 11.76}}

well, which generates us a new List.

Have Fun!

| improve this answer | |
$\endgroup$
2
$\begingroup$

Turn the data into time series, and do the arithmetic with them:

ts1 = TimeSeries[{{1, 2}, {2, 3}, {3, 5}, {4, 7}, {5, 11}, 
                  {6, 13}, {7, 17}}];
ts2 = TimeSeries[{{1, 3.87}, {2, 3.53}, {3, 3.40}, {4, 3.33}, 
                  {5, 3.25}, {6, 4.25}, {7, 5.24}}];

Normal[ts1 - ts2]

{{1, -1.87}, {2, -0.53}, {3, 1.6}, {4, 3.67}, {5, 7.75}, {6, 8.75}, {7, 11.76}}

| improve this answer | |
$\endgroup$
0
$\begingroup$

try something like this:

ListPlot@Thread[{Flatten[{#1[[1]][[All, 1]] - #1[[2]][[All,1]]}],
#1[[1]][[All, 2]]}] &@{{{1, 1}, {2, 2}, {3, 3}}, {{1,1}, {1, 2}, {1, 3}}}
| improve this answer | |
$\endgroup$

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