2
$\begingroup$

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?

$\endgroup$
4
$\begingroup$

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

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

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]

enter image description here

$\endgroup$
0
$\begingroup$

bkarpuz's suggestion works perfectly:

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

plot

$\endgroup$
  • $\begingroup$ I added a function definition and new figures. I do not know why they are listed above your answer. $\endgroup$ – Gommaire Oct 8 '18 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$ – J. M. is in limbo Oct 8 '18 at 15:22
0
$\begingroup$

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]

enter image description here

And further on, you may wish to show

plotPrimes[37]

enter image description here

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

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.