Mathematica Stack Exchange is a question and answer site for users of Mathematica. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I wish to plot 2 functions $f$ and $g$ so that the region bounded by $f$ has color $c_f$, the region bounded by $g$ has color $c_g$, and the region bounded by both has color $c_f+c_g$. E.g. $c_f$ is red, $c_g$ is blue, $c_f+c_g$ is magenta.

I tried this:

Plot[{Sin[x], Cos[x]}, {x, -π, +π}, Filling -> {1 -> {Axis, Blue}, 2 -> {Axis, Red}}]


I found no documentation on how to do additive mixture of colors in a plot. How can I do so?

share|improve this question
up vote 2 down vote accepted

A simple approach would be to plot a third function which defines the overlap region.

overlap[f_List] := Piecewise[{{Sign[First@f] Min[Abs@f], Equal @@ Sign[f]}}, I]

Plot[{Sin[x], Cos[x], overlap[{Sin[x], Cos[x]}]}, {x, -π, π},
 Filling -> {1 -> {0, Blue}, 2 -> {0, Red}, 3 -> {0, Thread[Red + Blue, RGBColor]}}]

enter image description here

share|improve this answer
Thanks. It's a slight kludge, but it works. I actually defined overlap thus: overlap[x_, y_] := UnitStep[Sign[x] Sign[y]] Sign[x] Min[Abs[x], Abs[y]] – M Farkas-Dyck Nov 24 '13 at 15:41
@strake: For two functions the easiest thing to do might be Median[{x, y, 0}]. – Rahul Nov 24 '13 at 16:50
@RahulNarain, that's clever. – Simon Woods Nov 24 '13 at 16:53
@RahulNarain Thanks, that's much neater. – M Farkas-Dyck Dec 10 '13 at 19:36

One easy way will be to use Opacity!

Plot[{Sin[x], Cos[x]}, {x, -\[Pi], +\[Pi]},
Filling -> {1 -> {Axis, Directive[Opacity[.7], Blue]},
2 -> {Axis, Directive[Opacity[.7], Red]}}]

enter image description here

Update: Here comes a better solution for your problem. The function is pretty much self explanatory as far as the argument names are concerned. Given a list of functions to be plotted together we take any one of them and use Plot to sample this function. Then we do a condition check on the sample to find the x-coordinates for the common region. We do a ListLinePlot for this common region and use Show to display it on the default plot.

UnionPlot[funs_, samplingFunction_, {start_, end_}, fillingUnion_, 
  plot_, plotPoints_: 600, maxRecursion_: 4] := 
 Block[{union, sample, fun},
  union = 
   If[And @@ (#[x] <= 0 & /@ ##), Evaluate@(Max @@ (#[x] & /@ ##)), 
      If[And @@ (#[x] > 0 & /@ ##), Evaluate@(Min @@ (#[x] & /@ ##)), 
       0]] &@funs;
  sample = (First@Cases[Plot[Evaluate[funs[[samplingFunction]][x]],
  {x, start, end},PlotPoints -> plotPoints, MaxRecursion -> maxRecursion], 
       Line[a___] :> a, Infinity])[[All, 1]]; 
  fun = Function[x, Evaluate@union];
   ListLinePlot[Transpose@{sample, fun[#] & /@ sample}, 
    Filling -> Axis, PlotRange -> All, InterpolationOrder -> 1, 
    Evaluate@fillingUnion, Axes -> False]]


Lets take a list of functions to be plotted and define a FillingStyle for the common region!

funs = {Sin[3 #] &, Cos[4 #] &, .8 Sin[2 #]^2 Cos[3 #] &};
commonfillingStyle = FillingStyle -> Blend[{Red, Blue, Green}, .3];

Now we will use the 3rd function to sample the x-coordinate of our plot.

UnionPlot[funs, 3, {-Pi, Pi}, commonfillingStyle, 
 Plot[Evaluate[#[x] & /@ funs], {x, -Pi, Pi}, 
  Filling -> {1 -> {Axis, None}, 2 -> {Axis, Red},3 -> {Axis, Orange}}]]

enter image description here

Your example case with commonfillingStyle = FillingStyle -> Blend[{Red, Blue}, .5] becomes the following.

enter image description here

share|improve this answer
Not additive mixture; try swapping the colors. I tried this with my actual functions and it was unreadable. – M Farkas-Dyck Nov 23 '13 at 23:21
@strake Check the update! Tell me if this solves your problem. – PlatoManiac Nov 24 '13 at 2:18
Unfortunately I'm not quite sure how to use this in my case. I tried to rewrite the plotted expression as a function evaluation but failed. – M Farkas-Dyck Nov 24 '13 at 4:51

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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