# Changing color of overlapping plots

If I have two plots with two different colors,

Plot[{Sin[x]^2, Sin[10 x]^2}, {x, 0, 5}, PlotStyle -> {Red, Blue}]


is there a way to change the color of the overlapping line? I don't want to fill rather change the overlapping part only. I wanted to do this for ListLinePlot if possible!

In the following plot blue curve is on top of the red one. What I want is every time the blue and red overlap change the color of the overlapping line.

Plot[{Sin[x]^2, Sin[5 x]^2}, {x, 0, 5},
PlotStyle -> {Directive[Red, Thickness[.02]], Directive[Blue, Thickness[.02]]}]


Thank you!

• Adding Filling -> {1 -> {2}} yields this result. Is that along the lines of what you want? Perhaps you could define what you mean by "overlapping region" in your case. – MarcoB May 12 '16 at 19:17
• I meant overlapping line. – crossingsymmetry May 12 '16 at 19:18
• I'm sorry, but I'm afraid that I don't understand what/where the overlapping line is. Could you clarify further? – MarcoB May 12 '16 at 19:22
• In the following plot s32.postimg.org/541nyxc3p/test.png blue curve is on top of the red one, what I want is every time the blue and red overlap change the color of the overlapping line. Thank you! – crossingsymmetry May 12 '16 at 19:26
• @crossingsymmetry, I added your last comment and a variant of the linked picture to the question. – kglr May 12 '16 at 19:57

MeshFunctions are useful here.

Plot[{Sin[x]^2, Sin[10 x]^2}, {x, 0, 5}, PlotStyle -> {Red, Blue},
Mesh -> {{0}}, MeshFunctions -> {Sin[#]^2 - Sin[10 #]^2 &},
MeshStyle -> Green]


• It is what I wanted, is there a way to do this with ListLinePlot? – crossingsymmetry May 12 '16 at 19:42
• you would need to use Interpolation, at which point you could ultimately go back to using Plot. – chuy May 12 '16 at 20:24
\$Version

(*  "10.4.1 for Mac OS X x86 (64-bit) (April 11, 2016)"  *)


Unfortunately, MeshFunctions does not work fully for all cases. For your second example,

Plot[{Sin[x]^2, Sin[5 x]^2}, {x, 0, 5},
PlotStyle -> {{Thick, Red}, {Thick, Blue}}, Mesh -> {{0}},
MeshFunctions -> {Sin[#]^2 - Sin[5 #]^2 &},
MeshStyle -> {Green, AbsolutePointSize[5]}]


Note that overlaps at the extrema are not shown. A more reliable method is to Solve for the intersections and use Epilog.

xVal = Solve[{Sin[x]^2 == Sin[5 x]^2, 0 <= x <= 5}, x, Reals] // FullSimplify // Union


Plot[{Sin[x]^2, Sin[5 x]^2}, {x, 0, 5},
PlotStyle -> {{Thick, Red}, {Thick, Blue}},
Epilog -> {Green, AbsolutePointSize[5], Point[{x, Sin[x]^2} /. xVal]}]


The overlap at the extrema is now shown.

You can always find the intersection separately and then do what you want. For example,

f1[x_] = Sin[x]^2;
f2[x_] = Sin[10 x]^2;
pts = x /. NSolve[{f1[x] == f2[x], 0 < x < 5}, x];
Plot[{f1[x], f2[x]}, {x, 0, 5}, PlotStyle -> {Red, Blue},
Prolog -> {Green,Evaluate[{Opacity[0.5], Disk[{#, f1[#]}, {5, 1} 0.02] & /@ pts}]}]