I want to create a plot with vertical gridlines for prime x, and no gridlines otherwise. I can do it manually:

Plot[PrimePi[x], {x, 0, 100}, GridLines -> 
{{2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 
 73, 79, 83, 89, 97}, None}, PlotPoints -> 100]

But I can't find a way of using (say) PrimeQ to automate the process; I have to enter each prime value by hand.

I suspect there is an answer on MSE already, but I have searched and failed to find it.



4 Answers 4


I am a novice, but this does not seem too difficult. Let us use variable q instead of the integer 100. Then:

q = 100;
Plot[PrimePi[x], {x, 0, q}, GridLines -> {Table[Prime[n], {n, PrimePi[q]}], None},
     PlotPoints -> q]

plot of prime-counting function

  • $\begingroup$ I have marked this as answered, but I do have one further question. Is there a way of doing this using Replace instead of assigning a value to q which must then be cancelled before any further work is carried out? Putting /. q -> 20 on the end works, but produces annoying error messages. $\endgroup$ Commented Oct 6, 2018 at 23:21
  • $\begingroup$ Maybe you can use With $\endgroup$
    – bkarpuz
    Commented Oct 7, 2018 at 10:49
  • $\begingroup$ That works. Do you want to add it as an answer and I'll mark it up? Thanks, BTW :-) $\endgroup$ Commented Oct 8, 2018 at 11:47

Using With (as suggested by bkarpuz) we can define an own function plotPrimes[x_] where the argument x is the integer to be entered in PrimePi[]. Apparently, Richard Burke-Ward already did so.

If we wish to experiment with another kind of plot where we put the primes as ticks on the X-axis, we may define an alternative function. We put the prime values alternately on two X-axes to enhance readability. The color and size of the font may be adjusted in Directive[Black,9].

plotPrimesAlt[z_Integer] :=

 With[{q = z}, DiscretePlot[PrimePi[x], {x, 0, q},
   GridLines -> {Table[Prime[n], {n, PrimePi[q]}], None},
   FrameTicks -> 
     {Range[0, q], Range[0, q]},
     {Table[Prime[n], {n, PrimePi[q]}][[1 ;; ;; 2]], 
      Table[Prime[n], {n, PrimePi[q]}][[2 ;; ;; 2]]}
   FrameTicksStyle -> Directive[Black, 9], PlotLegends -> None,
   PlotTheme -> "Detailed", ExtentSize -> Right, ImageSize -> Large]]

Let us use the alternative function with x=100 as in the original question:


enter image description here


bkarpuz's suggestion works perfectly:

With[{q = 100},
          Plot[PrimePi[x], {x, 0, q},
                   GridLines -> {Table[Prime[n], {n, PrimePi[q]}], None},
                    PlotPoints -> q]]


  • $\begingroup$ I added a function definition and new figures. I do not know why they are listed above your answer. $\endgroup$
    – Gommaire
    Commented Oct 8, 2018 at 15:14
  • $\begingroup$ @Gommaire, that is because answers are sorted by vote counts by default. Try clicking on the "active" tab above the answers. $\endgroup$ Commented Oct 8, 2018 at 15:22

I fully agree that bkarpuz's suggestion works perfectly. However, you have to assign a value to q and repeat the lines for plotting. If you first define a function:

plotPrimes[z_Integer] :=
 With[{q = z}, Plot[PrimePi[x], {x, 0, q},
   GridLines -> {Table[Prime[n], {n, PrimePi[q]}], None},
   PlotPoints -> q]]

, then you can use this function repeatedly in further work without having to repeat Plot[.....]. May be I am mistaken, but I suppose you may wish to do something like:


enter image description here

And further on, you may wish to show


enter image description here

  • $\begingroup$ If it is not an entirely new solution and merely an addition to a previous answer, please edit your answer instead of making a new one. $\endgroup$ Commented Oct 8, 2018 at 15:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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