# Shading a region where an inequality is satisfied

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?

• Possible duplicate of Filling Between Curves
– user9660
Commented Oct 7, 2015 at 7:02
• 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. Commented Oct 7, 2015 at 7:03
• I can reproduce the behavior in V9.0.1 and V10.2 Commented Oct 7, 2015 at 10:44
• Same bug in Version 8 Commented Oct 7, 2015 at 14:00

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
]


## 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):

• Aha, nice catch +1.
– user9660
Commented Oct 7, 2015 at 10:02
• There will be an update. There's more to it. Commented Oct 7, 2015 at 10:03
• You beat me to your update: i.sstatic.net/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. Commented Oct 7, 2015 at 10:27
• @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? Commented Oct 7, 2015 at 10:30
• Yes, I think it's a bug. Commented Oct 7, 2015 at 10:32

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

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


To remember

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


• Aha! A clever trick indeed. Thanks! Commented Oct 7, 2015 at 7:04

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

• just saw the answer by @Lou which I believe is from the same source. Commented Oct 7, 2015 at 7:00

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


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


• 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`.