# Annoying errors in sections of plot

Not a major problem, but for some reason, Mathematica outputs unevaluated diagonal sections in what should be a step plot:

Plot[{PrimePi[x]}, {x, 0, 200}] Incidentally, I am using v 8.0.1.0 on a Windows 32-bit OS.

For this kind of plot DiscretePlot is probably the best choice:

DiscretePlot[PrimePi[x], {x, 0, 200}, Filling -> None] If you want to have perfect vertical lines joining the steps, ListPlot would be a good alternative:

t1 = Table[{x, PrimePi[x]}, {x, 0, 200}];
t2 = Table[{x + 1, PrimePi[x]}, {x, 0, 200}];
ListPlot[Riffle[t1, t2], Joined -> True] • @ Sjoerd C. de Vries, many thanks - the bottom plot is what I was really after! :) – martin Nov 20 '13 at 9:52

Perhaps the explanation of why Plot does not produce an accurate graph is easily inferred from the other answers: Plot samples points in constructing a plot. For a function with many discrete jumps, Plot may not detect all of them with the automatically chosen sample points. Sjoerd's answer shows the best ways of dealing with plotting PrimePi, but to get Plot to work there are a couple of approaches.

First, one might increase the number of sample points with the option PlotPoints. The initials points are at the minimum bisected once, so for the domain {x, 0, 200}, one should expect that about half the integers, or PlotPoints -> 100 will be about the minimum needed to produce a pretty good graph. There may still be a perceptible angle to the "vertical" joins, but there will always be a slight angle. One can check that PlotPoints -> 99 still produces one slanted line around x equals 45 or 50. Once all the steps are all detected, the recursive subdivision refines the jump. How much recursion is done is controlled by MaxRecursion, which may be increased to make the jumps look vertical, if necessary.

Plot[PrimePi[x], {x, 0, 200}, PlotPoints -> 101] Second, one might make use of the Exclusions option. Just feed it all the discontinuities.

Plot[{PrimePi[x]}, {x, 0, 200},
Exclusions -> Prime[Range[PrimePi]],
ExclusionsStyle -> ColorData] Here's a variation on Sjoerd's ListPlot method, using Prime for the x coordinate instead of PrimePi for the y coordinate.

p0 = Prime[Range[PrimePi@200]] ~Append~ 200;  (* x coords of jumps, plus endpoint *)
t1 = Transpose[{p0, Range[0, PrimePi@200]}];
t2 = Transpose[{Most[p0] ~Prepend~ 0, Range[0, PrimePi@200]}];
ListPlot[Riffle[t2, t1], Joined -> True]
(* output looks the same *)


You looking for something like this?

Plot[{PrimePi[x]}, {x, 0, 200}, Mesh -> All, PlotPoints -> 1050] By the way you don't need {} just this input will be enough for plotting

Plot[PrimePi[x], {x, 0, 200}, Mesh -> All, PlotPoints -> 1050]


For more info please see this http://reference.wolfram.com/mathematica/ref/PrimePi.html where you will see the plot of PrimePi[x].

To see the no diagonal sections - let's try for x=0..20 For a large domain - let's try x=100..200 • Thank you! Any idea why the basic plot produces the above output though? – martin Nov 20 '13 at 8:32
• Yes, I realised that :) – martin Nov 20 '13 at 8:36
• Had a look at the link - but could see no mention of issue in question ... – martin Nov 20 '13 at 8:40
• It's obvious that you will not see the diagonal sections clearly because you chose x=0..200. – zhk Nov 20 '13 at 8:43
• Is it clear now? – zhk Nov 20 '13 at 8:45