7
$\begingroup$

Is there a way to include specific points in a plot?

The Exclusions option of Plot (and related plotting functions) allows us to tell Plot to avoid specific points, (e.g. because we know the plotted function is singular at that point). I want to do the opposite, tell Plot to include specific points in the initial points in the plot (before any recursion), because we know the plotted function will have a particularly sharp feature at that point, that might otherwise be missed.

As MWE consider the following:

f[x_?NumericQ] := Exp[-10^4 (x - \[Pi])^2]
Plot[f[x], {x, 5/2, 4}, PlotRange -> 1, PlotPoints -> 5]

With these settings Plot misses the Gaussian peak at x=Pi. Is there a way to tell Plot to specifically include x=Pi in the plotted points. (Of course, we can just krank up the PlotPoints to make sure we hit the feature, but this is not viable in the real world application I am dealing with.)

$\endgroup$
2
  • $\begingroup$ PlotPoints -> 5 is too low. Increase PlotPoints and use MaxRecursion, e.g., PlotPoints -> 6, MaxRecursion -> 10 $\endgroup$ Commented Sep 26, 2024 at 16:22
  • $\begingroup$ Use Show and Graphics along with PlotRange to get Show[Plot[f[x],{x,5/2,4},PlotRange->{{5/2,4},{0,1}}, PlotPoints->5],Graphics[Point[{Pi,Exp[0]}]]] to include a specific point with a plot. Then you can deal with pushing the plot resolution to be what you want. $\endgroup$ Commented Sep 26, 2024 at 16:45

1 Answer 1

10
$\begingroup$

I can't find the appropriate question right now, but the syntax is:

  • PlotPoints -> {None, pts} (initial sampling only at points pts) or
  • PlotPoints -> {Automatic, pts} (initial sampling at points pts and at equidistant points)
f[x_?NumericQ] := Exp[-10^4 (x - π)^2]
Plot[f[x], {x, 5/2, 4}, PlotRange -> 1, PlotPoints -> {Automatic, {Pi}}]

enter image description here

$\endgroup$
3
  • 3
    $\begingroup$ One ref for functions of two variables: mathematica.stackexchange.com/questions/19121/… $\endgroup$ Commented Sep 26, 2024 at 18:54
  • $\begingroup$ @MichaelE2 Thanks! That was almost exactly what I needed. I actually need the syntax for ParametricPlot3D , which does not seem to follow the same syntax as Plot3D (surprisingly). Do you happen to know if anyone looked into the ParametricPlot3D case? $\endgroup$ Commented Sep 27, 2024 at 7:24
  • $\begingroup$ @TimRias I know I looked into it and failed to discover a way. I decided it probably isn't implemented in all *Plot*[] functions. But I don't know for sure. $\endgroup$ Commented Sep 27, 2024 at 14:06

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.