9
$\begingroup$

I'm trying to have the integers evenly spaced on the x axis of a ListPlot, and the prime numbers evenly spaced on the y axis.

maxPrimeIndex = 25;
maxPrime = Prime[maxPrimeIndex];
Flatten[(Partition[
      Riffle[Flatten[FactorInteger[#]][[;; ;; 2]], #, {1, -2, 2}], 2]
     ) & /@ Range[2, maxPrime], 1];
ListPlot[%, 
 Ticks -> {Automatic, Prime[Range[1, maxPrimeIndex]]},
 PlotRange -> {{0, maxPrime}, {0, maxPrime}}
 ]

Which gives: Output of mathematica

How can I get the y axis to be evenly spaced?

$\endgroup$
7
  • 1
    $\begingroup$ "prime numbers" and "evenly spaced" sounds like incompatible conditions. What exactly are you envisioning here? $\endgroup$ Jun 7, 2020 at 16:19
  • $\begingroup$ Is it impossible to evenly space an arbitrary set? I'm just messing around; visualizing how many divisors there are around certain primes. $\endgroup$
    – Neil_P
    Jun 7, 2020 at 16:20
  • $\begingroup$ In the case of the primes, the spacing not being regular is well-known. $\endgroup$ Jun 7, 2020 at 16:23
  • 4
    $\begingroup$ If your points are only primes you can try adding ScalingFunctions -> {PrimePi, Prime} option to ListPlot. $\endgroup$
    – kirma
    Jun 7, 2020 at 16:23
  • $\begingroup$ @J.M.'stechnicaldifficulties But whether or not there are infinite spacings of 2 or 4 or 220 are unknown. $\endgroup$
    – Neil_P
    Jun 7, 2020 at 16:24

2 Answers 2

14
$\begingroup$

As people on the comments pointed out this may be a problematic in various ways, but if all y-values of your points are primes, this can be accomplished with ScalingFunctions option. PrimePi is an inverse of the Prime function, and you list both to guide ListPlot:

maxPrimeIndex = 25;
maxPrime = Prime[maxPrimeIndex];
Flatten[(Partition[
      Riffle[Flatten[FactorInteger[#]][[;; ;; 2]], #, {1, -2, 2}], 2]
     ) & /@ Range[2, maxPrime], 1];
ListPlot[%, 
 Ticks -> {Automatic, Prime[Range[1, maxPrimeIndex]]},
 PlotRange -> {{0, maxPrime}, {0, maxPrime}},
 ScalingFunctions -> {PrimePi, Prime}]

enter image description here

$\endgroup$
3
$\begingroup$

You can also map PrimePi on the second column of input data and change the vertical axis tick labels using custom ticks:

maxPrimeIndex = 25;
maxPrime = Prime[maxPrimeIndex];

data  = Flatten[
   Partition[Riffle[Flatten[FactorInteger[#]][[;; ;; 2]], #, {1, -2, 2}], 2] & /@ 
    Range[2, maxPrime], 1];

ListPlot[MapAt[PrimePi, data, {All, -1}], 
 Ticks -> {Automatic, 
   Thread[{Range @ maxPrimeIndex, Prime @ Range @ maxPrimeIndex}]}, 
 PlotRange -> {{0, maxPrime}, {0, maxPrimeIndex}}]

enter image description here

$\endgroup$
1
  • 2
    $\begingroup$ Yeah, this alternative came to my mind too. I wonder if there are cases where these solutions are not effectively identical. Certainly it's of pedagogical value to point out that tick labels don't need to correspond their actual coordinate values! $\endgroup$
    – kirma
    Jun 8, 2020 at 4:44

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.