4
$\begingroup$

What is the problem with my plot? I want to get a disk shape.

Plot[Table[-x + k, {k, -2, 2, .1}], {x, -5 ,5}, 
  RegionFunction -> Function[{x, y}, (x^2 + y^2) < 1]]

enter image description here

$\endgroup$
5
  • 2
    $\begingroup$ Try reducing the plotting range {x, -2, 2} and setting AspectRatio -> Automatic. $\endgroup$ Commented Jun 7, 2013 at 2:08
  • $\begingroup$ @0x4A4D yeah, set (x^2+y^2)<2 will be another right shape, do you know why this happen in the case (x^2+y^2)<1 ? $\endgroup$ Commented Jun 7, 2013 at 2:21
  • $\begingroup$ I didn't tell you to change the RegionFunction. I told you to change the plotting range. You used {x, -5, 5} previously. $\endgroup$ Commented Jun 7, 2013 at 2:27
  • 2
    $\begingroup$ Or increase your PlotPoints. $\endgroup$
    – Silvia
    Commented Jun 7, 2013 at 3:10
  • $\begingroup$ @0x4AD fine, with Silvia's suggestion, I am not so curious about knowing why this happens. --@@-- $\endgroup$ Commented Jun 7, 2013 at 4:23

2 Answers 2

3
$\begingroup$

I think this is a bug. Here is a test with a single straight line and a RegionFunction that differs in its threshold only by .007:

Table[
 Plot[-x, {x, -5, 5},
  RegionFunction -> Function[{x, y}, y <= max],
  AspectRatio -> Automatic,
  PlotRange -> {{-1, 0}, {0, 1}},
  Frame -> True,
  Mesh -> True,
  PlotLabel -> max
  ],
 {max, {0.7, 0.707}}]

comparison

Both lines should be visually the same. The mesh points indicate that the function was repeatedly evaluated far outside the allowed region in the second plot, but nevertheless it didn't try to refine the location of the region boundary.

The problem is very sensitive to the choice of region boundary.

To work around it, you can either change the interval of x in the Plot command to something slightly bigger or smaller than $\pm 5$, or increase the number of PlotPoints as Silvia already pointed out.

$\endgroup$
3
$\begingroup$

Mathematica by default cannot always guess what humans find aesthetic. In your case, it assumes the standard aspect ratio of 1/GoldenRatio. Specifying a ratio of 1 though is not enough, as the plot ranges are still automatically selected by Mathematica to include most of the data (in this case, all). So to plot a correct shape, you have to give both options explicit values, and the aspect ratio must reflect the ratios of the $x$ and $y$ ranges.

Plot[Table[-x + k, {k, -2, 2, .1}], {x, -5, 5}, 
 PlotRange -> {{-5, 5}, {-2, 2}},
 AspectRatio -> 2/5, 
 RegionFunction -> Function[{x, y}, (x^2 + y^2) < 1]]

enter image description here

$\endgroup$
6
  • 2
    $\begingroup$ Or, for the lazy: AspectRatio -> Automatic. $\endgroup$ Commented Jun 7, 2013 at 7:01
  • 1
    $\begingroup$ @0x4A4D True, but I don't find that didactic enough :) $\endgroup$ Commented Jun 7, 2013 at 7:02
  • 2
    $\begingroup$ @0x4A4D and Istvan Zachar - I thougth the main problem is existance of those 2 elongated lines, elliptical shape is obvious. On Your Plot strange lines are still there but it can be easily fixed with PlotPoints like Sylvia has said. $\endgroup$
    – Kuba
    Commented Jun 7, 2013 at 7:21
  • $\begingroup$ @Kuba, that's also why I told the OP to tweak the range of the independent variable. Nobody seems to take my suggestions seriously... :P $\endgroup$ Commented Jun 7, 2013 at 7:54
  • $\begingroup$ @0x4A4D I think it is because there is no precise question. Some think ellipsis is an issue and others are focusing on those lines. $\endgroup$
    – Kuba
    Commented Jun 7, 2013 at 8:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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