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]$?
2 Answers
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]]
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}
]
(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.)
-
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$– s0rceFeb 27, 2012 at 2:00
-
1$\begingroup$ I've used a higher setting for
PlotPoints
andMaxRecursion
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$– DavidFeb 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$– s0rceFeb 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$– DavidFeb 27, 2012 at 3:49
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$