TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I wish to create this plot preferably in pgfplots or tikz.


I have actually got a version plotted (with help from SO), but I have used R and exported the plot using tikzDevice. I can't post the tikz code because it is way over the limit for posting (so I'll give the R code instead).

ps <- ldply(0:35, function(i)data.frame(s=0:i, n=i))
 plot.window(c(0,36), c(0,1))
 apply(ps[ps$s<6 & ps$n - ps$s < 30, ], 1, function(x){
   s<-x[1]; n<-x[2];
   lines(c(n, n+1, n, n+1), c(s/n, s/(n+1), s/n, (s+1)/(n+1)), type="o")})
 lines(6:36, 6/(6:36), type="o")
 # need to fill in the unconnected points on the upper frontier

Q. So looking to plot directly with tikz/pgfplots rather than via R and tikzdevice.


share|improve this question
up vote 4 down vote accepted

You can do this with three plots: One for the decreasing lines, one for the increasing ones (with dots), and one for the row of six dots at the top. To make the increasing lines stop at the last decreasing one, you can use a comparison operator that becomes 0 when the increasing line is higher than the last decreasing one, and divide by that: Unbounded coordinates are dropped by default.


    declare function={f(\x,\n) = (1/(\x)*(\n));}
    restrict y to domain=0:1,
    axis lines*=left,
    xmin=1, xmax=36,
    xtick = {1,6,...,36},
    ytick = {0,0.5,1},
    mark size=1, mark=*

% Draw decreasing lines
    \addplot [no markers,domain=1:30+#1, samples=30+#1] {f(x,#1)};

% Draw increasing lines, but only up to the level of the last decreasing line
% (that's what the "1 / ..." part does. 0.001 is for preventing precision errors)
    \addplot [domain=1:36, samples=36] {1-f(x,#1) * 1/( (1-(f(x,#1))) <= (f(x,6)+0.001)};

% Draw top markers
\addplot [domain=1:6, samples=6] {1};

share|improve this answer
Certainly much less code than the tikzDevice provides. Will examine and learn. Thanks. – Frank_Zafka Nov 15 '12 at 12:53

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.