2

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.

user37452
  • 21
  • 1

3 Answers3

2

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!

2

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

Brett Champion
  • 20,779
  • 2
  • 64
  • 121
0

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}}}
Ali Hashmi
  • 8,950
  • 4
  • 22
  • 42