Here is a direct approach. The usage is explained in the code comments. I have had an issue with pgfplots
, I don't know why passing red
, or draw=red
, or color=red
to addplot
has an impact on the line thickness of the plot.
update: downgraded to compat=1.12
as I realized 1.13
is very recent. Also, it seems \addplot+[options]
is what I should use. But the result is a bit ... artistic. (last image)
The code is for any (non-negative, ordered) integer sequence given as a comma separated list. Nothing here for computing primes. The sample generates for demonstration a random sequence using \pdfuniformdeviate
.
\documentclass[tikz,ignorerest=false, border=12pt]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.12}% 1.13 seems to be very recent
% generated a random strictly increasing sequence of 30 integers
% for the purpose of testing
\pdfsetrandomseed 1234
\makeatletter
\newcommand*\seqA {}%
\edef\@tempa {\pdfuniformdeviate10}%
\edef\seqA {\@tempa}%
% for very very long lists, there are faster ways.
% but let's not bother here.
\count@ 29
\loop
\edef\@tempa {\the\numexpr\@tempa+\@ne+\pdfuniformdeviate10}%
\edef\seqA {\seqA, \@tempa}%
\advance\count@\m@ne
\ifnum\count@>\z@
\repeat
\typeout{\string\seqA\space prepared with meaning: \meaning\seqA}
% with pdfrandomseed=1234
% \seqA prepared with meaning macro:->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
% \CumulCnts expandably constructs pairs (x, \pi_S(x)), 0≤ x ≤ xmax,
% for sequence S, given as comma separated increasing list
% It admits optional argument, default xmax=100 to limit x.
% usage: \CumulCnts[optional max x]{\A} will expand to the list of pairs
% inside an \edef or a \csname...\endcsname.
% \edef\cumlA {\CumulCnts[optional max x]{\A}}
% and then use \cumulA
\newcommand*\CumulCnts {}
\def\CumulCnts #1{\expandafter\CumulCnts@i\romannumeral`\^^@#1,\relax,}%
\def\CumulCnts@i #1{\ifx [#1\expandafter\CumulCnts@opt\else
\expandafter\CumulCnts@noopt\fi #1}%
\def\CumulCnts@opt [#1,\relax,#2]#3%
{\expandafter\CumulCnts@ii
\the\numexpr #2\expandafter;\romannumeral`\^^@#3,\relax,}%
\def\CumulCnts@noopt {\CumulCnts@ii 100;}%
\def\CumulCnts@ii {\CumulCnts@iii 0;0;}%
\def\CumulCnts@iii #1;#2;#3;#4#5,{%
\if\relax #4\expandafter\CumulCnts@finish\fi
\ifnum #3<#4#5
\expandafter\@firstoftwo
\else
\expandafter\@secondoftwo
\fi
{\CumulCnts@c #1;#2;#3;}
{\CumulCnts@iv #1;#2;#4#5;{#3}}%
}%
\def\CumulCnts@finish \ifnum #1\fi #2#3{#2\relax,}
\def\CumulCnts@iv #1;#2;#3;{%
\ifnum #1=#3 \expandafter\CumulCnts@v\fi
(#1, #2)
\expandafter\CumulCnts@iv\the\numexpr #1+\@ne;#2;#3;%
}
\def\CumulCnts@v #1;#2;#3;{\expandafter\CumulCnts@vi\the\numexpr #2+\@ne;#3;}%
\def\CumulCnts@vi #1;#2;#3{%
(#2, #1)
\expandafter\CumulCnts@iii \the\numexpr#2+\@ne;#1;#3;}%
\def\CumulCnts@c #1;#2;#3;{%
\ifnum #1>#3 \expandafter\CumulCnts@d\fi
(#1, #2)
\expandafter\CumulCnts@c\the\numexpr #1+\@ne;#2;#3;%
}%
\def\CumulCnts@d #1;#2;#3;#4\relax,{}%
\makeatletter
\begin{document}
\edef\cumulA {\CumulCnts{\seqA}}
\typeout {\string\cumulA\space prepared with meaning: \meaning\cumulA}
\begin{tikzpicture}
\begin{axis}[
title={The counting function: $\pi_S(x)$},
xlabel=$x$,
ylabel=$\pi_S(x)$,
]
\addplot coordinates {\cumulA};
\end{axis}
\end{tikzpicture}
% extending to x≤200
\edef\cumulA {\CumulCnts[200]{\seqA}}
\typeout {\string\cumulA\space prepared with meaning: \meaning\cumulA}
\begin{tikzpicture}
\begin{axis}[
title={The counting function: $\pi_S(x)$},
xlabel=$x$,
ylabel=$\pi_S(x)$,
]
\addplot[red] coordinates {\cumulA};
\end{axis}
\end{tikzpicture}
\end{document}


Adding here for the passers-by what gets written to the log (lines cut by TeX to 79 characters)
First our toy random sequence:
\seqA prepared with meaning: macro:->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
Then the result of doing: \edef\cumulA {\CumulCnts[200]{\seqA}}
(there is a space token after the final coordinate pair; if people protest the code can be modified to not have it...).
\cumulA prepared with meaning: macro:->(0, 0) (1, 0) (2, 0) (3, 0) (4, 0) (5, 0
) (6, 1) (7, 2) (8, 2) (9, 2) (10, 2) (11, 2) (12, 2) (13, 2) (14, 3) (15, 3) (
16, 3) (17, 3) (18, 3) (19, 3) (20, 3) (21, 3) (22, 4) (23, 4) (24, 4) (25, 4)
(26, 4) (27, 4) (28, 4) (29, 4) (30, 4) (31, 5) (32, 6) (33, 6) (34, 6) (35, 6)
(36, 6) (37, 6) (38, 7) (39, 7) (40, 7) (41, 7) (42, 7) (43, 7) (44, 7) (45, 7
) (46, 8) (47, 8) (48, 8) (49, 8) (50, 8) (51, 8) (52, 9) (53, 9) (54, 9) (55,
9) (56, 9) (57, 9) (58, 9) (59, 9) (60, 10) (61, 10) (62, 10) (63, 10) (64, 10)
(65, 11) (66, 11) (67, 11) (68, 11) (69, 11) (70, 12) (71, 12) (72, 12) (73, 1
2) (74, 12) (75, 12) (76, 12) (77, 12) (78, 12) (79, 12) (80, 13) (81, 14) (82,
14) (83, 14) (84, 14) (85, 14) (86, 15) (87, 15) (88, 15) (89, 15) (90, 16) (9
1, 16) (92, 16) (93, 16) (94, 16) (95, 17) (96, 17) (97, 17) (98, 17) (99, 17)
(100, 18) (101, 18) (102, 18) (103, 18) (104, 18) (105, 18) (106, 18) (107, 18)
(108, 19) (109, 19) (110, 19) (111, 19) (112, 19) (113, 19) (114, 19) (115, 19
) (116, 19) (117, 20) (118, 20) (119, 21) (120, 21) (121, 21) (122, 21) (123, 2
1) (124, 21) (125, 21) (126, 22) (127, 22) (128, 22) (129, 22) (130, 22) (131,
22) (132, 22) (133, 22) (134, 22) (135, 23) (136, 23) (137, 23) (138, 23) (139,
23) (140, 24) (141, 24) (142, 24) (143, 24) (144, 24) (145, 24) (146, 24) (147
, 24) (148, 25) (149, 25) (150, 25) (151, 25) (152, 25) (153, 25) (154, 25) (15
5, 25) (156, 25) (157, 25) (158, 26) (159, 26) (160, 26) (161, 26) (162, 26) (1
63, 26) (164, 26) (165, 27) (166, 27) (167, 27) (168, 27) (169, 27) (170, 27) (
171, 27) (172, 28) (173, 28) (174, 28) (175, 28) (176, 29) (177, 29) (178, 29)
(179, 30) (180, 30) (181, 30) (182, 30) (183, 30) (184, 30) (185, 30) (186, 30)
(187, 30) (188, 30) (189, 30) (190, 30) (191, 30) (192, 30) (193, 30) (194, 30
) (195, 30) (196, 30) (197, 30) (198, 30) (199, 30) (200, 30)
With :
\begin{tikzpicture}
\begin{axis}[
title={The counting function: $\pi_S(x)$},
xlabel=$x$,
ylabel=$\pi_S(x)$,
]
\addplot+[color=red] coordinates {\cumulA};
\end{axis}
\end{tikzpicture}


/pgfplots/hist/cumulative
?\begin{axis}[hist=cumulative] \addplot[only marks] table [y index=0] { 2 3 5 7 11 … 1811 }; \end{axis}
does not work.