3
$\begingroup$

How do you define the domain of a plot? For instance, if I want to plot a standardized bivariate normal density function (with $r=0$), how do I specify the domain to be $[-3,3]$?

$\endgroup$
4
  • 1
    $\begingroup$ Plot[(e^(-x^2/2)/sqrt(2*pi)), {x, -3, 3}] $\endgroup$
    – Mike Wierzbicki
    Feb 24, 2012 at 4:36
  • $\begingroup$ @mike The answer in your comment is correct, but in the future you'd better provide it as a formal answer in the answer box and preferably with a bit of explanation. $\endgroup$ Feb 24, 2012 at 13:00
  • $\begingroup$ @badreferences We usually appreciate it if questions show some effort to understand the problem. Your question does not do this as this seems to be a question that could have been answered easily by reading the Plot section of mathematica's electronic manual. For future questions, please try to read the documentation first and if that doesn't help indicate where you encounter problems. $\endgroup$ Feb 24, 2012 at 13:10
  • 1
    $\begingroup$ @Sjoerd My apologies; I'm asking this question in proxy for someone else, as I myself don't use Mathematica. There are special circumstances preventing said person from accessing the internet. Given my unfamiliarity with computers in general, I merely copy-pasted the exact question. $\endgroup$ Feb 27, 2012 at 17:33

2 Answers 2

8
$\begingroup$

For simple regions like your interval or a square region, you can just give the appropriate border, as shown by the earlier answers. For completeness, I repeat them here:

Domain $[-3,3]$:

Plot[f[x], {x, -3, 3}]

Domain $[-3,3]\times [-3,3]$:

Plot3D[f[x,y], {x, -3, 3}, {y, -3,3}]

For more complex domains, you have several options:

First, you can explicitly use a function with limited domain, e.g. for the domain $[-2,-1)\cup(1,2]$:

Plot[ConditionalExpression[Sin[x], Abs[x] > 1], {x, -2, 2}]

Or for 2D:

Plot3D[ConditionalExpression[Sin[x y],x<y], {x, -Pi, Pi}, {y, -Pi, Pi}]

Another possibility is to give the region in the plot command:

Plot[Sin[x],{x,-2,2},RegionFunction->(Abs[#] > 1&)]

Plot3D[Sin[x y],{x,-Pi,Pi},{y,-Pi,Pi},RegionFunction->(#1<#2&)]

Finally, for 3D plot you might also use some parametrization of your domain resulting in a square parameter range and use ParametricPlot3D (you can, of course, do that for 2D plots as well, but there it's not as useful):

ParametricPlot3D[Module[{x=r Cos[phi],y=r Sin[phi]},{x, y, Sin[x y]}],
                 {r, 1, 2}, {phi, 0, 3 Pi/2}]

Of course you can also combine those methods, e.g.

ParametricPlot3D[Module[{x=r Cos[phi], y=r Sin[phi]}, {x, y, Sin[x y]}],
                 {r, 1, 2}, {phi, 0, 3 Pi/2},
                 RegionFunction->Function[{x, y, z, r, phi},
                                          x < 1.5 && phi < (r-1) 3 Pi/2]]
$\endgroup$
0
6
$\begingroup$

You can either plot the function only for a narrow range, or play around with the PlotRange option.

There is an important difference between specifying the plot range a la {x, xMin, xMax} and using the PlotRange option though: The first one determines which values will be calculated, the latter only crops the plot (it can do this even after the actual plotting has been done, it's an option for Show).

Here's an example that uses both options: It first calculates your multivariate distribution on $(x,y)\in([-5,5],[-5,5])$, and then displays only the proportion that lies in $([-5,3],[-2,3])$:

Plot3D[
    PDF[MultinormalDistribution[{-1, 1}, {{2, -1}, {-1, 1}}], {x, y}],
    {x, -5, 5},
    {y, -5, 5},
    PlotRange -> {{-5, 3}, {-2, 3}, All}
]

enter image description here

(The All in PlotRange stands for plotting all the $z$ components, i.e. the function is not cut off at the top if it is high.)

$\endgroup$
4
  • 1
    $\begingroup$ how does your rendering of that plot look so good, when I export a png from mathematica it looks terrible, bumpy, ugly mesh. Thanks! $\endgroup$
    – s0rce
    Feb 27, 2012 at 2:00
  • 1
    $\begingroup$ I've used a higher setting for PlotPoints and MaxRecursion to generte the picture above, but didn't include these parameters in the code here, because they're not really part of the answer. However, that changes the plot itself in general, not only the PNG export. How does your plot look like in the nb? $\endgroup$
    – David
    Feb 27, 2012 at 2:31
  • $\begingroup$ I had tried plotpoints and that solves the bumpiness but my mesh is very jagged from the rasterization, did you export to a pdf and use another program to rasterize? Thanks! $\endgroup$
    – s0rce
    Feb 27, 2012 at 3:03
  • $\begingroup$ Apart from the things mentioned above, I did just the usual "right click, save graphic as PNG" procedure. $\endgroup$
    – David
    Feb 27, 2012 at 3:49

Your Answer

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

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