0
$\begingroup$

I've a function of two variables f(x, y) which assumes values between 0 and 1 only in a certain region of the domain of the R^2 space, i.e.

x ∈ (1, 2)

y ∈ (0, x/2)

I'm interested in a heatmap/density-plot, thus I'm doing

DensityPlot[f[x, y], {x, 1, 2}, {y, 0, 1}, 
            ColorFunction -> "SunsetColors", Frame -> True, 
            FrameLabel -> {"x", "y"}, 
            PlotLegends -> BarLegend[All, LegendLabel -> "Frequency"]]

How could I

  • exclude from the density map those values not in the [0, 1] range, or
  • shadow the plot area where f(x, y) is not between 0 and 1 ?

Thanks a bunch for your help!

$\endgroup$
  • 1
    $\begingroup$ Look up PlotRange and ClippingStyle. $\endgroup$ – 2012rcampion Apr 22 '15 at 15:07
  • $\begingroup$ The option PlotRange->{0,1}' will clip your z-values, and you can format the style of the clipping with 'ClippingStyle->"Whatever you want". I can't tell if this is what you want, or @Chip Hurst's answer. $\endgroup$ – N.J.Evans Apr 22 '15 at 15:08
  • $\begingroup$ Thank you, @N.J.Evans !! I'm accepting Chip Hurst's answer but your comment will help a lot as well. $\endgroup$ – Orso Apr 23 '15 at 9:14
0
$\begingroup$

This is the sole purpose of RegionFunction:

DensityPlot[Sin[10 x + 10 y], {x, 1, 2}, {y, 0, 1}, 
  ColorFunction -> "SunsetColors", Frame -> True, 
  FrameLabel -> {"x", "y"}, 
  PlotLegends -> BarLegend[All, LegendLabel -> "Frequency"], 
  RegionFunction -> Function[{x, y}, 0 < y < x/2]
]

enter image description here

Edit

Another way in V10 is

R = Polygon[{{1, 0}, {1, 0.5}, {2, 1}, {2, 0}}];

DensityPlot[Sin[10 x + 10 y], {x, y} ∈ R, 
  ColorFunction -> "SunsetColors", Frame -> True, 
  FrameLabel -> {"x", "y"}, 
  PlotLegends -> BarLegend[All, LegendLabel -> "Frequency"], 
  PlotPoints -> 40
]
$\endgroup$
  • $\begingroup$ A big "thank you", @Chip Hurst !!! That's exactly what I was looking for. $\endgroup$ – Orso Apr 23 '15 at 9:16

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.