9
$\begingroup$

Bug introduced in 9.0 or earlier and persisting through 11.0.1 or later


I'm creating solution sets for a calculus course and need to shade the region in $\mathbb R^2$ such that $x^2 < y < x^4$. This is what I have so far:

Plot[{x^2,x^4}, {x, -2, 2},PlotRange->{-1,5},PlotStyle->{Automatic,Red},
     Filling->{1->{{2},{LightBlue,White}}}]

Unfortunately this gives me the following picture. In particular, the region in the interval $(-1,0)$ is shaded. How can I avoid this?this

$\endgroup$
4
  • $\begingroup$ Possible duplicate of Filling Between Curves $\endgroup$
    – user9660
    Oct 7, 2015 at 7:02
  • 3
    $\begingroup$ This looks like a duplicate but it isn't. The issue here is that the OP is using the correct syntax, but the filling is nevertheless incorrect (possibly caused by the curves touching at 0 but not crossing). Looks like a bug to me. I guess a workaround may be to lower the x^2 function a tiny bit. $\endgroup$ Oct 7, 2015 at 7:03
  • $\begingroup$ I can reproduce the behavior in V9.0.1 and V10.2 $\endgroup$
    – Michael E2
    Oct 7, 2015 at 10:44
  • $\begingroup$ Same bug in Version 8 $\endgroup$ Oct 7, 2015 at 14:00

5 Answers 5

9
$\begingroup$

As I stated in my comment below the post, the filling syntax used by the OP is correct. The behavior seen in the plot is a bug.

A workaround is to simply increase the number of plot points. The following works:

Plot[{x^2, x^4}, {x, -2, 2}, 
    PlotRange -> {-1, 5}, PlotStyle -> {Automatic, Red}, 
    Filling -> {1 -> {{2}, {LightBlue, White}}}, 
    PlotPoints -> 100
]

Mathematica graphics

Update

Actually, it is not a small plot point number that seems to be the cause. Depending on the PlotPoint setting two of the four areas are either incorrectly filled or incorrectly not filled. The following plot shows the filling of those areas as a function of the PlotPoint value (1 is filled, 0 is not filled):

Mathematica graphics

$\endgroup$
5
  • $\begingroup$ Aha, nice catch +1. $\endgroup$
    – user9660
    Oct 7, 2015 at 10:02
  • $\begingroup$ There will be an update. There's more to it. $\endgroup$ Oct 7, 2015 at 10:03
  • $\begingroup$ You beat me to your update: i.stack.imgur.com/O6ojv.gif. There's an interesting "discontinuity" at 49 and 53 plot points. The x^2 curve jumps to the x^4 for a point or two at x == 1. $\endgroup$
    – Michael E2
    Oct 7, 2015 at 10:27
  • $\begingroup$ @Michael Yes, I noted that as well. Do you agree that we can tag this question with the "bugs" tag? Also, can I use your animation as an additional illustration? $\endgroup$ Oct 7, 2015 at 10:30
  • $\begingroup$ Yes, I think it's a bug. $\endgroup$
    – Michael E2
    Oct 7, 2015 at 10:32
6
$\begingroup$

Since Filling shades between two curves in the plot, add an extra curve that serves as the limit.

How about

Plot[{Max[x^2, x^4], x^2, x^4}, {x, -2, 2}, Filling -> {1 -> {2}}, 
PlotRange -> {-1, 5}]

enter image description here

To remember

Plot[{Min[x^2, x^4], x^2, x^4}, {x, -2, 2}, Filling -> {1 -> {2}}, 
PlotRange -> {-1, 5}]

enter image description here

$\endgroup$
1
  • $\begingroup$ Aha! A clever trick indeed. Thanks! $\endgroup$
    – user217285
    Oct 7, 2015 at 7:04
2
$\begingroup$

I got this here

Plot[{Max[x^2, x^4], x^2, x^4}, {x, -2, 2}, PlotRange -> {-1, 5}, 
PlotStyle -> {Automatic, Red}, Filling -> {1 -> {2}}]
$\endgroup$
1
  • $\begingroup$ just saw the answer by @Lou which I believe is from the same source. $\endgroup$
    – Hubble07
    Oct 7, 2015 at 7:00
2
$\begingroup$

Using Region* functionality:

The advantages of this approach are that one does not need to include additional curves to fill in the desired region and the description of the region is mathematical. The downside is that using Region commands may be accompanied with their own challenges.

Clear["Global`*"];
reg = ImplicitRegion[x^2 < y < x^4, {{x, -2, 2}, {y, -1, 5}}];

p1 = Plot[{x^2, x^4}, {x, -2, 2}
   , PlotRange -> {-1, 5}
   , PlotStyle -> {Automatic, Red}
   ];

p2 = RegionPlot[reg
   , PlotStyle -> HatchFilling[]
   , BoundaryStyle -> None
   ];

gr = Graphics[{#, Rectangle[]}
     , ImageSize -> Small
     ] & /@ {Red, ColorData[97][1], HatchFilling[]};

legend = SwatchLegend[{Red, ColorData[97][1], None}
   , {x^4, x^2, x^2 < y < x^4}
   , LegendMarkers -> gr
   , LegendMarkerSize -> 20];

Legended[
 Show[p1, p2]
 , Placed[legend, {0.65, 0.8}]
 ]

enter image description here

$\endgroup$
1
$\begingroup$
  • Another way is decreasing the MaxRecursion to <=1. We compare with the four cases.( Version 14.0 )
plots = Plot[{x^2, x^4}, {x, -2, 2}, PlotRange -> {-1, 5}, 
     PlotStyle -> {Automatic, Red}, 
     Filling -> {1 -> {{2}, {LightBlue, White}}}, MaxRecursion -> #, 
     PlotLabel -> "MaxRecursion=" <> ToString@#] & /@ {0, 1, 2, 4};
GraphicsGrid[Partition[plots, 2, 2]]

enter image description here

  • I belive that one of the problem is that y=x^2 and y=x^4 tangent at x=0 so x^2-x^4 does not change the sign at x=0.
$\endgroup$

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.