4
$\begingroup$

I would like to shade the white excluded region in the following DensityPlot

DensityPlot[Sin[x y], {x, -2, 2}, {y, 0, 3}, Frame -> True, FrameLabel -> {"x", "y"}, ColorFunction -> "Rainbow", RegionFunction -> Function[{x, y, f}, Abs[y] - Abs[x] > -.52], ColorFunctionScaling -> False, PlotPoints -> 50]

enter image description here

$\endgroup$

3 Answers 3

3
$\begingroup$
DensityPlot[Sin[x y], {x, -2, 2}, {y, 0, 3}, Frame -> True, 
  FrameLabel -> {"x", "y"}, ColorFunction -> "Rainbow", 
  RegionFunction -> Function[{x, y, f}, Abs[y] - Abs[x] > -.52], 
  ColorFunctionScaling -> False, PlotPoints -> 50, 
  Epilog -> RegionPlot[Abs[y] - Abs[x] <= -.52, {x, -2, 2}, {y, 0, 3}, 
    PlotStyle -> Green][[1]]]

enter image description here

Alternatively, use a Piecewise function as the first argument of DensityPlot:

DensityPlot[Piecewise[{{Sin[x y], Abs[y]-Abs[x] >= -.52}, {1/2, Abs[y]- Abs[x] <= -.52}}],
 {x, -2, 2}, {y, 0, 3}, 
 Frame -> True, FrameLabel -> {"x", "y"}, ColorFunction -> "Rainbow", 
 ColorFunctionScaling -> False, PlotPoints -> 100, Exclusions -> None]

enter image description here

You can modify the ColorFunction option setting to color the excluded region independent of the main region:

DensityPlot[Piecewise[{{Sin[x y], Abs[y]-Abs[x] >= -.52}, {100, Abs[y]-Abs[x] <= -.52}}], 
 {x, -2, 2}, {y, 0, 3}, Frame -> True, FrameLabel -> {"x", "y"}, 
  ColorFunction -> (If[# == 100, Yellow, ColorData["Rainbow"]@#] &), 
  ColorFunctionScaling -> False, PlotPoints -> 100, Exclusions -> None]

enter image description here

$\endgroup$
3
  • $\begingroup$ I like the second one. Thanks! $\endgroup$
    – MMA13
    Nov 11, 2018 at 10:41
  • $\begingroup$ But is it possible to choose a different code color because this region has no relevance to the rest of the plot? $\endgroup$
    – MMA13
    Nov 11, 2018 at 10:46
  • 1
    $\begingroup$ @Abdullah, please see the update for assigning a different color to the excluded region. $\endgroup$
    – kglr
    Nov 11, 2018 at 17:21
3
$\begingroup$

This is what I was Looking for, Thanks @kglr

DensityPlot[Sin[x y], {x, -2, 2}, {y, 0, 3}, Frame -> True, FrameLabel -> {"x", "y"}, ColorFunction -> "Rainbow",  RegionFunction -> Function[{x, y, f}, Abs[y] - Abs[x] > -.52], ColorFunctionScaling -> False, PlotPoints -> 50, Epilog -> RegionPlot[
Abs[\[Epsilon]] - Abs[ky] <= -.52, {ky, -2, 2}, {\[Epsilon], 0, 
 3}, Mesh -> 35, MeshStyle -> Lighter@Gray, 
MeshFunctions -> {#1 - #2 &, #1 + #2 &}, BoundaryStyle -> None, 
PlotStyle -> White][[1]]]

enter image description here

$\endgroup$
1
$\begingroup$

Another possibility is to combine two plots, using Show[], but have another ColorFunction or PlotTheme on the "shaded parts".

a = DensityPlot[Sin[x y], {x, -2, 2}, {y, 0, 3}, Frame -> True, 
   ImageSize -> 700, FrameTicksStyle -> Directive[Black, 26], 
   FrameLabel -> {Style["x", Black, FontSize -> 28], 
     Style["y", Black, FontSize -> 28]}, ColorFunction -> "Rainbow", 
   RegionFunction -> Function[{x, y, f}, Abs[y] - Abs[x] > -.52], 
   ColorFunctionScaling -> False, PlotPoints -> 50];
b = DensityPlot[Sin[x y], {x, -2, 2}, {y, 0, 3}, Frame -> True, 
   PlotTheme -> "Monochrome", 
   RegionFunction -> Function[{x, y, f}, Abs[y] - Abs[x] <= -.52], 
   ColorFunctionScaling -> False, PlotPoints -> 50];


Show[{a,b}]

enter image description here

The downside with this method is that there will be a sharp discontinuity, since the DensityPlot in Grayscale is not the same as you get when you convert the Rainbow colors to Grayscale (red would appear darker on the Grayscale than the surrounding yellow parts). For b you could use the same ColorFunction as in a and then use ColorConvert[b,"Grayscale"] if you prefer this for visual reasons, but it would distort the interpretation of the data in the plot.

Yet another possibility is that you artificially put irrelevant values to zero. This would make the implementation simpler than for "shading", but, it is important to clearly specify that the data is artificially set to zero in that case.

DensityPlot[
 If[Abs[y] - Abs[x] > -.52, Sin[x y], 0], {x, -2, 2}, {y, 0, 3}, 
 Frame -> True, ImageSize -> 700, 
 FrameTicksStyle -> Directive[Black, 26], 
 FrameLabel -> {Style["x", Black, FontSize -> 28], 
   Style["y", Black, FontSize -> 28]}, ColorFunction -> "Rainbow", 
 ColorFunctionScaling -> False, PlotPoints -> 50]

enter image description here

For the second plot, you could easily change the 0 in the If[] statement to e.g. -1 if you want to make these areas distinct from real zeros.

$\endgroup$

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.