# How to find the difference between two plots?

I have the following analytical plot.

i2k[i_] = 1/2 (i - 1)
pixx = k^2 Exp[-2 k^2 Sin[(t)/2]^2 ](-Sin[2t] - Sin[t](1-k^2 Sin[t]^2));
piyy = k^2 Exp[ -2 k^2 Sin[(t)/2 ]^2 ](-4Sin[t]Sin[(t)/2]^2 - Sin[t](1-4k^2 Sin[(t)/2]^4));
pixy = k^2 Exp[-2k^2 Sin[(t)/2 ]^2 ](2Cos[t]Sin[(t)/2]^2 + Sin[t]^2 - 2 k^2 Sin[(t)/2]^2 Sin[t]^2);

xxrot = Cos[t + 2.0 - \[Pi] / 2]^2 pixx +
Sin[t + 2.0 - \[Pi] / 2]^2 piyy -
Sin[2 (t + 2.0 - \[Pi] / 2)] pixy;

xxrotplt =
Plot[ xxrot /. k -> -i2k[6] /. t -> t - 2 // Evaluate, {t, 666 0.01,
950 0.01}, PlotRange -> All, PlotTheme -> "Detailed",
LabelStyle -> Directive[FontSize -> 40], PlotStyle -> Red]



Then my code produced the following result. s11resp is the data from the code which I can not have here for some reasons. It is a {30000, 1, 512, 1} dimension list.

s11pltcc =
ListLinePlot[Re@s11resp[[666 ;; 950, 1, 6, 1]], PlotRange -> All,
DataRange -> {665 0.01, 949 0.01} , PlotTheme -> "Detailed",
LabelStyle -> Directive[FontSize -> 40]]


Now I should compare the code and analytic result. The problem is if we show them together the difference is invisible.

My question is how to make the difference between the code and analytical results visible? I appreciate it if you could help me.

• You could use PlotStyle->Dashed in one plot and Show[...] Nov 10, 2023 at 10:02
• Is it also possible to somehow plot the subtraction of the two? My problem is then having a list of data and an analytical formula. It is not clear to me how to plot the subtraction. Nov 10, 2023 at 10:08
• If you know how the data was sampled, why not sample your analytic function at the same points, then compare the two?
– user87932
Nov 10, 2023 at 18:50

To compare two nearly identical plots

plot1=Plot[Sin[x],{x,0,2 Pi},PlotStyle->Red];
plot2=Plot[Sin[x],{x,0,2 Pi},PlotStyle->{Dashed,Black}];
Show[plot1,plot2]


Suppose we have an approximation

sin = Function[x, Sin[x] (1 + RandomReal[{-.01, .01}])]


we can plot the difference easily

Plot[{Sin[x] - sin[x]}, {x, 0, 2 Pi} ]


If you want to compare with data try

data=Table[{x,sin[x]},{x,RandomReal[{0,2Pi},10]}];
error=Map[Sin[#[[1]]]-#[[2]]&,data]&
ListPlot[error]


Perhaps Needs["ErrorBarPlots"] might find your interest!

• To show the accuracy of a code, this is not sufficient. I need to show the difference in a more robust way. Imagine you want to see the convergence of the code result for a bigger time step in your simulation. Then every time you produce the same comparison because the changes are so tiny. Nov 10, 2023 at 10:13
• In your question you compared the two plots not their difference! Nov 10, 2023 at 10:31