4
$\begingroup$

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

Mathematica graphics

Thank you!

$\endgroup$
5
  • $\begingroup$ 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. $\endgroup$
    – MarcoB
    May 12, 2016 at 19:17
  • $\begingroup$ I meant overlapping line. $\endgroup$ May 12, 2016 at 19:18
  • $\begingroup$ I'm sorry, but I'm afraid that I don't understand what/where the overlapping line is. Could you clarify further? $\endgroup$
    – MarcoB
    May 12, 2016 at 19:22
  • $\begingroup$ 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! $\endgroup$ May 12, 2016 at 19:26
  • 1
    $\begingroup$ @crossingsymmetry, I added your last comment and a variant of the linked picture to the question. $\endgroup$
    – kglr
    May 12, 2016 at 19:57

3 Answers 3

11
$\begingroup$

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]

Mathematica graphics

$\endgroup$
2
  • $\begingroup$ It is what I wanted, is there a way to do this with ListLinePlot? $\endgroup$ May 12, 2016 at 19:42
  • 1
    $\begingroup$ you would need to use Interpolation, at which point you could ultimately go back to using Plot. $\endgroup$
    – chuy
    May 12, 2016 at 20:24
3
$\begingroup$
$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]}]

enter image description here

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

enter image description here

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

enter image description here

The overlap at the extrema is now shown.

$\endgroup$
2
$\begingroup$

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

enter image description here

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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