# Is it possible to shade specific region in DensityPlot?

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]


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]]]


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]


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]


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

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]]]


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}]


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]


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.