Here is a first attempt with MetaPost, with a number of iterations n to be chosen at will, 10 on this example.
It surely needs refinement, but it shows anyway it can be done with it. To be typeset with LuaLaTeX.
Edit A second, recursive implementation, much more refined.
vardef cantor(expr A, B, n) =
if n = 0 : A -- B
else:
save C, D; pair C, D;
C = (1/3[xpart A, xpart B], .5[ypart A, ypart B]);
D = (2/3[xpart A, xpart B], ypart C);
cantor(A, C, n-1) -- cantor(D, B, n-1)
fi
enddef;
Now n is taken equal to 15 in the subsequent application, but I guess it could be a deal more with this implementation.
Edit bis For better presentation I have added a grid similar to Qrrbrbirlbel's one.
\documentclass[border=2mm]{standalone}
\usepackage{amsmath, luamplib}
\mplibtextextlabel{enable}
\mplibnumbersystem{double}
\begin{document}
\begin{mplibcode}
u := 10cm;
vardef cantor(expr A, B, n) =
if n = 0 : A -- B
else:
save C, D; pair C, D;
C = (1/3[xpart A, xpart B], .5[ypart A, ypart B]);
D = (2/3[xpart A, xpart B], ypart C);
cantor(A, C, n-1) -- cantor(D, B, n-1)
fi
enddef;
beginfig(1);
% Grid and axes
for i = 1 upto 9: draw (i*u/9, 0) -- (i*u/9, u) withcolor .8white; endfor
for j = 1 upto 4: draw (0, j*u/4) -- (u, j*u/4) withcolor .8white; endfor
drawarrow origin -- (1.1u, 0); drawarrow origin -- (0, 1.1u);
% The function
draw cantor(origin, (1, 1), 15) scaled u;
% labels
label.llft("$O$", origin); label.bot("$1$", (u, 0)); label.lft("$1$", (0, u));
label.bot("$x$", (1.1u, 0)); label.lft("$y$", (0, 1.1u));
label.bot("$\dfrac{1}{3}$", (1/3u, 0)); label.bot("$\dfrac{2}{3}$", (2/3u, 0));
label.lft("$\dfrac{1}{2}$", (0, .5u));
endfig;
\end{mplibcode}
\end{document}
