0
$\begingroup$

I had a problem with ContourPlot not showing the following function for certain domain values.

The function I was looking at: $f(x,y)=\left(\frac{\sin \left(\frac{x}{2}\right) \sin \left(\frac{y}{2}\right)}{x y}\right)^2$.

This function is largest at $(0,0)$ where it takes the value $0.0625$. When I enlarged the domain of the ContourPlot, Mathematica didn't include the center of the plot. I tried using Exclusions, RegionPlot and specifying Contours to get around this behaviour, but nothing worked. This is shown in the image below.

ContourPlot problem

Then as I was typing up this question I found the answer which I will post below.

$\endgroup$

marked as duplicate by m_goldberg, Michael E2 plotting Jun 17 '17 at 3:45

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

2
$\begingroup$

I realised I had used PlotRange->All with Plot3D and PlotRange->Automatic with ContourPlot. This is the source of the problem, and simply specifying the PlotRange on both plots resolves the issue, as shown in the image below.

This was one of those "you have got to be ******* kidding me moments" that are always fun to read about, but not so much fun to perform. Hopefully this post helps someone out there on the interwebs.

ContourPlot problem solution

The following code produces the above image.

ClearAll[r, list,range];
list = Range[-1, 1, 1/4];
size1 = 20; size2 = 24; aspect = .7; imagesize = 600; imagesize3d = 600;
f[x_, y_] := ((Sin[x/2] Sin[y/2])/(x y))^2;
max = Limit[Limit[f[x, y], x -> 0], y -> 0];

plot1[r_, range_] := 
  ContourPlot[f[x, y], {x, -r, r}, {y, -r, r}, 
   PlotRange -> range,
   Contours -> Range[0.01, 0.0625, 0.005], 
   ColorFunction -> "DeepSeaColors", AspectRatio -> 1, 
   ImageSize -> imagesize, PlotPoints -> 50, 
   LabelStyle -> Directive[FontSize -> size1], 
   PlotLabel -> 
    Style[HoldForm["r"] == (r/\[Pi])*HoldForm[\[Pi]], Bold, size2], 
   FrameLabel -> {x, y}, 
   FrameTicks -> {{#*r, # "r"} & /@ (2*list), {#*r, # "r"} & /@ (2*
        list)}];
plot3[r_, range_] := 
  Plot3D[f[x, y], {x, -r, r}, {y, -r, r},
   PlotRange -> range,
   LabelStyle -> Directive[FontSize -> size1], PlotPoints -> 50, 
   PlotStyle -> {Lighter[Orange]}, 
   PlotLabel -> 
    Style[HoldForm["r"] == (r/\[Pi])*HoldForm[\[Pi]], Bold, size2], 
   AxesLabel -> {Style[x, Bold, size2], Style[y, Bold, size2], 
     Style[z, Bold, size2]}, AxesStyle -> Thickness[0.005], 
   Ticks -> {{#*r, # "r"} & /@ (2*list), {#*r, # "r"} & /@ (2*list), 
     Range[0, 0.0625, 0.01]}, 
   Mesh -> {list*r, list*r}, 
   FaceGrids -> {{{0, 1, 0}, {list*r, list*max}}, {{-1, 0, 
       0}, {list*r, list*max}}}, ImageSize -> imagesize3d, 
   BoxRatios -> {1, 1, aspect}];
image = GraphicsGrid[{{plot1[2.5 \[Pi], Automatic], 
     plot3[2.5 \[Pi], Automatic]}, {plot1[2.5 \[Pi], All], 
     plot3[2.5 \[Pi], All]}}, Frame -> All, Spacings -> 0, 
   ImageSize -> 1200];
imageras = Rasterize[image, ImageResolution -> 96]
$\endgroup$

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