Contour calculations and RegionFunction

When a ContourPlot is restricted to a region, the contour levels are still being calculated based on data values from the entire rectangular field. I think it would be more useful if they were calculated from data values as restricted by the specified RegionFunction.

The attached notebook is an example. x^3 + y^3 is plotted over the unit square, with the region restricted to an enclosed disk. But contours are clearly calculated from the unrestricted data. This is of course a simple example of another data set I’m working with.

This

In[167]:= Plot[f[x, y], {x, -1, 1}, {y, -1, 1},RegionFunction -> Function[{x, y}, x^2 + y^2 <= (1/2)^2],
    Contours -> Range[1/32 - 0.025, 1/32, 0.005 ], PlotRange -> {{-3/5, 3/5}, {-3/5, 3/5}}]

will do it because of

In[166]:= f[x, x] /. Last[Solve[2 x^2 == (1/2)^2, x, Reals]]
Out[166]= 1/32

because of the symmetry coincidence.

1 Like

Hi,

in the Cloud (and probably in MMA 10 ?) this will work as well:

f[x_, y_] := x^4 + y^4;   
R = ImplicitRegion[x^2 + y^2 < 0.5^2, {x, y}];

and then

ContourPlot[f[x, y], {x, y} \[Element] R , Contours -> (FindDivisions[{#1, #2}, 10] &), ColorFunction -> "Rainbow"]

I suppose that it is actually useful to have the two options for plotting. The RegionFunction does not change the colours and with ImplicitRegion you can actually restrict your calculation to the domain you are interested in.

M.

1 Like

Yes! Very nice, Marco. Thanks.

1 Like