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

enter image description here

Incidentally, I am using v 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]

Mathematica graphics

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]

Mathematica graphics

  • $\begingroup$ @ Sjoerd C. de Vries, many thanks - the bottom plot is what I was really after! :) $\endgroup$ – 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]

Mathematica graphics

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[200]]], 
 ExclusionsStyle -> ColorData[1][1]]

Mathematica graphics

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]

enter image description here

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

enter image description here

For a large domain - let's try x=100..200

enter image description here

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

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.