3
$\begingroup$

I want to use DensityPlot to plot the function, let's say f[x,y]=xy, but only in the x<y region with 0<x<3, 1<y<4. How can I do this? What about other regions?

$\endgroup$
10
$\begingroup$

Here is a starting point.

  1. First, define your function:

    ClearAll[f]
    f[x_, y_] := x y
    
  2. Then set up a density plot using DensityPlot and specifying appropriate ranges for the $x$ and $y$ variables; then provide an appropriate RegionFunction option to specify that the plot should only be drawn wherever $x<y$:

    DensityPlot[
      f[x, y], {x, 0, 3}, {y, 1, 4}, 
      RegionFunction -> Function[{x, y, z}, x < y]
    ]
    

density plot over desired region x<y


  1. For more complex regions, the approach is similar, except that you should have a way of generating a function that reports on whether an $(x,y)$ point is within your desired plotting region or not. Here is an example with a triangular region:

    rmf = RegionMember@Triangle[{{1.5, 1}, {2.5, 3}, {0.5, 3}}]
    DensityPlot[
      f[x, y], {x, 0, 3}, {y, 1, 4},
      RegionFunction -> Function[{x, y, z}, rmf[{x, y}]],
      PlotPoints -> 30
    ]
    

triangular region plot

| improve this answer | |
$\endgroup$
2
$\begingroup$

Another approach is to define your function only for $x<y$ as follows

f[x_,y_]:=x y/;x<y
DensityPlot[f[x,y],{x,0,3},{y,1,4}]

this give you the same graphics. enter image description here

| improve this answer | |
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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