3
$\begingroup$

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

$\endgroup$
2
$\begingroup$

I would use ListPlot if values had to be excluded

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

enter image description here

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

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

enter image description here

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

enter image description here

$\endgroup$
1
$\begingroup$

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

enter image description here

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

enter image description here

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

enter image description here

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

enter image description here

$\endgroup$
  • $\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 with RegionFunction. Is there a way to prevent the function from being evaluated at excluded points? $\endgroup$ – becko Jul 8 '17 at 11:08
  • $\begingroup$ @becko, can you give a small example? $\endgroup$ – kglr Jul 8 '17 at 11:37
  • $\begingroup$ DiscretePlot[1/x, {x, -10, 10}, RegionFunction -> Function[{x, y}, x < -2 \[Or] x > 2]] $\endgroup$ – becko Jul 8 '17 at 12:43
  • $\begingroup$ @becko, can't think of any fix except using Quiet to suppress the message. $\endgroup$ – kglr Jul 8 '17 at 14:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.