# Plot vertical gridlines for prime values of x

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.

Suggestions?

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]


• 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. – Richard Burke-Ward Oct 6 '18 at 23:21
• Maybe you can use With – bkarpuz Oct 7 '18 at 10:49
• That works. Do you want to add it as an answer and I'll mark it up? Thanks, BTW :-) – Richard Burke-Ward Oct 8 '18 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:

plotPrimesAlt[100]


bkarpuz's suggestion works perfectly:

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


• I added a function definition and new figures. I do not know why they are listed above your answer. – Gommaire Oct 8 '18 at 15:14
• @Gommaire, that is because answers are sorted by vote counts by default. Try clicking on the "active" tab above the answers. – J. M. is in limbo Oct 8 '18 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:

plotPrimes[13]


And further on, you may wish to show

plotPrimes[37]


• 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. – J. M. is in limbo Oct 8 '18 at 15:21