When I use RegionPlot to plot the region between two functions, I get strange gaps in the resulting figure. Is there a way to prevent this from happening?

For example,

RegionPlot[x^2 < y && y < x^4, {x, -3, 3}, {y, 0, 3}]

produces the following strange result:

enter image description here


Just increase the number of PlotPoints

RegionPlot[x^2 < y && y < x^4, {x, -3, 3}, {y, 0, 3}, 
  PlotPoints -> 100]

enter image description here

  • 9
    $\begingroup$ As a note, some functions require a lot more than 100 points, I've needed upwards of 500 in some cases. Also, x and y can have different number of points via PlotPoints -> {100, 150}. $\endgroup$ – rcollyer Jan 17 '12 at 22:58
  • 3
    $\begingroup$ This same principle also applies to other Plot functions. Sometimes Mathematica gets unlucky with sampling the function and you get discontinuities or odd behavior. In almost every case, either increasing PlotPoints or MaxRecursion will help. $\endgroup$ – Mike Bailey Jan 17 '12 at 23:24

Or, the MaxRecursion:

RegionPlot[x^2 < y && y < x^4, {x, -3, 3}, {y, 0, 3},
  MaxRecursion -> 8]

The plot commands generally use a adaptive procedure that is applied recursively. MaxRecursion controls how many times this recursion can be applied. PlotPoints by contrast, simply indicates how many points should be used in the initial grid. It might be simplest to illustrate with the most basic Plot command:

  Plot[Sin[x^2], {x, 0, 3},
    Mesh -> All, PlotRange -> 1.1,
    MaxRecursion -> mr, PlotPoints -> pp],
  {mr, 0, 8, 1}, {pp, 4, 100, 1}]

Mathematica graphics

This is a visualization of the sampling mesh for your example function:

RegionPlot[{x^2 < y && y < x^4, Not[x^2 < y && y < x^4]}, 
    {x, -3, 3}, {y, 0, 3}, Mesh -> All, MaxRecursion -> 4]

Mathematica graphics

  • $\begingroup$ What are the tradeoffs between MaxRecursion and PlotPoints ? $\endgroup$ – orome Jan 17 '12 at 22:44
  • 2
    $\begingroup$ MaxRecursion is adaptive and should be the preferable solution (faster for a given required plot accuracy). Compare: ImageDifference[RegionPlot[x^2<y&&y<x^4,{x,-3,3},{y,0,3},MaxRecursion->8],RegionPlot[x^2<y&&y<x^4,{x,-3,3},{y,0,3},PlotPoints->100]] $\endgroup$ – Andrew Moylan Jan 17 '12 at 22:53
  • $\begingroup$ @ras See the edit. $\endgroup$ – Mark McClure Jan 17 '12 at 23:00
  • 8
    $\begingroup$ @raxacoricofallapatorius PlotPoints is good for making sure features show up; MaxRecursion is better for "polishing" off the edges. (MaxRecursion generally can't improve something that isn't there to start with.) $\endgroup$ – Brett Champion Jan 18 '12 at 5:01
  • 3
    $\begingroup$ @AndrewMoylan Isn't it the case the to avoid holes or missing parts, one generally needs to increase PlotPoints, while MaxRecursion is only for increasing the accuracy of already found regions? While in this example it works well, I'm often a bit wary of MaxRecursion because of its exponential nature (increasing it by one may increase the plotting time significantly) $\endgroup$ – Szabolcs Jan 18 '12 at 15:37

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.