A data set with missing observations:
SeedRandom[1];
s = Map[If[Abs[#] < 10^-1, Missing[], #] &, RandomReal[{-1, 1}, {80, 50}], {-1}];
Update:
In general, since non-numeric / missing data do not lie in the numeric range of plotted values, a more accurate legend would be a combination of BarLegend
showing numerical values depicted in the plot and a seperate legend showing the color used for rendering non-numeric/missing values.
A simple way to achieve this is to combine a BarLegend
with a SwatchLegend
showing the label and color used to render missing/non-numeric values:
ListDensityPlot[s, Mesh -> Automatic, PlotRange -> {-1, 1},
ColorFunction -> "Rainbow",
PlotLegends -> Column[
{BarLegend["Rainbow", Frame -> False, LegendMargins -> 0],
SwatchLegend[{White}, {"Missing[]"}, LegendMarkerSize -> 14]}]]

The custom legend barLegendWithMissingValues
below adds a rectangle with desired color/label/size for missing values below a regular BarLegend
:
ClearAll[barLegendWithMissingValues]
barLegendWithMissingValues[label_ : Automatic, color_ : Automatic,
size_ : 30, gap_ : 3, offset_ : 10] :=
Module[{coord = First@Cases[ToBoxes@#, RasterBox[_, a_, ___] :> First@a, All]},
With[{txt = {label /. Automatic -> "Missing[]" /. Style -> StyleBox,
Offset[{offset, 0}, coord {-1, 1} - {0, gap + size/2}], Left},
clr = color /. Automatic -> LightGray,
rct = {#, # {-1, 1} + {0, size}} &@(coord - {0, size + gap}),
ef = EdgeForm[Directive[AbsoluteThickness[.5], RGBColor[.5, .5, .5]]]},
RawBoxes @ Replace[ToBoxes @ #,
{a___, r_RasterBox, b___} :> RuleCondition @
{a, r, InsetBox @@ txt, clr, ef, RectangleBox @@ rct, b}, All]]] &
Examples:
Row[{barLegendWithMissingValues[] @ BarLegend["Rainbow"],
barLegendWithMissingValues[Style["NaN", 12], Brown, 50, 5]@
BarLegend["Rainbow", LabelStyle -> 14],
barLegendWithMissingValues[Style["NaN", 12], Black, 40, 15]@
BarLegend[{"TemperatureMap", {-5, 15}}, LabelStyle -> 14,
LegendMarkerSize -> {90, 300}]}, Spacer[30]]

ReplaceAll[b_BarLegend :> barLegendWithMissingValues[Automatic, White]@b]@
ListDensityPlot[s, Mesh -> Automatic, PlotRange -> {-1, 1},
ColorFunction -> "Rainbow", PlotLegends -> Automatic]

ReplaceAll[b_BarLegend :> barLegendWithMissingValues[] @ b] @
ListDensityPlot[s, Mesh -> Automatic, PlotRange -> {-1, 1},
ColorFunction -> "Rainbow", BoundaryStyle -> White,
Prolog -> {LightGray, Rectangle[Scaled[{0, 0}], Scaled[{1, 1}]]},
PlotLegends -> Automatic]

Original answer:
ListDensityPlot
excludes (renders as White) areas where data is non-real. So we need to modify the BarLegend
to show the color used for excluded areas. This can be done using the (undocumented) option StyledContours
(or "StyledContours"
) and adding the associated tick label using the (also undocumented) option Ticks
("Ticks"
also works) in BarLegend
:
ListDensityPlot[s, Mesh -> Automatic, PlotRange -> {-1, 1},
ColorFunction -> "Rainbow",
PlotLegends -> BarLegend[Automatic,
StyledContours -> {{0, Directive[Thick, White]}},
Ticks -> (Range[-1, 1, .5] /. 0. -> {0, Missing[]})]]

We can set the color of excluded areas using Prolog
and use the same color for styled contours in BarLegend
:
ListDensityPlot[s, Mesh -> Automatic, PlotRange -> {-1, 1},
ColorFunction -> "Rainbow",
BoundaryStyle -> White,
Prolog -> {LightGray, Rectangle[Scaled[{0, 0}], Scaled[{1, 1}]]},
PlotLegends -> BarLegend[Automatic,
"StyledContours" -> {{0, Directive[AbsoluteThickness[5], LightGray]}},
"Ticks" -> (Range[-1, 1, .5] /. 0. -> {0, Missing[]})]]
