7
$\begingroup$

Let's plot two functions $2e^{-\pi x^2}$ and $e^{-x}$ and fill the area between two curves with two colors depending on the sign of their difference. However, the following code does not work:

Plot[{2 E^(-Pi x^2), E^(-x)}, {x, 0, 5},PlotLegends -> "Expressions",
  Filling -> {1 -> {{2}, {LightBlue, Red}}}, PlotRange -> All]

enter image description here

However, if start the plot with $-0.01$, it works!

Plot[{2 E^(-Pi x^2), E^(-x)}, {x, -.01, 5},PlotLegends -> "Expressions",
  Filling -> {1 -> {{2}, {LightBlue, Red}}}, PlotRange -> All]

enter image description here

Why is it so weird?

$\endgroup$

2 Answers 2

5
$\begingroup$

Set MaxRecursion->r and let r<4. Maybe a bug.

r=2;
Plot[{2  E^(-Pi  x^2), E^(-x)}, {x, 0, 5}, 
   PlotLegends -> "Expressions", 
   Filling -> {1 -> {{2}, {LightBlue, Red}}}, PlotRange -> All, 
   MaxRecursion -> r]

enter image description here

$\endgroup$
2
  • 2
    $\begingroup$ Even manually specifying PlotPoints seems to work, for example PlotPoints -> 2 ... $\endgroup$
    – Domen
    Commented Mar 4 at 8:35
  • $\begingroup$ That is a good tip. Does anyone know why it works for points? $\endgroup$
    – MathArt
    Commented Jun 6 at 6:47
5
$\begingroup$

As a workaround, plotting over the Interval {0,5} works as well as this can be changed.

Clear["Global`*"];
f1 = 2 E^(-Pi x^2);
f2 = E^(-x);
c1 = Opacity[0.2, ColorData[97][1]];
c2 = Opacity[0.2, ColorData[97][2]];

Plot[{f1, f2}, x ∈ Interval[{0, 5}]
 , PlotRange -> All
 , Filling -> {1 -> {{2}, {c2, c1}}}
 , PlotLegends -> Placed[LineLegend[Automatic, {f1, f2}], {0.8, 0.6}]
 ]

Original:

On v12.2.0 running on Win7-x64, there seems to be no issue.

Clear["Global`*"];
f1 = 2 E^(-Pi x^2);
f2 = E^(-x);
c1 = Opacity[0.2, ColorData[97][1]];
c2 = Opacity[0.2, ColorData[97][2]];

Plot[{f1, f2}, {x, -1, 3}
 , Filling -> {1 -> {{2}, {c2, c1}}}
 , PlotLegends -> Placed[LineLegend[Automatic, {f1, f2}], {0.8, 0.6}]
 ]

enter image description here


Plot[{f1, f2}, {x, 0, 3}
 , Filling -> {1 -> {{2}, {c2, c1}}}
 , PlotLegends -> Placed[LineLegend[Automatic, {f1, f2}], {0.8, 0.6}]
 ]

enter image description here

$\endgroup$
4
  • 1
    $\begingroup$ The problem is that it doesn't work for particular ranges of $x$. Namely, please try with {x, 0, 5}. $\endgroup$
    – Domen
    Commented Mar 4 at 10:14
  • $\begingroup$ I see; I will delete the answer since the choice of domain affects the filling. Thanks @Domen $\endgroup$
    – Syed
    Commented Mar 4 at 10:21
  • $\begingroup$ Domen Can you please check if the workaround works for other versions. $\endgroup$
    – Syed
    Commented Mar 4 at 10:26
  • 1
    $\begingroup$ The workaround with Interval works for v13.3 and v14.0 (Cloud). $\endgroup$
    – Domen
    Commented Mar 4 at 10:28

Your Answer

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

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