8
$\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$
  • $\begingroup$ Possible duplicate of Filling Between Curves $\endgroup$ – user9660 Oct 7 '15 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$ – Sjoerd C. de Vries Oct 7 '15 at 7:03
  • $\begingroup$ I can reproduce the behavior in V9.0.1 and V10.2 $\endgroup$ – Michael E2 Oct 7 '15 at 10:44
  • $\begingroup$ Same bug in Version 8 $\endgroup$ – Sjoerd C. de Vries Oct 7 '15 at 14:00
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$
  • $\begingroup$ Aha, nice catch +1. $\endgroup$ – user9660 Oct 7 '15 at 10:02
  • $\begingroup$ There will be an update. There's more to it. $\endgroup$ – Sjoerd C. de Vries Oct 7 '15 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 '15 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$ – Sjoerd C. de Vries Oct 7 '15 at 10:30
  • $\begingroup$ Yes, I think it's a bug. $\endgroup$ – Michael E2 Oct 7 '15 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$
  • $\begingroup$ Aha! A clever trick indeed. Thanks! $\endgroup$ – user217285 Oct 7 '15 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$
  • $\begingroup$ just saw the answer by @Lou which I believe is from the same source. $\endgroup$ – Hubble07 Oct 7 '15 at 7:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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