Given a list of primes, how can I plot π(x)?

You can use pgfplotstable to create a table that contains one column that stores the output of isprime for every number, and one that counts the number of primes encountered so far by summing up the isprime values:

\documentclass{article}
\usepackage{pgfplots, pgfplotstable}
\pgfplotsset{compat=1.12}

\begin{document}
\pgfplotstablenew[
    create on use/x/.style={
        create col/expr={
            \pgfplotstablerow
        }
    },
    create on use/isprime/.style={
        create col/assign/.code={% Can't use "expr" here because "isint" doesn't work reliably with the FPU engine at the moment
            \pgfmathparse{isprime(\thisrow{x})}%
            \pgfkeyslet{/pgfplots/table/create col/next content}\pgfmathresult%
        }
    },
    create on use/primecount/.style={
        create col/expr={
            \pgfmathaccuma + \thisrow{isprime}
        }
    },
    columns={x, isprime, primecount}
]{25}\loadedtable

\begin{tikzpicture}
\begin{axis}[
  title={The prime counting function: $\pi(x)$},
  xlabel=$x$,
  ylabel=$\pi(x)$,
  ]
\addplot[only marks] table [x=x, y=primecount] {\loadedtable};
\end{axis}
\end{tikzpicture}
\end{document}

And here's a way of plotting the counting function for an ordered list, using a PGF math array and a counter pointing to the next element in the list:

\documentclass{article}
\usepackage{pgfplots, pgfplotstable}
\pgfplotsset{compat=1.12}

\begin{document}

\def\mylist{{6, 7, 14, 22, 31, 32, 38, 46, 52, 60, 65, 70, 80, 81, 86, 90, 95, 100, 108, 117, 119, 126, 135, 140, 148, 158, 165, 172, 176, 179}}
\newcounter{listindex}

\pgfplotstablenew[
    create on use/x/.style={
        create col/expr={
            \pgfplotstablerow
        }
    },
    create on use/isinlist/.style={
        create col/assign/.code={
            \pgfmathtruncatemacro\thisx{\thisrow{x}}
            \pgfmathtruncatemacro\nextlistitem{\mylist[\value{listindex}]}
            \ifnum\thisx=\nextlistitem
                \def\result{1}
                \stepcounter{listindex}
            \else
                \def\result{0}
            \fi
            \pgfkeyslet{/pgfplots/table/create col/next content}{\result}%
        }
    },
    create on use/count/.style={
        create col/expr={
            \pgfmathaccuma + \thisrow{isinlist}
        }
    },
    columns={x, isinlist, count}
]{50}\loadedtable

\begin{tikzpicture}
\begin{axis}[
  title={The prime counting function: $\pi(x)$},
  xlabel=$x$,
  ylabel=$\pi(x)$,
  ]
\addplot[only marks] table [x=x, y=count] {\loadedtable};
\end{axis}
\end{tikzpicture}
\end{document}

A sagetex solution:

\documentclass{standalone}
\usepackage{sagetex}
\usepackage{pgfplots}
\pgfplotsset{compat=1.12}
\begin{document}
\begin{sagesilent}
output = r""
output += r"\begin{tikzpicture}"
output += r"\begin{axis}["
output += r"title={The prime counting function: $\pi(x)$},"
output += r"xlabel=$x$,"
output += r"ylabel=$\pi(x)$,"
output += r"]"
output += r"\addplot[only marks] coordinates {"
for i in range(0,20):
    output += r"(%s, %s)"%(i,prime_pi(i))
output += r"};"
output += r"\end{axis}"
output += r"\end{tikzpicture}"  
\end{sagesilent}
\sagestr{output}
\end{document}

This gives the following output in Sagemath Cloud: enter image description here

All you have to do is change 20 to whatever you want and compile the code to get the revised graph. Note that in Python the last number, 20, doesn't execute. This relies on having Sage on your computer or, easier still, using SagemathCloud (internet connection required). Sage handles the calculations; the y-values are determined by the function prime_pi() which is documented here. No files to read, Sage creates the code on-the-fly.


"Pure-TeX" solution can look like:

\def\primes{2,3,5,7,11,13,17,19,23,29,31,37}

\newcount\tmpnum  \newcount\a
\def\ppi#1{\tmpnum=0 \def\ppiA{#1}\expandafter\ppiB\primes,,\relax}
\def\ppiB#1,{\ifx,#1,\message{use more primes}\def\ppiOUT{0}\else
   \ifnum\ppiA<#1\relax \edef\ppiOUT{\the\tmpnum}\ppiC
   \else \advance\tmpnum by1 \fi
   \expandafter\ppiB\fi
}
\def\ppiC#1,\relax{\fi\fi}

\a=0
\loop
    \ppi\a
    \hbox{\hbox to2em{\hss\the\a:}\hbox to2em{\hss\ppiOUT}}
    \ifnum\a<36 \advance\a by1
    \repeat

\bye

And visualisation:

\newdimen\ystep \ystep=2mm
\vbox{\hrule\hbox{\vrule height28mm depth3mm \kern2mm
\loop
    \ppi\a
    \raise\ppiOUT\ystep\hbox{$\bullet$}\kern.1mm
    \ifnum\a<36 \advance\a by1
    \repeat
\kern2mm\vrule}\hrule}

pi

Note that the graphics is "pure-TeX" too. No PostScript, no Tikz, no \pdfspecial.

Tags:

Pgfplots

Fun