Exclusions in DiscretePlot and DiscretePlot3D

How do I exclude certain values from a DiscretePlot or a DiscretePlot3D?

I would use ListPlot if values had to be excluded

DiscretePlot[Tan[x], {x, -2, 2, 0.1}, PlotRange -> Automatic]


tab = Table[Tan[x], {x, -2, 2, 0.1}];

ListPlot[tab, Filling -> Axis, PlotRange -> Automatic, DataRange -> {-2, 2}]


ListPlot[tab /. x_ /; Abs[x] > 10 :> Null, Filling -> Axis,
PlotRange -> All, DataRange -> {-2, 2}]


You can remove the excluded points/regions from the function domain using ConditionalExpression or using RegionFunction:

exclusions = Range[0, 2 Pi, Pi/3];
p2d = Plot[Sin[t], {t, 0, 2 Pi}];
plt1 = Show[DiscretePlot[Sin[t], {t, 0, 2 Pi, Pi/6}, ExtentSize -> Full], p2d,
ImageSize -> 300];
plt2 = Show[DiscretePlot[ConditionalExpression[Sin[t], Not[MemberQ[exclusions, t]]],
{t, 0, 2 Pi, Pi/6}, ExtentSize -> Full], p2d, ImageSize -> 300];
plt3 = Show[DiscretePlot[Sin[t], {t, 0, 2 Pi, Pi/6}, ExtentSize -> Full,
RegionFunction -> Function[{x, y}, Not[MemberQ[exclusions, x]]]], p2d,
ImageSize -> 300];

Row[{plt1, plt2, plt3}]


plt2a = Show[DiscretePlot[ConditionalExpression[Sin[t], Not[2 <= t < 4]],
{t, 0, 2 Pi, Pi/6}, ExtentSize->Full], p2d,  ImageSize -> 300];
plt3a = Show[DiscretePlot[Sin[t], {t, 0, 2 Pi, Pi/6}, ExtentSize -> Full,
RegionFunction -> Function[{x, y}, Not[2 <= x < 4]]], p2d, ImageSize -> 300];

Row[{plt1, plt2a, plt3a}]


p3d = Plot3D[PDF[BinormalDistribution[0.5], {x, y}], {x, -2, 2}, {y, -2, 2},
PlotStyle -> Directive[Opacity[0.4], Yellow], Mesh -> None];

exclusions2 = RandomSample[Range[-2, 2, 2/10], 6];

plt1b = Show[p3d, DiscretePlot3D[PDF[BinormalDistribution[0.5], {x, y}],
{x, -2, 2, 2/10}, {y, -2, 2, 2/10}, ExtentSize -> Full],
ImageSize -> 300,  ViewPoint -> {Pi, Pi, 2 Pi}];

plt2b = Show[p3d, DiscretePlot3D[ConditionalExpression[
PDF[BinormalDistribution[0.5], {x, y}], Not[MemberQ[exclusions2, x | y]]],
{x, -2, 2, 2/10}, {y, -2, 2, 2/10}, ExtentSize -> Full],
ImageSize -> 300, ViewPoint -> {Pi, Pi, 2 Pi}];

plt3b = Show[p3d, DiscretePlot3D[PDF[BinormalDistribution[0.5], {x, y}],
{x, -2, 2, 2/10}, {y, -2, 2, 2/10}, ExtentSize -> Full,
RegionFunction -> Function[{x, y, z}, Not[MemberQ[exclusions2, x | y]]]],
ImageSize -> 300, ViewPoint -> {Pi, Pi, 2 Pi}];

Row[{plt1b, plt2b, plt3b}]


plt2c = Show[p3d, DiscretePlot3D[ConditionalExpression[
PDF[BinormalDistribution[0.5], {x, y}],
Not[2 <= x ^2 + y^2 <= 3]], {x, -2, 2, 2/10}, {y, -2, 2, 2/10},
ExtentSize -> Full], ImageSize -> 300, ViewPoint -> {Pi, Pi, 2 Pi}];

plt3c = Show[p3d, DiscretePlot3D[PDF[BinormalDistribution[0.5], {x, y}],
{x, -2, 2, 2/10}, {y, -2, 2, 2/10}, ExtentSize -> Full,
RegionFunction -> Function[{x, y, z}, Not[2 <= x ^2 + y^2 <= 3]]],
ImageSize -> 300, ViewPoint -> {Pi, Pi, 2 Pi}];

Row[{plt1b, plt2c, plt3c}]


• The problem with ConditionalExpression is that it evaluates the argument, so singular points (that I want to avoid) will generate messages or give errors. The same occurs with RegionFunction. Is there a way to prevent the function from being evaluated at excluded points?
– a06e
Jul 8, 2017 at 11:08
• @becko, can you give a small example?
– kglr
Jul 8, 2017 at 11:37
• DiscretePlot[1/x, {x, -10, 10}, RegionFunction -> Function[{x, y}, x < -2 \[Or] x > 2]]
– a06e
Jul 8, 2017 at 12:43
• @becko, can't think of any fix except using Quiet to suppress the message.
– kglr
Jul 8, 2017 at 14:21