How do I exclude certain values from a DiscretePlot
or a DiscretePlot3D
?
2 Answers
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}]
-
$\begingroup$ 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 withRegionFunction
. Is there a way to prevent the function from being evaluated at excluded points? $\endgroup$– a06eJul 8, 2017 at 11:08 -
-
$\begingroup$
DiscretePlot[1/x, {x, -10, 10}, RegionFunction -> Function[{x, y}, x < -2 \[Or] x > 2]]
$\endgroup$– a06eJul 8, 2017 at 12:43 -
$\begingroup$ @becko, can't think of any fix except using
Quiet
to suppress the message. $\endgroup$– kglrJul 8, 2017 at 14:21